Skip to content

Commit 17b7550

Browse files
authored
support public trigger-event; by default all events are private (#159)
* support public trigger-event; by default all events are private (account specific) * update cli to work with trigger-events * update cli to work with trigger-events * aligning cli with private triggers
1 parent 3b471c0 commit 17b7550

File tree

9 files changed

+96
-42
lines changed

9 files changed

+96
-42
lines changed

lib/interface/cli/commands/trigger/event/create.cmd.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const command = new Command({
2222
.option('kind', {
2323
describe: 'trigger-event kind',
2424
})
25+
.option('public', {
26+
describe: 'wether trigger-event is public (system-wide): can be linked to any pipeline in any account',
27+
default: false,
28+
})
2529
.option('secret', {
2630
describe: 'trigger-event secret (omit to auto-generate)',
2731
require: true,
@@ -34,11 +38,13 @@ const command = new Command({
3438
.option('context', {
3539
describe: 'context with credentials required to setup event on remote system',
3640
})
37-
.example('codefresh create trigger-event --type registry --kind dockerhub --secret XYZ1234 --value namespace=codefresh --value name=fortune --context dockerhub', 'Create registry/dockerhub trigger-event');
41+
.example('codefresh create trigger-event --type registry --kind dockerhub --secret XYZ1234 --value namespace=codefresh --value name=fortune --context dockerhub', 'Create registry/dockerhub trigger-event')
42+
.example('codefresh create trigger-event --type cron --kind codefresh --secret XYZ1234 --value expression="0 0 */1 * * *" --value message=hello', 'Create cron (once in hour) trigger-event')
43+
.example('codefresh create trigger-event --type cron --kind codefresh --secret XYZ1234 --value expression="@daily" --value message=hello-all', 'Create public daily cron trigger-event');
3844
},
3945
handler: async (argv) => {
4046
const values = prepareKeyValueFromCLIEnvOption(argv.value);
41-
const uri = await trigger.createEvent(argv.type, argv.kind, argv.secret, values, argv.context);
47+
const uri = await trigger.createEvent(argv.type, argv.kind, argv.secret, values, argv.context, argv.public);
4248
console.log(`Trigger event: ${uri} was successfully created.`);
4349
},
4450
});

lib/interface/cli/commands/trigger/event/get.cmd.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const command = new Command({
1717
builder: (yargs) => {
1818
yargs
1919
.positional('event-uri', {
20-
describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)',
20+
describe: 'trigger-event URI)',
2121
})
2222
.option('type', {
2323
describe: 'trigger-event type',
@@ -31,14 +31,18 @@ const command = new Command({
3131
describe: 'trigger-event URI filter (regex)',
3232
default: '',
3333
})
34+
.option('public', {
35+
describe: 'get public trigger-event(s)',
36+
default: true,
37+
})
3438
.example('codefresh get trigger-event registry:dockerhub:codefresh:fortune:push', 'Get DockerHub codefresh/fortune push `trigger-event`')
3539
.example('codefresh get trigger-event --type registry --kind dockerhub --filter *codefresh', 'Get all DockerHub codefresh/* push `trigger-events`');
3640
},
3741
handler: async (argv) => {
3842
const uri = argv['event-uri'];
3943
let events;
4044
if (typeof uri === 'undefined') {
41-
events = await trigger.getEvents(argv.type, argv.kind, argv.filter);
45+
events = await trigger.getEvents(argv.type, argv.kind, argv.filter, argv.public);
4246
} else {
4347
events = await trigger.getEvent(argv['event-uri']);
4448
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,37 @@ const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers
66
const getRoot = require('../root/get.cmd');
77

88
const command = new Command({
9-
command: 'triggers <pipeline>',
9+
command: 'triggers',
1010
aliases: ['t'],
1111
parent: getRoot,
12-
description: 'Get pipeline triggers',
12+
description: 'Get triggers, optionally filtered by pipeline or event',
1313
webDocs: {
1414
category: 'Triggers',
15-
title: 'Get Pipeline Triggers',
15+
title: 'Get Triggers',
1616
},
1717
builder: (yargs) => {
1818
yargs
19-
.positional('pipeline', {
19+
.optional('pipeline', {
2020
describe: 'pipeline id',
21-
require: true,
21+
})
22+
.optional('event-uri', {
23+
describe: 'event URI',
2224
});
2325
},
2426
handler: async (argv) => {
2527
/* eslint-disable prefer-destructuring */
2628
const pipeline = argv.pipeline;
29+
const eventURI = argv['event-uri'];
2730
/* eslint-enable prefer-destructuring */
2831

29-
const triggers = await trigger.getPipelineTriggers(pipeline);
32+
let triggers;
33+
if (pipeline) {
34+
triggers = await trigger.getPipelineTriggers(pipeline);
35+
} else if (eventURI) {
36+
triggers = await trigger.getEventTriggers(eventURI);
37+
} else {
38+
triggers = await trigger.getTriggers();
39+
}
3040

3141
if (_.isArray(triggers)) {
3242
specifyOutputForArray(argv.output, triggers);

lib/interface/cli/commands/trigger/link.cmd.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@ const { trigger } = require('../../../../logic').api;
55

66
const command = new Command({
77
root: true,
8-
command: 'link <event-uri> <pipeline> [pipelines...]',
9-
description: 'Define new trigger(s): link pipeline(s) to the specified `trigger-event`',
8+
command: 'link <event-uri> <pipeline>',
9+
description: 'Create trigger: link pipeline to `trigger-event`',
1010
webDocs: {
1111
category: 'Triggers',
1212
title: 'Define Pipeline Trigger',
1313
},
1414
builder: (yargs) => {
1515
yargs
1616
.positional('event-uri', {
17-
describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)',
17+
describe: '`trigger-event` URI',
1818
require: true,
1919
})
2020
.positional('pipeline', {
21-
describe: 'pipeline(s) to be triggered by the specified `trigger-event`',
21+
describe: 'pipeline to be triggered by the `trigger-event`',
2222
require: true,
2323
})
2424
.example('codefresh link registry:dockerhub:codefresh:fortune:push 5a439664af73ad0001f3ece0', 'Setup trigger by linking 5a43... pipeline to the DockerHub `codefresh/fortune` `push` event');
2525
},
2626
handler: async (argv) => {
2727
/* eslint-disable prefer-destructuring */
28-
const pipelines = [].concat(argv.pipeline).concat(argv.pipelines);
29-
const event = argv['event-uri'];
28+
const pipeline = argv.pipeline;
29+
const eventURI = argv['event-uri'];
3030
/* eslint-enable prefer-destructuring */
31-
await trigger.linkPipelinesToEvent(event, pipelines);
32-
console.log(`Trigger: ${event} was successfully linked to the pipeline(s): ${pipelines}`);
31+
await trigger.createTrigger(eventURI, pipeline);
32+
console.log(`Trigger: ${eventURI} was successfully linked to the pipeline(s): ${pipeline}`);
3333
},
3434
});
3535

lib/interface/cli/commands/trigger/unlink.cmd.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const { trigger } = require('../../../../logic').api;
55

66
const command = new Command({
77
root: true,
8-
command: 'unlink <event-uri> <pipeline> [pipelines...]',
9-
description: 'Undefine trigger(s): unlink pipeline(s) from the specified `trigger-event`',
8+
command: 'unlink <event-uri> <pipeline>',
9+
description: 'Delete trigger: unlink pipeline from `trigger-event`',
1010
webDocs: {
1111
category: 'Triggers',
1212
title: 'Remove Pipeline Trigger',
@@ -25,12 +25,12 @@ const command = new Command({
2525
},
2626
handler: async (argv) => {
2727
/* eslint-disable prefer-destructuring */
28-
const event = argv['event-uri'];
29-
const pipelines = [].concat(argv.pipeline).concat(argv.pipelines);
28+
const eventURI = argv['event-uri'];
29+
const pipeline = argv.pipeline;
3030
/* eslint-enable prefer-destructuring */
3131

32-
await trigger.unlinkPipelinesFromEvent(event, pipelines);
33-
console.log(`Trigger: ${eventURI} was unlinked from the pipeline(s): ${pipelines}`);
32+
await trigger.deleteTrigger(eventURI, pipeline);
33+
console.log(`Trigger: ${eventURI} was unlinked from the pipeline: ${pipeline}`);
3434
},
3535
});
3636

lib/logic/api/trigger.js

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const _extractTriggerEventEntity = triggerEvent => ({
1616
uri: triggerEvent.uri,
1717
type: triggerEvent.type,
1818
kind: triggerEvent.kind,
19+
// [0]{12} - public account ID
20+
public: triggerEvent.account === '000000000000',
1921
secret: triggerEvent.secret,
2022
status: triggerEvent.status,
2123
endpoint: triggerEvent.endpoint,
@@ -61,6 +63,23 @@ const getType = async (type, kind) => {
6163

6264
// TRIGGERS
6365

66+
const getTriggers = async () => {
67+
const options = {
68+
url: '/api/hermes/triggers/',
69+
method: 'GET',
70+
};
71+
72+
const result = await sendHttpRequest(options);
73+
const triggers = [];
74+
75+
_.forEach(result, (trigger) => {
76+
const data = _extractTriggerEntity(trigger);
77+
triggers.push(new Trigger(data));
78+
});
79+
80+
return triggers;
81+
};
82+
6483
const getPipelineTriggers = async (pipeline) => {
6584
const options = {
6685
url: `/api/hermes/triggers/pipeline/${pipeline}`,
@@ -78,23 +97,36 @@ const getPipelineTriggers = async (pipeline) => {
7897
return triggers;
7998
};
8099

81-
const linkPipelinesToEvent = async (event, pipelines) => {
100+
const getEventTriggers = async (event) => {
101+
const options = {
102+
url: `/api/hermes/triggers/event/${event.replace('/', '_slash_')}`,
103+
method: 'GET',
104+
};
105+
106+
const result = await sendHttpRequest(options);
107+
const triggers = [];
108+
109+
_.forEach(result, (trigger) => {
110+
const data = _extractTriggerEntity(trigger);
111+
triggers.push(new Trigger(data));
112+
});
113+
114+
return triggers;
115+
};
116+
117+
const createTrigger = async (event, pipeline) => {
82118
const options = {
83-
url: `/api/hermes/events/trigger/${event.replace('/', '_slash_')}`,
119+
url: `/api/hermes/triggers/${event.replace('/', '_slash_')}/${pipeline}`,
84120
method: 'POST',
85-
body: pipelines,
86-
json: true,
87121
};
88122

89123
return sendHttpRequest(options);
90124
};
91125

92-
const unlinkPipelinesFromEvent = async (event, pipelines) => {
126+
const deleteTrigger = async (event, pipeline) => {
93127
const options = {
94-
url: `api/hermes/events/trigger/${event.replace('/', '_slash_')}`,
128+
url: `api/hermes/triggers/${event.replace('/', '_slash_')}/${pipeline}`,
95129
method: 'DELETE',
96-
body: pipelines,
97-
json: true,
98130
};
99131

100132
return sendHttpRequest(options);
@@ -113,9 +145,9 @@ const getEvent = async (event) => {
113145
return new TriggerEvent(data);
114146
};
115147

116-
const getEvents = async (type, kind, filter) => {
148+
const getEvents = async (type, kind, filter, pub) => {
117149
const options = {
118-
url: `/api/hermes/events/?type=${type}&kind=${kind}&filter=${filter.replace('/', '_slash_')}`,
150+
url: `/api/hermes/events/?type=${type}&kind=${kind}&filter=${filter.replace('/', '_slash_')}&public=${pub}`,
119151
method: 'GET',
120152
};
121153

@@ -130,9 +162,9 @@ const getEvents = async (type, kind, filter) => {
130162
return triggerEvents;
131163
};
132164

133-
const createEvent = async (type, kind, secret, values, context) => {
165+
const createEvent = async (type, kind, secret, values, context, pub) => {
134166
const options = {
135-
url: '/api/hermes/events',
167+
url: `/api/hermes/events/?public=${pub}`,
136168
method: 'POST',
137169
body: {
138170
type, kind, secret, values, context,
@@ -145,7 +177,7 @@ const createEvent = async (type, kind, secret, values, context) => {
145177

146178
const deleteEvent = async (event, context) => {
147179
const options = {
148-
url: `/api/hermes/events/event/${event.replace('/', '_slash_')}/${context}`,
180+
url: `/api/hermes/events/${event.replace('/', '_slash_')}/${context}`,
149181
method: 'DELETE',
150182
};
151183

@@ -157,9 +189,11 @@ module.exports = {
157189
getType,
158190
getAllTypes,
159191
// trigger methods
192+
getTriggers,
160193
getPipelineTriggers,
161-
linkPipelinesToEvent,
162-
unlinkPipelinesFromEvent,
194+
getEventTriggers,
195+
createTrigger,
196+
deleteTrigger,
163197
// trigger event methods
164198
getEvent,
165199
getEvents,

lib/logic/entities/TriggerEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class TriggerEvent extends Entity {
55
super();
66
this.entityType = 'trigger-event';
77
this.info = data;
8-
this.defaultColumns = ['uri', 'type', 'kind', 'status'];
8+
this.defaultColumns = ['uri', 'type', 'kind', 'public', 'status'];
99
this.wideColumns = this.defaultColumns.concat(['endpoint', 'description']);
1010
}
1111
}

lib/logic/entities/TriggerType.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class TriggerType extends Entity {
55
super();
66
this.entityType = 'trigger-type';
77
this.info = data;
8-
this.defaultColumns = ['type', 'kind', 'uri-template', 'uri-regex'];
8+
this.defaultColumns = ['type', 'kind', 'uri-template'];
99
this.wideColumns = this.defaultColumns.concat([]);
1010
}
1111
}

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.8.24",
3+
"version": "0.8.25",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)