Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/interface/cli/commands/trigger/event/create.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.`);
},
});
Expand Down
8 changes: 6 additions & 2 deletions lib/interface/cli/commands/trigger/event/get.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -31,14 +31,18 @@ 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`');
},
handler: async (argv) => {
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']);
}
Expand Down
22 changes: 16 additions & 6 deletions lib/interface/cli/commands/trigger/get.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,37 @@ const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers
const getRoot = require('../root/get.cmd');

const command = new Command({
command: 'triggers <pipeline>',
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);
Expand Down
16 changes: 8 additions & 8 deletions lib/interface/cli/commands/trigger/link.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ const { trigger } = require('../../../../logic').api;

const command = new Command({
root: true,
command: 'link <event-uri> <pipeline> [pipelines...]',
description: 'Define new trigger(s): link pipeline(s) to the specified `trigger-event`',
command: 'link <event-uri> <pipeline>',
description: 'Create trigger: link pipeline to `trigger-event`',
webDocs: {
category: 'Triggers',
title: 'Define Pipeline Trigger',
},
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}`);
},
});

Expand Down
12 changes: 6 additions & 6 deletions lib/interface/cli/commands/trigger/unlink.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { trigger } = require('../../../../logic').api;

const command = new Command({
root: true,
command: 'unlink <event-uri> <pipeline> [pipelines...]',
description: 'Undefine trigger(s): unlink pipeline(s) from the specified `trigger-event`',
command: 'unlink <event-uri> <pipeline>',
description: 'Delete trigger: unlink pipeline from `trigger-event`',
webDocs: {
category: 'Triggers',
title: 'Remove Pipeline Trigger',
Expand All @@ -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}`);
},
});

Expand Down
64 changes: 49 additions & 15 deletions lib/logic/api/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}`,
Expand All @@ -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);
Expand All @@ -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',
};

Expand All @@ -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,
Expand All @@ -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',
};

Expand All @@ -157,9 +189,11 @@ module.exports = {
getType,
getAllTypes,
// trigger methods
getTriggers,
getPipelineTriggers,
linkPipelinesToEvent,
unlinkPipelinesFromEvent,
getEventTriggers,
createTrigger,
deleteTrigger,
// trigger event methods
getEvent,
getEvents,
Expand Down
2 changes: 1 addition & 1 deletion lib/logic/entities/TriggerEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/logic/entities/TriggerType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codefresh",
"version": "0.8.24",
"version": "0.8.25",
"description": "Codefresh command line utility",
"main": "index.js",
"preferGlobal": true,
Expand Down