Skip to content

Commit

Permalink
Add ability to trigger events directly from the API (#137)
Browse files Browse the repository at this point in the history
* Add ability to trigger events directly from the API

* Add documentation for directly triggering an event

* Fix English
  • Loading branch information
rick-nu authored and eXistenZNL committed Jul 25, 2019
1 parent bd08733 commit 5c8996d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions back-end/domain/module/AbstractModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AbstractModule {
}

/**
* @param config
* @param {Status} status
*/
fireEvent(config, status) {
Expand Down
29 changes: 29 additions & 0 deletions back-end/routes/event.js
Original file line number Diff line number Diff line change
@@ -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.`,
});
});
1 change: 1 addition & 0 deletions back-end/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
29 changes: 28 additions & 1 deletion docs/status-API.md → docs/API.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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. |
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c8996d

Please sign in to comment.