Skip to content

Commit c784d47

Browse files
authored
Merge pull request #115 from codefresh-io/triggers
implement trigger methods
2 parents e3df6a0 + cccd8ba commit c784d47

File tree

13 files changed

+437
-5
lines changed

13 files changed

+437
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules
22
yarn-error.log
33
.idea/
4+
.vscode
45
*.log
56
coverage
67
.DS_Store
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
const Command = require('../../Command');
22
const cliPackage = require('./../../../../../package.json');
3+
const { version } = require('../../../../logic').api;
34

4-
const version = new Command({
5+
const versionCmd = new Command({
56
root: true,
67
requiresAuthentication: false,
78
command: 'version',
89
cliDocs: {
910
description: 'Print version',
1011
},
1112
builder: (yargs) => {
12-
return yargs;
13+
return yargs
14+
.option('component', {
15+
describe: 'print component version',
16+
alias: 'c',
17+
choices: ['client', 'api', 'hermes', 'nomios'],
18+
default: 'client',
19+
});
1320
},
14-
handler: async () => {
15-
console.log(cliPackage.version);
21+
handler: async (argv) => {
22+
let ver = '';
23+
switch (argv.component) {
24+
case 'api':
25+
ver = await version.getServerVersion();
26+
ver = ver.current_commit;
27+
break;
28+
case 'hermes':
29+
ver = await version.getHermesVersion();
30+
break;
31+
case 'nomios':
32+
ver = await version.getNomiosVersion();
33+
break;
34+
case 'client':
35+
default:
36+
ver = cliPackage.version;
37+
}
38+
console.log(argv.component, 'version:', ver);
1639
},
1740
});
1841

19-
module.exports = version;
42+
module.exports = versionCmd;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const debug = require('debug')('codefresh:cli:create:trigger');
2+
const Command = require('../../Command');
3+
const { trigger } = require('../../../../logic').api;
4+
const createRoot = require('../root/create.cmd');
5+
6+
const command = new Command({
7+
command: 'trigger <event-uri> <pipeline> [pipelines...]',
8+
aliases: ['t'],
9+
parent: createRoot,
10+
cliDocs: {
11+
description: 'Set pipeline(s) trigger: connect the `trigger-event` to the pipeline(s). *Note:* `trigger-event` exists only if there is at least one connected pipeline.',
12+
},
13+
webDocs: {
14+
category: 'Triggers',
15+
title: 'Set Pipeline Trigger',
16+
},
17+
builder: (yargs) => {
18+
return yargs
19+
.positional('event-uri', {
20+
describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)',
21+
require: true,
22+
})
23+
.positional('pipeline', {
24+
describe: 'pipeline(s) to be triggered by the specified `trigger-event`',
25+
require: true,
26+
});
27+
},
28+
handler: async (argv) => {
29+
/* eslint-disable prefer-destructuring */
30+
const pipelines = [].concat(argv.pipeline).concat(argv.pipelines);
31+
const eventURI = argv['event-uri'];
32+
/* eslint-enable prefer-destructuring */
33+
await trigger.addPipelineTrigger(eventURI, pipelines);
34+
console.log(`Trigger : ${eventURI} was successfully added to the pipeline(s): ${pipelines}`);
35+
},
36+
});
37+
38+
module.exports = command;
39+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const debug = require('debug')('codefresh:cli:delete:trigger');
2+
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const _ = require('lodash');
5+
const { trigger } = require('../../../../logic').api;
6+
const deleteRoot = require('../root/delete.cmd');
7+
8+
const command = new Command({
9+
command: 'trigger <event-uri> <pipeline>',
10+
aliases: ['t'],
11+
parent: deleteRoot,
12+
cliDocs: {
13+
description: 'Remove pipeline trigger. *Note:* this may also remove `trigger-event` definition, if there are no pipelines left, that are connected to it.',
14+
},
15+
webDocs: {
16+
category: 'Triggers',
17+
title: 'Remove Pipeline Trigger',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.positional('event-uri', {
22+
describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)',
23+
required: true,
24+
})
25+
.positional('pipeline', {
26+
describe: 'pipeline ID',
27+
required: true,
28+
});
29+
},
30+
handler: async (argv) => {
31+
/* eslint-disable prefer-destructuring */
32+
const eventURI = argv['event-uri'];
33+
const pipeline = argv.pipeline;
34+
/* eslint-enable prefer-destructuring */
35+
36+
await trigger.deletePipelineTrigger(eventURI, pipeline);
37+
console.log(`Trigger: ${eventURI} has been removed from ${pipeline} pipeline`);
38+
},
39+
});
40+
41+
module.exports = command;
42+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const debug = require('debug')('codefresh:cli:get:trigger-event');
2+
const Command = require('../../../Command');
3+
const _ = require('lodash');
4+
const { trigger } = require('../../../../../logic').api;
5+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../../helpers/get');
6+
const getRoot = require('../../root/get.cmd');
7+
8+
const command = new Command({
9+
command: 'trigger-event <event-uri>',
10+
aliases: ['te', 'event'],
11+
parent: getRoot,
12+
cliDocs: {
13+
description: 'Get detailed information about specified `trigger-event`',
14+
},
15+
webDocs: {
16+
category: 'Triggers',
17+
title: 'Get Trigger Event',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.positional('event-uri', {
22+
describe: '`trigger-event` uri (as defined by trigger `type[/kind]`)',
23+
require: true,
24+
});
25+
},
26+
handler: async (argv) => {
27+
/* eslint-disable prefer-destructuring */
28+
const eventURI = argv['event-uri'];
29+
/* eslint-enable prefer-destructuring */
30+
31+
const info = await trigger.getEventInfo(eventURI);
32+
33+
if (_.isArray(info)) {
34+
specifyOutputForArray(argv.output, info);
35+
} else {
36+
specifyOutputForSingle(argv.output, info);
37+
}
38+
},
39+
});
40+
41+
module.exports = command;
42+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const debug = require('debug')('codefresh:cli:get:triggers');
2+
const Command = require('../../Command');
3+
const _ = require('lodash');
4+
const { trigger } = require('../../../../logic').api;
5+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
6+
const getRoot = require('../root/get.cmd');
7+
8+
const command = new Command({
9+
command: 'triggers <pipeline>',
10+
aliases: ['t'],
11+
parent: getRoot,
12+
cliDocs: {
13+
description: 'Get pipeline triggers',
14+
},
15+
webDocs: {
16+
category: 'Triggers',
17+
title: 'Get Pipeline Triggers',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.positional('pipeline', {
22+
describe: 'pipeline id',
23+
require: true,
24+
});
25+
},
26+
handler: async (argv) => {
27+
/* eslint-disable prefer-destructuring */
28+
const pipeline = argv.pipeline;
29+
/* eslint-enable prefer-destructuring */
30+
31+
const triggers = await trigger.getPipelineTriggers(pipeline);
32+
33+
if (_.isArray(triggers)) {
34+
specifyOutputForArray(argv.output, triggers);
35+
} else {
36+
specifyOutputForSingle(argv.output, triggers);
37+
}
38+
},
39+
});
40+
41+
module.exports = command;
42+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const debug = require('debug')('codefresh:cli:get:trigger-types');
2+
const Command = require('../../../Command');
3+
const _ = require('lodash');
4+
const { trigger } = require('../../../../../logic').api;
5+
const { specifyOutputForSingle, specifyOutputForArray } = require('../../../helpers/get');
6+
const getRoot = require('../../root/get.cmd');
7+
8+
const command = new Command({
9+
command: 'trigger-types [type] [kind]',
10+
aliases: ['tt'],
11+
parent: getRoot,
12+
cliDocs: {
13+
description: 'Get a list of system-wide available `trigger-types` or specified `trigger-type`',
14+
},
15+
webDocs: {
16+
category: 'Triggers',
17+
title: 'Get Trigger Types',
18+
},
19+
builder: (yargs) => {
20+
return yargs
21+
.positional('type', {
22+
describe: '`trigger-type` type name (e.g. `registry`, `cron`)',
23+
})
24+
.positional('kind', {
25+
describe: '`trigger-type` kind (e.g. `dockerhub`, `cfcr`, `gcr`, `acr`); only some `trigger-types` may have kinds',
26+
});
27+
},
28+
handler: async (argv) => {
29+
/* eslint-disable prefer-destructuring */
30+
const type = argv.type;
31+
const kind = argv.kind;
32+
/* eslint-enable prefer-destructuring */
33+
34+
let types;
35+
if (type && kind) {
36+
types = await trigger.getType(type, kind);
37+
} else {
38+
types = await trigger.getAllTypes();
39+
}
40+
41+
if (_.isArray(types)) {
42+
specifyOutputForArray(argv.output, types);
43+
} else {
44+
specifyOutputForSingle(argv.output, types);
45+
}
46+
},
47+
});
48+
49+
module.exports = command;
50+

lib/logic/api/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const user = require('./user');
2+
const trigger = require('./trigger');
23
const pipeline = require('./pipeline');
34
const pipeline2 = require('./pipeline2');
45
const context = require('./context');
@@ -7,9 +8,11 @@ const composition = require('./composition');
78
const environment = require('./environment');
89
const workflow = require('./workflow');
910
const log = require('./log');
11+
const version = require('./version');
1012

1113
module.exports = {
1214
user,
15+
trigger,
1316
pipeline,
1417
pipeline2,
1518
context,
@@ -18,4 +21,5 @@ module.exports = {
1821
environment,
1922
workflow,
2023
log,
24+
version,
2125
};

0 commit comments

Comments
 (0)