forked from chriskinsman/github-action-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhooks.js
More file actions
48 lines (43 loc) · 1.91 KB
/
webhooks.js
File metadata and controls
48 lines (43 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const debug = require('debug')('action-dashboard:webhooks');
if (process.env.GITHUB_APP_WEBHOOK_SECRET) {
const { Webhooks } = require("@octokit/webhooks");
const github = require('./github');
const webhooks = new Webhooks({
secret: process.env.GITHUB_APP_WEBHOOK_SECRET,
});
const { GITHUB_APP_WEBHOOK_PORT = 8081 } = process.env;
debug(`Setting up webhooks port: ${GITHUB_APP_WEBHOOK_PORT}`);
webhooks.on('workflow_run', ({ id, name, payload }) => {
debug(`workflow_run received id: ${id}, name: ${name}`, payload);
github.mergeRuns([{
runId: payload.workflow_run.id,
repo: payload.workflow_run.repository.name,
owner: payload.workflow_run.repository.owner.login,
workflowId: payload.workflow_run.workflow_id,
runNumber: payload.workflow_run.run_number,
workflow: payload.workflow_run.name,
branch: payload.workflow_run.head_branch,
sha: payload.workflow_run.head_sha,
message: payload.workflow_run.head_commit.message,
committer: payload.workflow_run.head_commit.committer.name,
status: payload.workflow_run.status === 'completed' ? payload.workflow_run.conclusion : payload.workflow_run.status,
createdAt: payload.workflow_run.created_at,
updatedAt: payload.workflow_run.updated_at
}]);
});
if (!process.env.DOCKER_BUILD) {
require("http").createServer((req, res) => {
debug(`received request path: ${req.url}`);
if (req.url === '/ping') {
debug('ping');
res.statusCode = 200;
res.end();
}
else {
webhooks.middleware(req, res);
}
}).listen({ port: GITHUB_APP_WEBHOOK_PORT }, () => {
console.log(`Listening for webhooks on ${GITHUB_APP_WEBHOOK_PORT}`);
});
}
}