forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-pull-request.js
More file actions
67 lines (62 loc) · 1.8 KB
/
Copy pathnew-pull-request.js
File metadata and controls
67 lines (62 loc) · 1.8 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const github = require("https://github.com/PipedreamHQ/pipedream/components/github/github.app.js");
//const github = require("./github.app.js");
const eventNames = ["pull_request"]
const eventTypes = ['opened']
function generateMeta(data) {
return {
summary: `#${data.number} ${data.pull_request.title} by ${data.sender.login}`
}
}
module.exports = {
name: "New Pull Request (Instant)",
description: "Triggers when a new pull request is opened",
version: "0.0.1",
props: {
github,
repoFullName: { propDefinition: [github, "repoFullName"] },
http: "$.interface.http",
db: "$.service.db",
},
hooks: {
async activate() {
const secret = await this.github.generateSecret();
const { id } = await this.github.createHook({
repoFullName: this.repoFullName,
endpoint: this.http.endpoint,
events: eventNames,
secret,
});
this.db.set("hookId", id);
this.db.set("secret", secret);
},
async deactivate() {
await this.github.deleteHook({
repoFullName: this.repoFullName,
hookId: this.db.get("hookId"),
});
},
},
async run(event) {
this.http.respond({
status: 200,
});
const { body, headers } = event;
if (headers["X-Hub-Signature"]) {
const crypto = require("crypto");
const algo = "sha1";
const hmac = crypto.createHmac(algo, this.db.get("secret"));
hmac.update(body, "utf-8");
if (headers["X-Hub-Signature"] !== `${algo}=${hmac.digest("hex")}`) {
throw new Error("signature mismatch");
}
}
if ("zen" in body) {
console.log("Zen event to confirm subscription, nothing to emit");
return;
}
if (eventTypes.indexOf(body.action) > -1) {
const meta = generateMeta(body)
this.$emit(body, meta);
}
},
};