From 5c8996d7c3ed4d76d5b7510b1c2a1d59aa09881d Mon Sep 17 00:00:00 2001 From: Rick van der Staaij <6495166+RickvdStaaij@users.noreply.github.com> Date: Thu, 25 Jul 2019 17:02:18 +0200 Subject: [PATCH] Add ability to trigger events directly from the API (#137) * Add ability to trigger events directly from the API * Add documentation for directly triggering an event * Fix English --- back-end/domain/module/AbstractModule.js | 1 + back-end/routes/event.js | 29 ++++++++++++++++++++++++ back-end/routes/index.js | 1 + docs/{status-API.md => API.md} | 29 +++++++++++++++++++++++- mkdocs.yml | 2 +- 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 back-end/routes/event.js rename docs/{status-API.md => API.md} (69%) diff --git a/back-end/domain/module/AbstractModule.js b/back-end/domain/module/AbstractModule.js index 461abc78..73c1eba9 100644 --- a/back-end/domain/module/AbstractModule.js +++ b/back-end/domain/module/AbstractModule.js @@ -12,6 +12,7 @@ class AbstractModule { } /** + * @param config * @param {Status} status */ fireEvent(config, status) { diff --git a/back-end/routes/event.js b/back-end/routes/event.js new file mode 100644 index 00000000..c0329b21 --- /dev/null +++ b/back-end/routes/event.js @@ -0,0 +1,29 @@ +const app = (module.exports = require('express')()); +const EventTrigger = require('../domain/event/EventTrigger.js'); +const Config = require('../config/ConfigLoaderFactory') + .getLoader() + .getConfig(); + +app.post('/', (request, response) => { + console.log('/event [POST]'); + + const eventName = request.body.event; + + if (!eventName) { + return response.status(422).json({ + message: `Missing required field "event".`, + }); + } + + if (Config.getEventByName(eventName)) { + EventTrigger.fireModulesForEvent(eventName, null); + + return response.json({ + message: 'Event has been triggered!', + }); + } + + return response.status(404).json({ + message: `The given event name "${eventName}" is not found.`, + }); +}); diff --git a/back-end/routes/index.js b/back-end/routes/index.js index f369fa6f..72fb2816 100644 --- a/back-end/routes/index.js +++ b/back-end/routes/index.js @@ -6,3 +6,4 @@ app.use('/webhook', require('./webhook')); app.use('/debug', require('./debug')); app.use('/contributors', require('./contributors')); app.use('/version', require('./version')); +app.use('/event', require('./event.js')); diff --git a/docs/status-API.md b/docs/API.md similarity index 69% rename from docs/status-API.md rename to docs/API.md index 5b867f1b..96105420 100644 --- a/docs/status-API.md +++ b/docs/API.md @@ -1,4 +1,11 @@ -# Status API +# API + +- [Statuses](#statuses) +- [Events](#events) + +## Statuses + +### POST /status @todo: Explain that you can manually push statuses to the `POST /status`. The API will let you know what's required or not. @@ -22,6 +29,26 @@ | `image` | no | An URL to an image representing the status | | `userImage` | no | An URL to an image showing the user who triggered the status | +### DELETE /status/:status-key + @todo: Explain that you can remove statues using `DELETE /status/:status-key` +### GET /status/clear-all + @todo: Explain that yo can remove all statuses using `GET /status/clear-all` + +## Events + +### POST /event + +You can directly trigger an event (configured in the configuration) via `POST /event`. + +```json +{ + "event": "celebrate-success" +} +``` + +| key | required? | description | +| ----------- | --------- | -------------------------------------------------------------------------- | +| `event` | yes | The configuration key that is used for the event that should be triggered. | diff --git a/mkdocs.yml b/mkdocs.yml index 1ded2502..cc988b73 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -12,7 +12,7 @@ pages: - Run as a service: setup/run-as-service.md - Run with Docker: setup/run-with-docker.md - Setup Raspberry: setup/Raspberry.md - - Status API: status-API.md + - CIMonitor API: API.md - Link a service: - GitLab: services/GitLab.md - Travis CI: services/Travis-CI.md