Skip to content

Commit 4d93662

Browse files
arikmaoritai-codefresh
authored andcommitted
Adding pipeline V2 beta commands (#51)
1 parent 14e7d13 commit 4d93662

File tree

14 files changed

+335
-16
lines changed

14 files changed

+335
-16
lines changed

examples/pipeline2.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: new-pipeline
2+
account: 59d33c4e9f75220a3710b2ee
3+
metadata:
4+
labels:
5+
repo: github:ArikMaor/ping-server
6+
spec:
7+
variables:
8+
- key: PORT
9+
value: 3000
10+
encrypted: false
11+
- key: PAPA
12+
value: BLA BLA
13+
encrypted: true
14+
steps:
15+
test_step:
16+
image: alpine
17+
commands:
18+
- echo "hello world"
19+
- echo "plain value $PORT"
20+
- echo "encrypted value $PAPA"

lib/interface/cli/Command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Command {
5050
b(yargs);
5151
_.forEach(this.subCommands, (subCommand) => {
5252
// TODO use .toCommand here too
53-
if (subCommand instanceof Command && (!subCommand.isBetaCommand() || authManager.getCurrentContext().beta)) {
53+
if (subCommand instanceof Command && (!subCommand.isBetaCommand() || authManager.getCurrentContext().isBetaFeatEnabled())) {
5454
yargs.command(subCommand.toCommand());
5555
} else {
5656
yargs.command(subCommand);

lib/interface/cli/codefresh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,23 @@ recursive(path.resolve(__dirname, 'commands'), (err, files) => {
5252
});
5353

5454

55-
56-
5755
yargs // eslint-disable-line
5856
.completion()
5957
.demandCommand(1, 'You need at least one command before moving on')
6058
.wrap(null)
6159
.version(false)
6260
.help('help')
63-
.option('help', {
64-
global: false,
65-
})
61+
.epilogue(`
62+
___| | _| | ___| | _ _|
63+
| _ \\ _\` | _ \\ | __| _ \\ __| __ \\ | | |
64+
| ( | ( | __/ __| | __/\\__ \\ | | | | | |
65+
\\____|\\___/ \\__,_|\\___|_| _| \\___|____/_| |_| \\____|_____|___|
66+
67+
68+
For more information, find our official documentation at http://cli.codefresh.io
69+
`)
70+
// .option('help', {
71+
// global: false,
72+
// })
6673
.argv;
6774
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { prepareKeyValueFromCLIEnvOption } = require('../../helpers/general');
6+
const { pipeline2 } = require('../../../../logic').api;
7+
const createRoot = require('../root/create.cmd');
8+
9+
const command = new Command({
10+
betaCommand: true,
11+
command: 'pipeline2 [name]',
12+
description: 'Create a pipeline',
13+
builder: (yargs) => {
14+
return yargs
15+
.positional('name', {
16+
describe: 'Name of context',
17+
});
18+
},
19+
handler: async (argv) => {
20+
const {filename, name} = argv;
21+
22+
if (!filename) {
23+
throw new CFError('Pipeline definitions file must be provided');
24+
}
25+
26+
if (!filename.name && !name) {
27+
throw new CFError('Name must be provided');
28+
}
29+
30+
const data = argv.filename;
31+
if (name) {
32+
data.name = name;
33+
}
34+
35+
await pipeline2.createPipeline(data);
36+
console.log(`Pipeline '${data.name}' created`);
37+
},
38+
});
39+
40+
createRoot.subCommand(command);
41+
42+
43+
module.exports = command;
44+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { pipeline2 } = require('../../../../logic').api;
6+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
7+
const deleteRoot = require('../root/delete.cmd');
8+
9+
10+
const command = new Command({
11+
betaCommand: true,
12+
command: 'pipeline2 [name]',
13+
description: 'Deletes a pipeline',
14+
builder: (yargs) => {
15+
return yargs
16+
.positional('name', {
17+
describe: 'Pipeline name',
18+
});
19+
},
20+
handler: async (argv) => {
21+
const {name} = argv;
22+
23+
await pipeline2.deletePipelineByName(name);
24+
console.log(`Pipeline '${name}' deleted.`);
25+
},
26+
});
27+
deleteRoot.subCommand(command);
28+
29+
30+
module.exports = command;
31+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { pipeline2 } = require('../../../../logic').api;
6+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
7+
const getRoot = require('../root/get.cmd');
8+
9+
10+
const command = new Command({
11+
betaCommand: true,
12+
command: 'pipelines2 [name]',
13+
aliases: ['pip2', 'pipeline2'],
14+
description: 'Get pipelines',
15+
builder: (yargs) => {
16+
return yargs
17+
.positional('name', {
18+
describe: 'Pipeline name',
19+
});
20+
},
21+
handler: async (argv) => {
22+
const {name, output} = argv;
23+
24+
if (name) {
25+
specifyOutputForSingle(output, await pipeline2.getPipelineByName(name));
26+
} else {
27+
specifyOutputForArray(output, await pipeline2.getAll());
28+
}
29+
},
30+
});
31+
getRoot.subCommand(command);
32+
33+
34+
module.exports = command;
35+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { prepareKeyValueFromCLIEnvOption } = require('../../helpers/general');
6+
const { pipeline2 } = require('../../../../logic').api;
7+
const replaceRoot = require('../root/replace.cmd');
8+
9+
const command = new Command({
10+
betaCommand: true,
11+
command: 'pipeline2 [name]',
12+
description: 'Replaces a pipeline',
13+
builder: (yargs) => {
14+
return yargs
15+
.positional('name', {
16+
describe: 'Name of context',
17+
});
18+
},
19+
handler: async (argv) => {
20+
const {filename, name} = argv;
21+
22+
if (!filename) {
23+
throw new CFError('Pipeline definitions file must be provided');
24+
}
25+
26+
if (!filename.name && !name) {
27+
throw new CFError('Name must be provided');
28+
}
29+
30+
const data = argv.filename;
31+
if (name) {
32+
data.name = name;
33+
};
34+
35+
await pipeline2.replaceByName(data.name, data);
36+
console.log(`Pipeline '${data.name}' updated`);
37+
},
38+
});
39+
40+
replaceRoot.subCommand(command);
41+
42+
43+
module.exports = command;
44+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const debug = require('debug')('codefresh:cli:create:pipelines2');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { pipeline2 } = require('../../../../logic').api;
6+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
7+
const runRoot = require('../workflow/run.cmd');
8+
9+
10+
const command = new Command({
11+
betaCommand: true,
12+
command: 'pipeline2 [name]',
13+
description: 'Runs a pipeline',
14+
builder: (yargs) => {
15+
return yargs
16+
.positional('name', {
17+
describe: 'Pipeline name',
18+
});
19+
},
20+
handler: async (argv) => {
21+
const {name, output} = argv;
22+
23+
const buildId = await pipeline2.runPipelineByName(name);
24+
console.log(`New build created for pipeline '${name}': ${buildId}`);
25+
},
26+
});
27+
runRoot.subCommand(command);
28+
29+
30+
module.exports = command;
31+

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ const Command = require('../../Command');
33
const _ = require('lodash');
44
const CFError = require('cf-errors');
55
const { prepareKeyValueFromCLIEnvOption, crudFilenameOption } = require('../../helpers/general');
6-
const { pipeline } = require('../../../../logic').api;
76
const ObjectID = require('mongodb').ObjectID;
8-
const { log } = require('../../../../logic').api;
9-
const { workflow } = require('../../../../logic').api;
7+
const { workflow, pipeline, pipeline2, log } = require('../../../../logic').api;
8+
const authManager = require('../../../../logic').auth.manager;
109

1110

1211
const run = new Command({
@@ -69,10 +68,15 @@ const run = new Command({
6968
const scale = argv['scale'];
7069
const variablesFromFile = argv['var-file'];
7170

72-
if (!ObjectID.isValid(pipelineId)) {
73-
throw new CFError({
74-
message: `Passed pipeline id: ${pipelineId} is not valid`,
75-
});
71+
72+
if (!authManager.getCurrentContext()
73+
.isBetaFeatEnabled()) {
74+
// validate that passed pipeline id an a mongo object id in case of pipeline V1
75+
if (!ObjectID.isValid(pipelineId)) {
76+
throw new CFError({
77+
message: `Passed pipeline id: ${pipelineId} is not valid`,
78+
});
79+
}
7680
}
7781

7882
const executionRequests = [];
@@ -101,8 +105,15 @@ const run = new Command({
101105
}
102106
}
103107

104-
_.forEach(executionRequests, async (request) => {
105-
const workflowId = await pipeline.runPipelineById(request.pipelineId, request.options);
108+
_.forEach(executionRequests, async ({ pipelineId, options }) => {
109+
let workflowId;
110+
if (authManager.getCurrentContext()
111+
.isBetaFeatEnabled()) {
112+
workflowId = await pipeline2.runPipelineByName(pipelineId, options);
113+
} else {
114+
workflowId = await pipeline.runPipelineById(pipelineId, options);
115+
}
116+
106117
if (executionRequests.length === 1) {
107118
if (argv.detach) {
108119
console.log(workflowId);

lib/logic/api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const user = require('./user');
22
const pipeline = require('./pipeline');
3+
const pipeline2 = require('./pipeline2');
34
const context = require('./context');
45
const image = require('./image');
56
const composition = require('./composition');
@@ -10,6 +11,7 @@ const log = require('./log');
1011
module.exports = {
1112
user,
1213
pipeline,
14+
pipeline2,
1315
context,
1416
image,
1517
composition,

lib/logic/api/pipeline2.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const _ = require('lodash'); // eslint-disable-line
2+
const CFError = require('cf-errors'); // eslint-disable-line
3+
const { sendHttpRequest } = require('./helper');
4+
const Pipeline = require('../entities/Pipeline2');
5+
6+
const getAll = async () => {
7+
const options = {
8+
url: '/api/pipelines/new',
9+
method: 'GET',
10+
};
11+
12+
const pipelines = await sendHttpRequest(options);
13+
return pipelines.map(p => new Pipeline(p));
14+
};
15+
16+
const getPipelineByName = async (name) => {
17+
const options = {
18+
url: `/api/pipelines/new/${name}`,
19+
method: 'GET',
20+
};
21+
22+
const pipeline = await sendHttpRequest(options);
23+
return new Pipeline(pipeline);
24+
};
25+
26+
const createPipeline = async (data) => {
27+
const options = {
28+
url: '/api/pipelines/new',
29+
method: 'POST',
30+
body: data,
31+
};
32+
33+
return sendHttpRequest(options);
34+
};
35+
36+
const replaceByName = async (name, data) => {
37+
const body = data;
38+
39+
const options = {
40+
url: `/api/pipelines/new/${name}`,
41+
method: 'PUT',
42+
body,
43+
};
44+
45+
return sendHttpRequest(options);
46+
};
47+
48+
const deletePipelineByName = async (name) => {
49+
const options = {
50+
url: `/api/pipelines/new/${name}`,
51+
method: 'DELETE',
52+
};
53+
54+
return sendHttpRequest(options);
55+
};
56+
57+
const runPipelineByName = async (name, data) => {
58+
59+
const body = _.pick(data, 'variables', 'branch');
60+
61+
const options = {
62+
url: `/api/pipelines/new/run/${name}`,
63+
method: 'POST',
64+
body
65+
};
66+
67+
return sendHttpRequest(options);
68+
};
69+
70+
module.exports = {
71+
getAll,
72+
getPipelineByName,
73+
createPipeline,
74+
replaceByName,
75+
deletePipelineByName,
76+
runPipelineByName,
77+
};

0 commit comments

Comments
 (0)