diff --git a/lib/interface/cli/commands/trigger/event/create.cmd.js b/lib/interface/cli/commands/trigger/event/create.cmd.js index 380b5a454..8cd82b8f7 100644 --- a/lib/interface/cli/commands/trigger/event/create.cmd.js +++ b/lib/interface/cli/commands/trigger/event/create.cmd.js @@ -22,6 +22,10 @@ const command = new Command({ .option('kind', { describe: 'trigger-event kind', }) + .option('public', { + describe: 'wether trigger-event is public (system-wide): can be linked to any pipeline in any account', + default: false, + }) .option('secret', { describe: 'trigger-event secret (omit to auto-generate)', require: true, @@ -34,11 +38,13 @@ const command = new Command({ .option('context', { describe: 'context with credentials required to setup event on remote system', }) - .example('codefresh create trigger-event --type registry --kind dockerhub --secret XYZ1234 --value namespace=codefresh --value name=fortune --context dockerhub', 'Create registry/dockerhub trigger-event'); + .example('codefresh create trigger-event --type registry --kind dockerhub --secret XYZ1234 --value namespace=codefresh --value name=fortune --context dockerhub', 'Create registry/dockerhub trigger-event') + .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') + .example('codefresh create trigger-event --type cron --kind codefresh --secret XYZ1234 --value expression="@daily" --value message=hello-all', 'Create public daily cron trigger-event'); }, handler: async (argv) => { const values = prepareKeyValueFromCLIEnvOption(argv.value); - const uri = await trigger.createEvent(argv.type, argv.kind, argv.secret, values, argv.context); + const uri = await trigger.createEvent(argv.type, argv.kind, argv.secret, values, argv.context, argv.public); console.log(`Trigger event: ${uri} was successfully created.`); }, }); diff --git a/lib/interface/cli/commands/trigger/event/get.cmd.js b/lib/interface/cli/commands/trigger/event/get.cmd.js index c7fc62574..35a109d00 100644 --- a/lib/interface/cli/commands/trigger/event/get.cmd.js +++ b/lib/interface/cli/commands/trigger/event/get.cmd.js @@ -17,7 +17,7 @@ const command = new Command({ builder: (yargs) => { yargs .positional('event-uri', { - describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)', + describe: 'trigger-event URI)', }) .option('type', { describe: 'trigger-event type', @@ -31,6 +31,10 @@ const command = new Command({ describe: 'trigger-event URI filter (regex)', default: '', }) + .option('public', { + describe: 'get public trigger-event(s)', + default: true, + }) .example('codefresh get trigger-event registry:dockerhub:codefresh:fortune:push', 'Get DockerHub codefresh/fortune push `trigger-event`') .example('codefresh get trigger-event --type registry --kind dockerhub --filter *codefresh', 'Get all DockerHub codefresh/* push `trigger-events`'); }, @@ -38,7 +42,7 @@ const command = new Command({ const uri = argv['event-uri']; let events; if (typeof uri === 'undefined') { - events = await trigger.getEvents(argv.type, argv.kind, argv.filter); + events = await trigger.getEvents(argv.type, argv.kind, argv.filter, argv.public); } else { events = await trigger.getEvent(argv['event-uri']); } diff --git a/lib/interface/cli/commands/trigger/get.cmd.js b/lib/interface/cli/commands/trigger/get.cmd.js index b687ba96f..514eef640 100644 --- a/lib/interface/cli/commands/trigger/get.cmd.js +++ b/lib/interface/cli/commands/trigger/get.cmd.js @@ -6,27 +6,37 @@ const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers const getRoot = require('../root/get.cmd'); const command = new Command({ - command: 'triggers ', + command: 'triggers', aliases: ['t'], parent: getRoot, - description: 'Get pipeline triggers', + description: 'Get triggers, optionally filtered by pipeline or event', webDocs: { category: 'Triggers', - title: 'Get Pipeline Triggers', + title: 'Get Triggers', }, builder: (yargs) => { yargs - .positional('pipeline', { + .optional('pipeline', { describe: 'pipeline id', - require: true, + }) + .optional('event-uri', { + describe: 'event URI', }); }, handler: async (argv) => { /* eslint-disable prefer-destructuring */ const pipeline = argv.pipeline; + const eventURI = argv['event-uri']; /* eslint-enable prefer-destructuring */ - const triggers = await trigger.getPipelineTriggers(pipeline); + let triggers; + if (pipeline) { + triggers = await trigger.getPipelineTriggers(pipeline); + } else if (eventURI) { + triggers = await trigger.getEventTriggers(eventURI); + } else { + triggers = await trigger.getTriggers(); + } if (_.isArray(triggers)) { specifyOutputForArray(argv.output, triggers); diff --git a/lib/interface/cli/commands/trigger/link.cmd.js b/lib/interface/cli/commands/trigger/link.cmd.js index 3f0cd85b0..600020513 100644 --- a/lib/interface/cli/commands/trigger/link.cmd.js +++ b/lib/interface/cli/commands/trigger/link.cmd.js @@ -5,8 +5,8 @@ const { trigger } = require('../../../../logic').api; const command = new Command({ root: true, - command: 'link [pipelines...]', - description: 'Define new trigger(s): link pipeline(s) to the specified `trigger-event`', + command: 'link ', + description: 'Create trigger: link pipeline to `trigger-event`', webDocs: { category: 'Triggers', title: 'Define Pipeline Trigger', @@ -14,22 +14,22 @@ const command = new Command({ builder: (yargs) => { yargs .positional('event-uri', { - describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)', + describe: '`trigger-event` URI', require: true, }) .positional('pipeline', { - describe: 'pipeline(s) to be triggered by the specified `trigger-event`', + describe: 'pipeline to be triggered by the `trigger-event`', require: true, }) .example('codefresh link registry:dockerhub:codefresh:fortune:push 5a439664af73ad0001f3ece0', 'Setup trigger by linking 5a43... pipeline to the DockerHub `codefresh/fortune` `push` event'); }, handler: async (argv) => { /* eslint-disable prefer-destructuring */ - const pipelines = [].concat(argv.pipeline).concat(argv.pipelines); - const event = argv['event-uri']; + const pipeline = argv.pipeline; + const eventURI = argv['event-uri']; /* eslint-enable prefer-destructuring */ - await trigger.linkPipelinesToEvent(event, pipelines); - console.log(`Trigger: ${event} was successfully linked to the pipeline(s): ${pipelines}`); + await trigger.createTrigger(eventURI, pipeline); + console.log(`Trigger: ${eventURI} was successfully linked to the pipeline(s): ${pipeline}`); }, }); diff --git a/lib/interface/cli/commands/trigger/unlink.cmd.js b/lib/interface/cli/commands/trigger/unlink.cmd.js index 6c4cf64f8..fa7454970 100644 --- a/lib/interface/cli/commands/trigger/unlink.cmd.js +++ b/lib/interface/cli/commands/trigger/unlink.cmd.js @@ -5,8 +5,8 @@ const { trigger } = require('../../../../logic').api; const command = new Command({ root: true, - command: 'unlink [pipelines...]', - description: 'Undefine trigger(s): unlink pipeline(s) from the specified `trigger-event`', + command: 'unlink ', + description: 'Delete trigger: unlink pipeline from `trigger-event`', webDocs: { category: 'Triggers', title: 'Remove Pipeline Trigger', @@ -25,12 +25,12 @@ const command = new Command({ }, handler: async (argv) => { /* eslint-disable prefer-destructuring */ - const event = argv['event-uri']; - const pipelines = [].concat(argv.pipeline).concat(argv.pipelines); + const eventURI = argv['event-uri']; + const pipeline = argv.pipeline; /* eslint-enable prefer-destructuring */ - await trigger.unlinkPipelinesFromEvent(event, pipelines); - console.log(`Trigger: ${eventURI} was unlinked from the pipeline(s): ${pipelines}`); + await trigger.deleteTrigger(eventURI, pipeline); + console.log(`Trigger: ${eventURI} was unlinked from the pipeline: ${pipeline}`); }, }); diff --git a/lib/logic/api/trigger.js b/lib/logic/api/trigger.js index 2b2627698..53f2ce85f 100644 --- a/lib/logic/api/trigger.js +++ b/lib/logic/api/trigger.js @@ -16,6 +16,8 @@ const _extractTriggerEventEntity = triggerEvent => ({ uri: triggerEvent.uri, type: triggerEvent.type, kind: triggerEvent.kind, + // [0]{12} - public account ID + public: triggerEvent.account === '000000000000', secret: triggerEvent.secret, status: triggerEvent.status, endpoint: triggerEvent.endpoint, @@ -61,6 +63,23 @@ const getType = async (type, kind) => { // TRIGGERS +const getTriggers = async () => { + const options = { + url: '/api/hermes/triggers/', + method: 'GET', + }; + + const result = await sendHttpRequest(options); + const triggers = []; + + _.forEach(result, (trigger) => { + const data = _extractTriggerEntity(trigger); + triggers.push(new Trigger(data)); + }); + + return triggers; +}; + const getPipelineTriggers = async (pipeline) => { const options = { url: `/api/hermes/triggers/pipeline/${pipeline}`, @@ -78,23 +97,36 @@ const getPipelineTriggers = async (pipeline) => { return triggers; }; -const linkPipelinesToEvent = async (event, pipelines) => { +const getEventTriggers = async (event) => { + const options = { + url: `/api/hermes/triggers/event/${event.replace('/', '_slash_')}`, + method: 'GET', + }; + + const result = await sendHttpRequest(options); + const triggers = []; + + _.forEach(result, (trigger) => { + const data = _extractTriggerEntity(trigger); + triggers.push(new Trigger(data)); + }); + + return triggers; +}; + +const createTrigger = async (event, pipeline) => { const options = { - url: `/api/hermes/events/trigger/${event.replace('/', '_slash_')}`, + url: `/api/hermes/triggers/${event.replace('/', '_slash_')}/${pipeline}`, method: 'POST', - body: pipelines, - json: true, }; return sendHttpRequest(options); }; -const unlinkPipelinesFromEvent = async (event, pipelines) => { +const deleteTrigger = async (event, pipeline) => { const options = { - url: `api/hermes/events/trigger/${event.replace('/', '_slash_')}`, + url: `api/hermes/triggers/${event.replace('/', '_slash_')}/${pipeline}`, method: 'DELETE', - body: pipelines, - json: true, }; return sendHttpRequest(options); @@ -113,9 +145,9 @@ const getEvent = async (event) => { return new TriggerEvent(data); }; -const getEvents = async (type, kind, filter) => { +const getEvents = async (type, kind, filter, pub) => { const options = { - url: `/api/hermes/events/?type=${type}&kind=${kind}&filter=${filter.replace('/', '_slash_')}`, + url: `/api/hermes/events/?type=${type}&kind=${kind}&filter=${filter.replace('/', '_slash_')}&public=${pub}`, method: 'GET', }; @@ -130,9 +162,9 @@ const getEvents = async (type, kind, filter) => { return triggerEvents; }; -const createEvent = async (type, kind, secret, values, context) => { +const createEvent = async (type, kind, secret, values, context, pub) => { const options = { - url: '/api/hermes/events', + url: `/api/hermes/events/?public=${pub}`, method: 'POST', body: { type, kind, secret, values, context, @@ -145,7 +177,7 @@ const createEvent = async (type, kind, secret, values, context) => { const deleteEvent = async (event, context) => { const options = { - url: `/api/hermes/events/event/${event.replace('/', '_slash_')}/${context}`, + url: `/api/hermes/events/${event.replace('/', '_slash_')}/${context}`, method: 'DELETE', }; @@ -157,9 +189,11 @@ module.exports = { getType, getAllTypes, // trigger methods + getTriggers, getPipelineTriggers, - linkPipelinesToEvent, - unlinkPipelinesFromEvent, + getEventTriggers, + createTrigger, + deleteTrigger, // trigger event methods getEvent, getEvents, diff --git a/lib/logic/entities/TriggerEvent.js b/lib/logic/entities/TriggerEvent.js index 9313cc297..e8e1c1d92 100644 --- a/lib/logic/entities/TriggerEvent.js +++ b/lib/logic/entities/TriggerEvent.js @@ -5,7 +5,7 @@ class TriggerEvent extends Entity { super(); this.entityType = 'trigger-event'; this.info = data; - this.defaultColumns = ['uri', 'type', 'kind', 'status']; + this.defaultColumns = ['uri', 'type', 'kind', 'public', 'status']; this.wideColumns = this.defaultColumns.concat(['endpoint', 'description']); } } diff --git a/lib/logic/entities/TriggerType.js b/lib/logic/entities/TriggerType.js index 66837c4d3..a888cbe6a 100644 --- a/lib/logic/entities/TriggerType.js +++ b/lib/logic/entities/TriggerType.js @@ -5,7 +5,7 @@ class TriggerType extends Entity { super(); this.entityType = 'trigger-type'; this.info = data; - this.defaultColumns = ['type', 'kind', 'uri-template', 'uri-regex']; + this.defaultColumns = ['type', 'kind', 'uri-template']; this.wideColumns = this.defaultColumns.concat([]); } } diff --git a/package.json b/package.json index 73e76e3b1..5db7bc32e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.8.24", + "version": "0.8.25", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true,