Skip to content

Commit 8e1543f

Browse files
support cli usage within codefresh pipelines (#597)
* support cli usage within codefresh pipelines * add new pagination interface * fix usage * fix comments * fix * fix * remove v2 builds inteface. add start end date filters * bump * Validate dates * bump
1 parent 0c31cc1 commit 8e1543f

File tree

7 files changed

+92
-15
lines changed

7 files changed

+92
-15
lines changed

lib/interface/cli/Command.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ class Command {
5050
this.builders = [];
5151
this.addBuilder(this._createUsageBuilder.bind(this));
5252

53-
(_.isUndefined(command.handler)) ? command.handler = (args) => {
54-
Output.printError(`Error: unknown command "${args._[args._.length - 1]}"\n` +
55-
'Run \'codefresh --help\' for usage.');
56-
57-
} : _.noop();
58-
(_.isUndefined(command.builder)) ? _.noop() :
53+
if (_.isUndefined(command.handler)) {
54+
command.handler = (args) => {
55+
Output.printError(`Error: unknown command "${args._[args._.length - 1]}"\n` +
56+
'Run \'codefresh --help\' for usage.');
57+
};
58+
}
59+
if (!_.isUndefined(command.builder)) {
5960
this.addBuilder(command.builder);
61+
}
6062
}
6163

6264
_createBuilderFunction() {

lib/interface/cli/commad-line-interface.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { version } = require('../../../package.json');
99
const configManager = require('../../logic/cli-config/Manager');
1010
const openapi = require('../../../openapi');
1111
const sdk = require('../../logic/sdk');
12+
const cliConfHelpers = require('./helpers/cli-config');
1213

1314
const PROCESS_ARGV = require('yargs-parser')(process.argv);
1415

@@ -25,7 +26,7 @@ async function getConfigForSdk() {
2526
'Codefresh-User-Agent-Type': 'cli',
2627
'Codefresh-User-Agent-Version': version,
2728
},
28-
}, cliConfig.request),
29+
}, cliConfig.request, cliConfHelpers.getSdkConfigForPagination().request),
2930
};
3031
return Config.load(configOptions);
3132
}

lib/interface/cli/commands/workflow/get.cmd.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,32 @@ const command = new Command({
7474
type: Array,
7575
default: [],
7676
})
77+
.option('from', {
78+
describe: 'Date in format: YYYY-MM-DD. Show builds from the provided date',
79+
type: String,
80+
})
81+
.option('to', {
82+
describe: 'Date in format: YYYY-MM-DD. Show builds up to provided date',
83+
type: String,
84+
})
85+
.check((argv) => {
86+
const { from, to } = argv;
87+
88+
const dateRegex = /^\d{4}-\d{1,2}-\d{1,2}$/;
89+
if (from && !dateRegex.test(from)) {
90+
throw new Error('--from option should take a date in format: YYYY-MM-DD. Example: --from 2020-12-31');
91+
}
92+
if (to && !dateRegex.test(to)) {
93+
throw new Error('--to option should take a date in format: YYYY-MM-DD. Example: --to 2020-12-31');
94+
}
95+
return true;
96+
})
7797
.example('codefresh get build ID', 'Get build ID')
7898
.example('codefresh get builds', 'Get all builds')
7999
.example('codefresh get builds --pipeline-id ID', 'Get all builds that are executions of pipeline "ID"')
80100
.example('codefresh get builds --pipeline-name NAME', 'Get all builds that are executions of pipelines that are named "NAME"')
81-
.example('codefresh get builds --status=error', 'Get all builds that their status is error');
82-
101+
.example('codefresh get builds --status=error', 'Get all builds that their status is error')
102+
.example('codefresh get builds --from=2020-01-01 --to=2020-02-01', 'Get all builds within given date range');
83103
},
84104
handler: async (argv) => {
85105
const workflowIds = argv.id;
@@ -91,9 +111,12 @@ const command = new Command({
91111
branch: branchName,
92112
commitId: revision,
93113
'pipeline-trigger-id': pipelineTriggerId,
114+
from,
115+
to,
94116
} = argv;
95117
const pipelineNames = !_.isArray(argv['pipeline-name']) ? [(argv['pipeline-name'])] : argv['pipeline-name'];
96118
const pipelineIds = !_.isArray(argv['pipeline-id']) ? [(argv['pipeline-id'])] : argv['pipeline-id'];
119+
97120
const requestOptions = {
98121
limit,
99122
page,
@@ -102,6 +125,9 @@ const command = new Command({
102125
branchName,
103126
revision,
104127
pipelineTriggerId,
128+
paginationReset: page === 1,
129+
startDate: from,
130+
endDate: to,
105131
};
106132

107133
let workflows = [];

lib/interface/cli/helpers/cli-config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const _jsonFormatter = data => JSON.stringify(data, null, 4);
99
const COLUMNIFY_OPTS = { columnSplitter: ' ', headingTransform: Style.bold.uppercase };
1010

1111
const NO_PROPERTIES_MESSAGE = 'no properties';
12+
const PAGINATION_SESSION_HEADER = 'X-Pagination-Session-Id';
1213

1314
function _valueFormatter(value) {
1415
if (value === null) {
@@ -87,9 +88,23 @@ function propertyErrorHandler(e) {
8788
throw e;
8889
}
8990

91+
function getSdkConfigForPagination() {
92+
const conf = {};
93+
const { HOSTNAME, CF_BUILD_ID } = process.env;
94+
95+
// if cli runs inside of the codefresh pipeline need to create pagination sessionId according to the build id and container id
96+
if (HOSTNAME && CF_BUILD_ID) {
97+
// HOSTNAME – container id inside docker container
98+
const sessionId = `${HOSTNAME}:${CF_BUILD_ID}`;
99+
_.set(conf, `request.headers.${PAGINATION_SESSION_HEADER}`, sessionId);
100+
}
101+
return conf;
102+
}
103+
90104
module.exports = {
91105
outputCliConfig,
92106
printProperties,
93107
propertyErrorHandler,
94108
outputCliMeta,
109+
getSdkConfigForPagination,
95110
};

openapi.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,40 @@
21792179
"type": "string"
21802180
},
21812181
"description": "Pipeline trigger id"
2182+
},
2183+
{
2184+
"name": "paginationReset",
2185+
"in": "query",
2186+
"schema": {
2187+
"type": "string"
2188+
},
2189+
"description": "Should pagination session be reseted to first page"
2190+
},
2191+
{
2192+
"name": "paginationDirection",
2193+
"in": "query",
2194+
"schema": {
2195+
"type": "string"
2196+
},
2197+
"description": "Direction of the pagination: 'prev' or 'next'"
2198+
},
2199+
{
2200+
"name": "startDate",
2201+
"in": "query",
2202+
"schema": {
2203+
"type": "string"
2204+
},
2205+
"description": "Lower date filter bound"
2206+
},
2207+
{
2208+
"name": "endDate",
2209+
"in": "query",
2210+
"schema": {
2211+
"type": "string"
2212+
},
2213+
"description": "Upper date filter bound"
21822214
}
2215+
21832216
],
21842217
"responses": {
21852218
"200": {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.73.28",
3+
"version": "0.73.30",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,
@@ -37,7 +37,7 @@
3737
"cf-errors": "^0.1.12",
3838
"chalk": "^4.1.0",
3939
"cli-progress": "3.6.0",
40-
"codefresh-sdk": "^1.9.14",
40+
"codefresh-sdk": "^1.9.15",
4141
"colors": "^1.1.2",
4242
"columnify": "^1.5.4",
4343
"compare-versions": "^3.4.0",

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,10 +1258,10 @@ code-point-at@^1.0.0:
12581258
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
12591259
integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=
12601260

1261-
codefresh-sdk@^1.9.14:
1262-
version "1.9.14"
1263-
resolved "https://registry.yarnpkg.com/codefresh-sdk/-/codefresh-sdk-1.9.14.tgz#e0751abd3eb7ce44da501f5130541126d5d40d75"
1264-
integrity sha512-fQ7gUUUyFQduRoTzhViAJv6d+6wcCQOn+dYQT9VKyos07GETYWFG/Sr4bG7TYlE5M0Yxn5xfkaIvXv+8GczrXQ==
1261+
codefresh-sdk@^1.9.15:
1262+
version "1.9.15"
1263+
resolved "https://registry.yarnpkg.com/codefresh-sdk/-/codefresh-sdk-1.9.15.tgz#ccd7e8a0815404f6d89171f27930808e4ca01419"
1264+
integrity sha512-/pk3cFpOxGlcK2rX7ejveB3oOa4TH0kkIgwLSJHHx7A+9NOta1kJXWtfj6khvct31gidrf/fmw6x29j/Zj4RgQ==
12651265
dependencies:
12661266
"@codefresh-io/cf-receiver" "0.0.1-alpha19"
12671267
bluebird "^3.5.3"

0 commit comments

Comments
 (0)