Skip to content

Commit fd3102f

Browse files
add support for restart/terminate +bump version
1 parent eff7f85 commit fd3102f

File tree

6 files changed

+110
-19
lines changed

6 files changed

+110
-19
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const DEFAULTS = require('../../defaults');
66
const { workflow , pipeline } = require('../../../../logic').api;
77
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
88
const getRoot = require('../root/get.cmd');
9+
const { printError } = require('../../helpers/general');
910

1011
const command = new Command({
1112
command: 'workflows [id]',
@@ -61,18 +62,18 @@ const command = new Command({
6162
workflows = await workflow.getWorkflowById(workflowId);
6263
specifyOutputForSingle(argv.output, workflows);
6364
} else {
64-
if (_.isEmpty(pipelineIds) && !_.isEmpty(pipelineNames)) {
65-
const pipelines = await pipeline.getAll();
66-
_.forEach(pipelineNames, (pipelineName) => {
67-
const matchPipelines = _.find(pipelines, pipeline => pipeline.info.name.toString() === pipelineName);
68-
if (_.isArray(matchPipelines)) {
69-
_.forEach(matchPipelines, (currPipeline) => {
70-
pipelineIds.push(currPipeline.info.id);
71-
});
72-
} else {
73-
pipelineIds.push(matchPipelines.info.id);
74-
}
65+
if (!_.isEmpty(pipelineNames)) {
66+
const pipelines = await pipeline.getAll({
67+
name: pipelineNames,
7568
});
69+
if (!_.isEmpty(pipelines)) {
70+
const MatchPipelines = _.isArray(pipelines) ? pipelines : [pipelines];
71+
_.forEach(MatchPipelines, (currPipeline) => {
72+
pipelineIds.push(currPipeline.info.id);
73+
});
74+
} else if (_.isEmpty(pipelineIds)) {
75+
throw new CFError('Cannot find any workflows with these pipelines names');
76+
}
7677
}
7778
workflows = await workflow.getWorkflows({
7879
limit,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const Command = require('../../Command');
2+
const { workflow } = require('../../../../logic/index').api;
3+
const { log } = require('../../../../logic').api;
4+
5+
const restart = new Command({
6+
root: true,
7+
command: 'restart <id>',
8+
description: 'Restart a workflow by its id',
9+
builder: (yargs) => {
10+
return yargs
11+
.positional('id', {
12+
describe: 'Workflow id',
13+
})
14+
.option('detach', {
15+
alias: 'd',
16+
describe: 'Run workflow and print workflow ID',
17+
});
18+
},
19+
handler: async (argv) => {
20+
const workflowId = await workflow.restartWorkflowById(argv.id);
21+
if (argv.detach) {
22+
console.log(workflowId);
23+
} else {
24+
await log.showWorkflowLogs(workflowId, true);
25+
const workflowInstance = await workflow.getWorkflowById(workflowId);
26+
switch (workflowInstance.getStatus()) {
27+
case 'success':
28+
process.exit(0);
29+
break;
30+
case 'error':
31+
process.exit(1);
32+
break;
33+
case 'terminated':
34+
process.exit(2);
35+
break;
36+
default:
37+
process.exit(100);
38+
break;
39+
}
40+
}
41+
},
42+
});
43+
44+
module.exports = restart;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Command = require('../../Command');
2+
const { workflow } = require('../../../../logic/index').api;
3+
4+
const terminate = new Command({
5+
root: true,
6+
command: 'terminate <id>',
7+
description: 'Terminate a workflow by its id',
8+
builder: (yargs) => {
9+
return yargs
10+
.positional('id', {
11+
describe: 'Workflow id',
12+
});
13+
},
14+
handler: async (argv) => {
15+
const workflowId = argv.id;
16+
await workflow.terminateWorkflowById(workflowId);
17+
console.log(`Workflow: ${workflowId} terminated`);
18+
},
19+
});
20+
21+
module.exports = terminate;

lib/logic/api/pipeline.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ const _extractFieldsForPipelineEntity = pipeline => ({
1313

1414

1515
const getAll = async (options) => {
16-
const qs = {
17-
name: options.name,
18-
limit: options.limit,
19-
page: options.page - 1,
20-
repoOwner: options.repoOwner,
21-
repoName: options.repoName,
22-
};
16+
let qs = {};
17+
if (options) {
18+
qs = {
19+
name: options.name,
20+
limit: options.limit,
21+
page: options.page - 1,
22+
repoOwner: options.repoOwner,
23+
repoName: options.repoName,
24+
};
25+
}
2326
const RequestOptions = {
2427
url: '/api/pipelines',
2528
qs,

lib/logic/api/workflow.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,30 @@ const waitForStatus = async (workflowId, desiredStatus, timeoutDate, descriptive
8181
}
8282
};
8383

84+
85+
const restartWorkflowById = async (id) => {
86+
const options = {
87+
url: `/api/builds/rebuild/${id}`,
88+
method: 'GET',
89+
};
90+
const response = await sendHttpRequest(options);
91+
return response;
92+
};
93+
94+
const terminateWorkflowById = async (id) => {
95+
const currWorkflow = await getWorkflowById(id);
96+
const options = {
97+
url: `/api/progress/${currWorkflow.info.progress}`,
98+
method: 'DELETE',
99+
};
100+
const response = await sendHttpRequest(options);
101+
return response;
102+
};
103+
84104
module.exports = {
85105
getWorkflowById,
86106
getWorkflows,
87107
waitForStatus,
108+
restartWorkflowById,
109+
terminateWorkflowById,
88110
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.5.3",
3+
"version": "0.5.4",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)