-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
121 lines (109 loc) · 2.79 KB
/
index.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
var body_parser = require('body-parser');
var rq = require('request-promise-native');
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Application} app
*/
module.exports = app => {
app.log('Yay, the app was loaded!');
const BUILDKITE_TOKEN = process.env.BUILDKITE_TOKEN;
const AHA_FLOW_TOKEN = process.env.AHA_FLOW_TOKEN;
// this is standard express route to receive buildkite hooks
const router = app.route("/aha-flow-app");
const title = "StanfordAHA Flow Check"
router.use(body_parser.json())
// add a route
router.post("/hook", async (req, res) => {
const token = req.headers.authorization;
if (token != AHA_FLOW_TOKEN) {
res.send(401);
}
const id = parseInt(req.body.id);
let github = await app.auth(id);
const org = req.body.org;
const repo = req.body.repo;
const head_sha = req.body.head_sha;
const status = req.body.status;
const conclusion = req.body.conclusion;
const url = req.body.url;
let summary = "";
github.checks.create({
owner: org,
repo: repo,
name: "StanfordAHA Flow",
head_sha: head_sha,
status: status,
conclusion: conclusion,
completed_at: new Date().toISOString(),
output: {
title: title,
summary: summary
},
details_url: url
})
res.send("okay")
});
app.on(["pull_request.synchronize", "pull_request.opened"], async context => {
const pr = context.payload.pull_request;
const head = pr.head
if (!pr || pr.state !== "open") {
// don't do anything with closed PR
return;
}
if (pr.base.ref != "master") {
// we are only interested in the pull request for the master
return;
}
const sha = head.sha;
const owner = pr.base.repo.owner;
const org = owner.login;
var repo = pr.base.repo.name;
// remove the "-"
new_repo_name = repo.split("-").join("_");
let summary = "";
// we need to send out a build kite build event
var env = {
FLOW_ORG: org,
FLOW_REPO: repo,
FLOW_HEAD_SHA: sha,
FLOW_ID: context.payload.installation.id
};
env[new_repo_name] = sha;
env["PR"] = 1;
var options = {
method: "POST",
url: "https://api.buildkite.com/v2/organizations/stanford-aha/pipelines/aha-flow/builds",
body: {
commit: "HEAD",
branch: "master",
message: "PR from " + repo,
env: env,
},
json: true,
headers: {
Authorization: "Bearer " + BUILDKITE_TOKEN
}
}
var link = "";
const result = await rq(options)
.then(function(info) {
link = info.web_url;
})
.catch(function(err) {
app.log(err);
});
return context.github.checks.create({
owner: org,
repo: repo,
name: "StanfordAHA Flow",
head_sha: sha,
status: 'in_progress',
output: {
title: title,
summary: summary
},
details_url: link,
started_at: new Date().toISOString(),
})
})
}