-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathwebhooks.js
144 lines (132 loc) · 4.33 KB
/
webhooks.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const debug = require("debug")("action-dashboard:webhooks");
const { Webhooks, createNodeMiddleware } = require("@octokit/webhooks");
const http = require("http");
class WebHooks {
constructor(
sitePort,
secret,
webhookPort,
webhookPath,
gitHub,
actions,
expressApp
) {
if (secret) {
this._secret = secret;
this._webhookPort = webhookPort;
this._sitePort = sitePort;
this._gitHub = gitHub;
this._actions = actions;
if (sitePort === webhookPort) {
this._defaultPath = "/webhook";
} else {
this._defaultPath = "/";
}
this._path = webhookPath || this._defaultPath;
// Fail in the case that the ports for the main site and webhooks
// are the same and the path is explicitly set to /
if (sitePort === webhookPort && this._path === "/") {
throw new Error(
"Path cannot be / when the webhooks are running on the same port as the main site"
);
}
this._expressApp = expressApp;
this._enabled = true;
}
}
start() {
if (this._enabled) {
debug(
`Setting up webhooks port: ${this._webhookPort}, path: ${this._path}`
);
// OctoKit webhooks, not this module
const webhooks = new Webhooks({
secret: this._secret,
});
webhooks.onError((error) => {
console.dir(error);
// console.error(
// `Webhook error occured in "${error.event.name} handler: ${error.stack}"`
// );
});
const middleware = createNodeMiddleware(webhooks, { path: this._path });
webhooks.on("workflow_run", this.workflowRun);
if (this._sitePort !== this._webhookPort) {
this._server = http
.createServer((req, res) => {
debug(`received request path: ${req.url}`);
if (req.url === "/ping") {
debug("ping");
res.statusCode = 200;
res.end();
} else {
middleware(req, res);
}
})
.listen({ port: this._webhookPort }, () => {
console.log(
`Listening for webhooks on ${this._webhookPort} at ${this._path}`
);
});
} else {
this._expressApp.use(this._path, middleware);
console.log(
`Listening for webhooks on ${this._webhookPort} at ${this._path}`
);
}
} else {
debug("Webhooks disabled");
}
}
// Mainly used by testing functions to cleanly shutdown web server
stop() {
if (this._enabled && this._server && this._server.listening) {
this._server.close();
}
}
workflowRun = async ({ id, name, payload }) => {
try {
debug(`workflow_run received id: ${id}, name: ${name}`, payload);
let usage = null;
if (payload.workflow_run.status === "completed") {
debug(`getting usage for id: ${id}, name: ${name}`);
usage = await this._gitHub.getUsage(
payload.workflow_run.repository.owner.login,
payload.workflow_run.repository.name,
payload.workflow_run.workflow_id,
payload.workflow_run.id
);
}
debug(`merging runs for id: ${id}, name: ${name}`);
this._actions.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,
durationMs: usage?.run_duration_ms,
},
]);
debug(`runs merged for id: ${id}, name: ${name}`);
} catch (e) {
console.dir(e);
console.error(
`Error processing workflow_run received id: ${id}, name: ${name}`,
payload
);
}
};
}
module.exports = WebHooks;