diff --git a/lib/interface/cli/commands/trigger/delete.cmd.js b/lib/interface/cli/commands/trigger/delete.cmd.js deleted file mode 100644 index 1c14fbcab..000000000 --- a/lib/interface/cli/commands/trigger/delete.cmd.js +++ /dev/null @@ -1,40 +0,0 @@ -const debug = require('debug')('codefresh:cli:delete:trigger'); -const Command = require('../../Command'); -const CFError = require('cf-errors'); -const _ = require('lodash'); -const { trigger } = require('../../../../logic').api; -const deleteRoot = require('../root/delete.cmd'); - -const command = new Command({ - command: 'trigger ', - aliases: ['t'], - parent: deleteRoot, - description: 'Remove pipeline trigger. *Note:* this may also remove `trigger-event` definition, if there are no pipelines left, that are connected to it.', - webDocs: { - category: 'Triggers', - title: 'Remove Pipeline Trigger', - }, - builder: (yargs) => { - return yargs - .positional('event-uri', { - describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)', - required: true, - }) - .positional('pipeline', { - describe: 'pipeline ID', - required: true, - }); - }, - handler: async (argv) => { - /* eslint-disable prefer-destructuring */ - const eventURI = argv['event-uri']; - const pipeline = argv.pipeline; - /* eslint-enable prefer-destructuring */ - - await trigger.deletePipelineTrigger(eventURI, pipeline); - console.log(`Trigger: ${eventURI} has been removed from ${pipeline} pipeline`); - }, -}); - -module.exports = command; - diff --git a/lib/interface/cli/commands/trigger/event/create.cmd.js b/lib/interface/cli/commands/trigger/event/create.cmd.js new file mode 100644 index 000000000..d84f4625e --- /dev/null +++ b/lib/interface/cli/commands/trigger/event/create.cmd.js @@ -0,0 +1,47 @@ +require('debug')('codefresh:cli:create:trigger-event'); + +const Command = require('../../../Command'); +const { trigger } = require('../../../../../logic').api; +const { prepareKeyValueFromCLIEnvOption } = require('../../../helpers/general'); +const createRoot = require('../../root/create.cmd'); + +const command = new Command({ + command: 'trigger-event', + parent: createRoot, + description: 'Create new `trigger-event`', + webDocs: { + category: 'Triggers', + title: 'Create Trigger Event', + }, + builder: (yargs) => { + yargs + .option('type', { + describe: 'trigger-event type', + require: true, + }) + .option('kind', { + describe: 'trigger-event kind', + }) + .option('secret', { + describe: 'trigger-event secret (omit to auto-generate)', + require: true, + default: '!generate', + }) + .option('value', { + describe: 'trigger-event specific values pairs (key=value), as required by trigger-type', + default: [], + }) + .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'); + }, + handler: async (argv) => { + const values = prepareKeyValueFromCLIEnvOption(argv.value); + const event = await trigger.createEvent(argv.type, argv.kind, argv.secret, values, argv.context); + console.log(`Trigger event: ${event.uri} was successfully created.`); + }, +}); + +module.exports = command; + diff --git a/lib/interface/cli/commands/trigger/event/delete.cmd.js b/lib/interface/cli/commands/trigger/event/delete.cmd.js new file mode 100644 index 000000000..9fa239620 --- /dev/null +++ b/lib/interface/cli/commands/trigger/event/delete.cmd.js @@ -0,0 +1,32 @@ +require('debug')('codefresh:cli:delete:trigger-event'); + +const Command = require('../../../Command'); +const { trigger } = require('../../../../../logic').api; +const deleteRoot = require('../../root/delete.cmd'); + +const command = new Command({ + command: 'trigger-event [event-uri]', + parent: deleteRoot, + description: 'Delete `trigger-event`', + webDocs: { + category: 'Triggers', + title: 'Delete Trigger Event', + }, + builder: (yargs) => { + yargs + .positional('event-uri', { + describe: 'trigger-event URI', + }) + .option('context', { + describe: 'context with credentials required to setup event on remote system', + }) + .example('codefresh delete trigger-event --context dockerhub registry:dockerhub:codefresh:fortune:push', 'Delete registry/dockerhub trigger-event'); + }, + handler: async (argv) => { + await trigger.deleteEvent(argv['event-uri'], argv.context); + console.log(`Trigger event: ${argv['event-uri']} was successfully deleted.`); + }, +}); + +module.exports = command; + diff --git a/lib/interface/cli/commands/trigger/event/get.cmd.js b/lib/interface/cli/commands/trigger/event/get.cmd.js index 5b9e396f8..c431800c7 100644 --- a/lib/interface/cli/commands/trigger/event/get.cmd.js +++ b/lib/interface/cli/commands/trigger/event/get.cmd.js @@ -1,4 +1,5 @@ -const debug = require('debug')('codefresh:cli:get:trigger-event'); +require('debug')('codefresh:cli:get:trigger-event'); + const Command = require('../../../Command'); const _ = require('lodash'); const { trigger } = require('../../../../../logic').api; @@ -7,31 +8,27 @@ const getRoot = require('../../root/get.cmd'); const command = new Command({ command: 'trigger-event ', - aliases: ['te', 'event'], parent: getRoot, - description: 'Get detailed information about specified `trigger-event`', + description: 'Get `trigger-event`', webDocs: { category: 'Triggers', title: 'Get Trigger Event', }, builder: (yargs) => { - return yargs + yargs .positional('event-uri', { - describe: '`trigger-event` uri (as defined by trigger `type[/kind]`)', + describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)', require: true, - }); + }) + .example('codefresh get trigger-event registry:dockerhub:codefresh:fortune:push', 'Get DockerHub codefresh/fortune push `trigger-event`'); }, handler: async (argv) => { - /* eslint-disable prefer-destructuring */ - const eventURI = argv['event-uri']; - /* eslint-enable prefer-destructuring */ - - const info = await trigger.getEventInfo(eventURI); + const event = await trigger.getEvent(argv['event-uri']); - if (_.isArray(info)) { - specifyOutputForArray(argv.output, info); + if (_.isArray(event)) { + specifyOutputForArray(argv.output, event); } else { - specifyOutputForSingle(argv.output, info); + specifyOutputForSingle(argv.output, event); } }, }); diff --git a/lib/interface/cli/commands/trigger/get.cmd.js b/lib/interface/cli/commands/trigger/get.cmd.js index f555740e8..b687ba96f 100644 --- a/lib/interface/cli/commands/trigger/get.cmd.js +++ b/lib/interface/cli/commands/trigger/get.cmd.js @@ -1,4 +1,4 @@ -const debug = require('debug')('codefresh:cli:get:triggers'); +require('debug')('codefresh:cli:get:triggers'); const Command = require('../../Command'); const _ = require('lodash'); const { trigger } = require('../../../../logic').api; @@ -15,7 +15,7 @@ const command = new Command({ title: 'Get Pipeline Triggers', }, builder: (yargs) => { - return yargs + yargs .positional('pipeline', { describe: 'pipeline id', require: true, diff --git a/lib/interface/cli/commands/trigger/create.cmd.js b/lib/interface/cli/commands/trigger/link.cmd.js similarity index 53% rename from lib/interface/cli/commands/trigger/create.cmd.js rename to lib/interface/cli/commands/trigger/link.cmd.js index 2ceac5a70..3f0cd85b0 100644 --- a/lib/interface/cli/commands/trigger/create.cmd.js +++ b/lib/interface/cli/commands/trigger/link.cmd.js @@ -1,19 +1,18 @@ -const debug = require('debug')('codefresh:cli:create:trigger'); +require('debug')('codefresh:cli:create:trigger'); + const Command = require('../../Command'); const { trigger } = require('../../../../logic').api; -const createRoot = require('../root/create.cmd'); const command = new Command({ - command: 'trigger [pipelines...]', - aliases: ['t'], - parent: createRoot, - 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.', + root: true, + command: 'link [pipelines...]', + description: 'Define new trigger(s): link pipeline(s) to the specified `trigger-event`', webDocs: { category: 'Triggers', - title: 'Set Pipeline Trigger', + title: 'Define Pipeline Trigger', }, builder: (yargs) => { - return yargs + yargs .positional('event-uri', { describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)', require: true, @@ -21,15 +20,16 @@ const command = new Command({ .positional('pipeline', { describe: 'pipeline(s) to be triggered by the specified `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 eventURI = argv['event-uri']; + const event = argv['event-uri']; /* eslint-enable prefer-destructuring */ - await trigger.addPipelineTrigger(eventURI, pipelines); - console.log(`Trigger : ${eventURI} was successfully added to the pipeline(s): ${pipelines}`); + await trigger.linkPipelinesToEvent(event, pipelines); + console.log(`Trigger: ${event} was successfully linked to the pipeline(s): ${pipelines}`); }, }); diff --git a/lib/interface/cli/commands/trigger/type/get.cmd.js b/lib/interface/cli/commands/trigger/type/get.cmd.js index 23e1e2005..5b053298e 100644 --- a/lib/interface/cli/commands/trigger/type/get.cmd.js +++ b/lib/interface/cli/commands/trigger/type/get.cmd.js @@ -1,4 +1,5 @@ -const debug = require('debug')('codefresh:cli:get:trigger-types'); +require('debug')('codefresh:cli:get:trigger-types'); + const Command = require('../../../Command'); const _ = require('lodash'); const { trigger } = require('../../../../../logic').api; @@ -7,7 +8,6 @@ const getRoot = require('../../root/get.cmd'); const command = new Command({ command: 'trigger-types [type] [kind]', - aliases: ['tt'], parent: getRoot, description: 'Get a list of system-wide available `trigger-types` or specified `trigger-type`', webDocs: { @@ -15,13 +15,14 @@ const command = new Command({ title: 'Get Trigger Types', }, builder: (yargs) => { - return yargs + yargs .positional('type', { describe: '`trigger-type` type name (e.g. `registry`, `cron`)', }) .positional('kind', { describe: '`trigger-type` kind (e.g. `dockerhub`, `cfcr`, `gcr`, `acr`); only some `trigger-types` may have kinds', - }); + }) + .example('codefresh get trigger-types --type registry', 'Get Docker registry trigger types'); }, handler: async (argv) => { /* eslint-disable prefer-destructuring */ diff --git a/lib/interface/cli/commands/trigger/unlink.cmd.js b/lib/interface/cli/commands/trigger/unlink.cmd.js new file mode 100644 index 000000000..6c4cf64f8 --- /dev/null +++ b/lib/interface/cli/commands/trigger/unlink.cmd.js @@ -0,0 +1,38 @@ +require('debug')('codefresh:cli:delete:trigger'); + +const Command = require('../../Command'); +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`', + webDocs: { + category: 'Triggers', + title: 'Remove Pipeline Trigger', + }, + builder: (yargs) => { + yargs + .positional('event-uri', { + describe: '`trigger-event` URI (as defined by trigger `type[/kind]`)', + required: true, + }) + .positional('pipeline', { + describe: 'pipeline ID', + required: true, + }) + .example('codefresh unlink registry:dockerhub:codefresh:fortune:push 5a439664af73ad0001f3ece0', 'Remove trigger by unlinking 5a43... pipeline from the DockerHub `codefresh/fortune` `push` event'); + }, + handler: async (argv) => { + /* eslint-disable prefer-destructuring */ + const event = argv['event-uri']; + const pipelines = [].concat(argv.pipeline).concat(argv.pipelines); + /* eslint-enable prefer-destructuring */ + + await trigger.unlinkPipelinesFromEvent(event, pipelines); + console.log(`Trigger: ${eventURI} was unlinked from the pipeline(s): ${pipelines}`); + }, +}); + +module.exports = command; + diff --git a/lib/logic/api/trigger.js b/lib/logic/api/trigger.js index 66e0a85eb..a91d7bd88 100644 --- a/lib/logic/api/trigger.js +++ b/lib/logic/api/trigger.js @@ -12,8 +12,11 @@ const _extractTriggerTypeEntity = triggerType => ({ 'uri-regex': triggerType['uri-regex'], }); -const _extractTriggerEventEntity = (triggerEvent, eventURI) => ({ - uri: eventURI, +const _extractTriggerEventEntity = triggerEvent => ({ + uri: triggerEvent.uri, + type: triggerEvent.type, + kind: triggerEvent.kind, + secret: triggerEvent.secret, status: triggerEvent.status, endpoint: triggerEvent.endpoint, description: triggerEvent.description, @@ -21,12 +24,12 @@ const _extractTriggerEventEntity = (triggerEvent, eventURI) => ({ }); const _extractTriggerEntity = trigger => ({ - uri: trigger.event, - secret: trigger.secret, - count: trigger.pipelines.length, - pipelines: trigger.pipelines, + event: trigger.event, + pipeline: trigger.pipeline, }); +// TRIGGER TYPES + const getAllTypes = async () => { const RequestOptions = { url: '/api/hermes/events/types', @@ -45,7 +48,6 @@ const getAllTypes = async () => { return types; }; - const getType = async (type, kind) => { const options = { url: `/api/hermes/events/types/${type}/${kind}`, @@ -57,20 +59,11 @@ const getType = async (type, kind) => { return new TriggerType(data); }; -const getEventInfo = async (uri) => { - const options = { - url: `/api/hermes/events/info/${uri}`, - method: 'GET', - }; - - const eventInfo = await sendHttpRequest(options); - const data = _extractTriggerEventEntity(eventInfo, uri); - return new TriggerEvent(data); -}; +// TRIGGERS const getPipelineTriggers = async (pipeline) => { const options = { - url: `/api/hermes/triggers/${pipeline}`, + url: `/api/hermes/triggers/pipeline/${pipeline}`, method: 'GET', }; @@ -85,9 +78,9 @@ const getPipelineTriggers = async (pipeline) => { return triggers; }; -const addPipelineTrigger = async (eventURI, pipelines) => { +const linkPipelinesToEvent = async (event, pipelines) => { const options = { - url: `/api/hermes/triggers/${eventURI}/pipelines`, + url: `/api/hermes/events/trigger/${event}`, method: 'POST', body: pipelines, json: true, @@ -96,9 +89,63 @@ const addPipelineTrigger = async (eventURI, pipelines) => { return sendHttpRequest(options); }; -const deletePipelineTrigger = async (eventURI, pipeline) => { +const unlinkPipelinesFromEvent = async (event, pipelines) => { + const options = { + url: `api/hermes/events/trigger/${event}`, + method: 'DELETE', + body: pipelines, + json: true, + }; + + return sendHttpRequest(options); +}; + +// TRIGGER EVENTS + +const getEvent = async (event) => { + const options = { + url: `/api/hermes/events/${event}`, + method: 'GET', + }; + + const triggerEvent = await sendHttpRequest(options); + const data = _extractTriggerEventEntity(triggerEvent); + return new TriggerEvent(data); +}; + +const getEvents = async (type, kind, filter) => { + const options = { + url: `/api/hermes/events/?type=${type}&kind=${kind}&filter=${filter}`, + method: 'GET', + }; + + const result = await sendHttpRequest(options); + const triggerEvents = []; + + _.forEach(result, (triggerEvent) => { + const data = _extractTriggerEventEntity(triggerEvent); + triggerEvents.push(new Trigger(data)); + }); + + return triggerEvents; +}; + +const createEvent = async (type, kind, secret, values, context) => { + const options = { + url: '/api/hermes/events', + method: 'POST', + body: { + type, kind, secret, values, context, + }, + json: true, + }; + + return sendHttpRequest(options); +}; + +const deleteEvent = async (event, context) => { const options = { - url: `api/hermes/triggers/${eventURI}/pipelines/${pipeline}`, + url: `/api/hermes/events/event/${event}/${context}`, method: 'DELETE', }; @@ -106,10 +153,16 @@ const deletePipelineTrigger = async (eventURI, pipeline) => { }; module.exports = { + // trigger type methods getType, getAllTypes, - getEventInfo, + // trigger methods getPipelineTriggers, - addPipelineTrigger, - deletePipelineTrigger, + linkPipelinesToEvent, + unlinkPipelinesFromEvent, + // trigger event methods + getEvent, + getEvents, + createEvent, + deleteEvent, }; diff --git a/lib/logic/entities/Trigger.js b/lib/logic/entities/Trigger.js index 91474e789..10b084033 100644 --- a/lib/logic/entities/Trigger.js +++ b/lib/logic/entities/Trigger.js @@ -5,7 +5,7 @@ class Trigger extends Entity { super(); this.entityType = 'trigger'; this.info = data; - this.defaultColumns = ['uri', 'secret', 'count']; + this.defaultColumns = ['event', 'pipeline']; this.wideColumns = this.defaultColumns.concat([]); } } diff --git a/lib/logic/entities/TriggerEvent.js b/lib/logic/entities/TriggerEvent.js index f22aab36a..9313cc297 100644 --- a/lib/logic/entities/TriggerEvent.js +++ b/lib/logic/entities/TriggerEvent.js @@ -5,8 +5,8 @@ class TriggerEvent extends Entity { super(); this.entityType = 'trigger-event'; this.info = data; - this.defaultColumns = ['uri', 'status', 'endpoint']; - this.wideColumns = this.defaultColumns.concat(['description']); + this.defaultColumns = ['uri', 'type', 'kind', 'status']; + this.wideColumns = this.defaultColumns.concat(['endpoint', 'description']); } } diff --git a/package.json b/package.json index b37a0f477..8ee857eea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.8.11", + "version": "0.8.14", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true,