forked from unicode-org/jira-github-pr-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
435 lines (412 loc) · 13.5 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html#License
"use strict";
const bodyParser = require("body-parser");
const cookieSession = require("cookie-session");
const crypto = require("crypto");
const express = require("express");
const morgan = require("morgan");
const github = require("./src/github-status");
const jira = require("./src/jira-status");
const githubUser = require("./src/github-user");
const JIRA_COMMIT_PATTERN = /^([A-Z]+-\d+)\u0020\w/;
const PR_BODY_VAR_PATTERN = /^([A-Z_]+)=(.*?)(\s*#.*)?$/gm;
function parseMessage(message) {
const match = JIRA_COMMIT_PATTERN.exec(message);
if (!match) {
return null;
}
return match[1];
}
function parsePullRequestFlags(body) {
PR_BODY_VAR_PATTERN.lastIndex = 0; // reset /g regex
let prFlags = {};
let match;
// eslint-disable-next-line no-cond-assign
while (match = PR_BODY_VAR_PATTERN.exec(body)) {
let value = match[2];
if (value === "true") {
value = true;
} else if (value === "false") {
value = false;
} else if (!isNaN(parseFloat(value))) {
value = parseFloat(value);
}
prFlags[match[1]] = value;
}
return prFlags;
}
function makeViewUrl(endpoint, params) {
return `${process.env.URL_PREFIX}/${endpoint}/${params.owner}/${params.repo}/${params.pull_number}`;
}
async function getJiraInfo(pullRequest) {
const prFlags = parsePullRequestFlags(pullRequest.body);
const issueKey = parseMessage(pullRequest.title);
if (!issueKey) {
return {
issueKey: null,
prFlags,
pass: false,
description: "Pull request title must start with a Jira ticket ID"
};
}
// Load additional data from Jira and GitHub
const [jiraIssue, commits] = await Promise.all([
jira.getStatus(issueKey),
github.getCommits({
owner: pullRequest.base.repo.owner.login,
repo: pullRequest.base.repo.name,
pull_number: pullRequest.number
})
]);
const jiraStatus = jiraIssue && jiraIssue.fields.status.name;
const jiraSummary = jiraIssue && jiraIssue.fields.summary;
const numCommits = commits.length;
const isMaintMerge = (pullRequest.base.ref === "master"
&& pullRequest.head.ref.match(/^maint\//)
&& pullRequest.base.repo.full_name == pullRequest.head.repo.full_name);
let jiraApprovedStatuses = process.env.JIRA_APPROVED_STATUSES || "Accepted, Reviewing, Review Feedback"
const jiraApprovedStatusesArray = jiraApprovedStatuses.split(",").map(status => status.trim())
// Check Jira ticket for validity
if (!jiraApprovedStatuses.includes(jiraStatus) && process.env.JIRA_STATUS_CHECK === "TRUE") {
return {
issueKey,
jiraStatus,
numCommits,
isMaintMerge,
prFlags,
pass: false,
description: jiraStatus === null ?
"Jira ticket " + issueKey + " not found" :
"Jira ticket " + issueKey + " is not accepted; it has status " + jiraStatus
};
}
// Check for consistency with the commit messages
if(process.env.SEARCH_JIRA_ISSUE_IN_COMMIT === "TRUE") {
for (const commitInfo of commits) {
const commitIssueKey = parseMessage(commitInfo.commit.message);
if (commitIssueKey === null) {
return {
issueKey,
jiraStatus,
numCommits,
isMaintMerge,
prFlags,
pass: false,
description: "Commit message for " + commitInfo.sha.substr(0, 7) + " fails validation",
badCommit: commitInfo
};
} else if (commitIssueKey !== issueKey && !prFlags["DISABLE_JIRA_ISSUE_MATCH"] && !isMaintMerge) {
return {
issueKey,
jiraStatus,
numCommits,
isMaintMerge,
prFlags,
pass: false,
description: "Commit " + commitInfo.sha.substr(0, 7) + " is for " + commitIssueKey + ", but the PR is for " + issueKey,
extendedDescription: "Please fix your commit message to have the same ticket number as the pull request. If the inconsistency is intentional, you can disable this warning with DISABLE_JIRA_ISSUE_MATCH=true in the PR description.",
badCommit: commitInfo
};
}
}
}
// Since we can't easily check more than 100 commits, reject PRs with more than 100 commits
if (commits.length === 100) {
return {
issueKey,
jiraStatus,
numCommits,
isMaintMerge,
prFlags,
pass: false,
description: "PR has more than 100 commits; please rebase and squash"
};
}
// All checks passed
return {
issueKey,
jiraStatus,
numCommits,
isMaintMerge,
prFlags,
pass: true,
description: issueKey + " \u201C" + jiraSummary + "\u201D"
};
}
async function checkForcePush({ before, after }, pullRequest) {
const owner = pullRequest.base.repo.owner.login;
const repo = pullRequest.base.repo.name;
// Check to see if this was a force push
const compRes = await github.getCommitDiff({ owner, repo, base: before, head: after });
if (compRes.status !== "diverged") {
// Not a force push
console.log("Push to branch status:", compRes.status);
return;
}
const base = pullRequest.base.sha;
const pull_number = pullRequest.number;
const [ beforeRes, afterRes ] = await Promise.all([
github.getCommitDiff({ owner, repo, base, head: before }),
github.getCommitDiff({ owner, repo, base, head: after })
]);
const compareFilename = (a, b) => {
if (!a.filename) return 1;
if (!b.filename) return -1;
return a.filename.localeCompare(b.filename);
};
const beforeFiles = beforeRes.files.slice().sort(compareFilename);
const afterFiles = afterRes.files.slice().sort(compareFilename);
const errors = [];
for (let i=0, j=0; i < beforeFiles.length || j < afterFiles.length; i++, j++) {
let file1 = beforeFiles[i] || {};
let file2 = afterFiles[j] || {};
if (file1.filename === file2.filename && file1.sha === file2.sha) {
continue;
}
if (file1.filename === file2.filename) {
errors.push(file1.filename + " is different");
continue;
}
if (compareFilename(file1, file2) < 0) {
// file1 is earlier
errors.push(file1.filename + " is no longer changed in the branch");
j--; // re-evaluate file2
} else {
// file2 is earlier
errors.push(file2.filename + " is now changed in the branch");
i--; // re-evaluate file1
}
}
let body;
if (errors.length) {
body = "Notice: the branch changed across the force-push!\n\n";
errors.forEach((error) => {
body += "- " + error + "\n";
});
const humanDiffUrl = `https://github.com/${owner}/${repo}/compare/${owner}:${before.substr(0, 7)}..${owner}:${after.substr(0, 7)}`;
body += "\n[View Diff Across Force-Push](" + humanDiffUrl + ")";
console.log(`Force-Push has diffs: ${owner}/${repo} ${before} ${after}`);
} else {
body = "Hooray! The files in the branch are the same across the force-push. 😃";
console.log(`Force-Push has no file diffs: ${owner}/${repo} ${before} ${after}`);
}
body += "\n\n~ Your Friendly Jira-GitHub PR Checker Bot";
return github.postComment({ owner, repo, issue_number: pull_number, body });
}
const DO_NOT_TOUCH_REPOS = (process.env.DO_NOT_TOUCH_REPOS || "").split(",");
async function touch(pullRequest, jiraInfo) {
const owner = pullRequest.base.repo.owner.login;
const repo = pullRequest.base.repo.name;
if (DO_NOT_TOUCH_REPOS.indexOf(owner + "/" + repo) !== -1) {
console.log("Not touching: repo is " + owner + "/" + repo);
return;
}
const pull_number = pullRequest.number;
const state = pullRequest.state;
if (state !== "open") {
console.log("Not touching: PR is " + state + ": " + pull_number);
return;
}
const url = makeViewUrl("info", { owner, repo, pull_number });
const multiCommitPass = jiraInfo.numCommits === 1
|| (jiraInfo.numCommits > 1 && (jiraInfo.isMaintMerge || jiraInfo.prFlags["ALLOW_MANY_COMMITS"]));
const multiCommitMessage = (jiraInfo.numCommits === 0) ? "No commits found on PR" : (jiraInfo.numCommits === 1) ? "This PR includes exactly 1 commit!" : "This PR has " + jiraInfo.numCommits + " commits" + (multiCommitPass ? "" : "; consider squashing:");
const promises = [
github.createStatus("jira-ticket", pullRequest, jiraInfo.pass, url, jiraInfo.description),
];
if (!(process.env.ALLOW_MANY_COMMITS === "TRUE")) {
promises.push(github.createStatus("single-commit", pullRequest, multiCommitPass, undefined, multiCommitMessage))
}
if (jiraInfo.isMaintMerge) {
promises.push(github.createStatus("maint-merge", pullRequest, false, undefined, "Reminder: use a MERGE COMMIT and new ticket in the message."));
}
return Promise.all(promises);
}
async function squash(req) {
const pullRequest = await github.getPullRequest(req.body);
const owner = pullRequest.head.repo.owner.login;
const repo = pullRequest.head.repo.name;
const ref = "heads/" + pullRequest.head.ref;
const parentSha = pullRequest.base.sha;
const headSha = pullRequest.head.sha;
const message = req.body.title + "\n\n" + req.body.description;
const githubToken = req.session["gh_user"];
if (!githubToken) {
console.error("Null github token!");
return;
}
const commitData = await github.writeSquashCommit(githubToken, {
owner,
repo,
parentSha,
headSha,
message
});
await github.writeBranch(githubToken, {
owner,
repo,
ref,
sha: commitData.sha,
force: true
});
}
const COOKIE_SECRET = process.env.COOKIE_SECRET || Math.random().toString(36).substr(2, 15);
const app = express()
.use(bodyParser.json({
verify: (req, res, body /*, encoding*/) => {
// Compute and save the hash of the raw body
const key = process.env.GITHUB_WEBHOOK_SECRET;
if (key) {
const hash = crypto.createHmac("sha1", key);
hash.update(body);
req.bodyDigest = hash.digest("hex");
}
}
}))
.use(bodyParser.urlencoded({ extended: false }))
.use(cookieSession({ secret: COOKIE_SECRET }))
.use(morgan("tiny"))
.get("/info/:owner/:repo/:pull_number", async (req, res) => {
try {
const pullRequest = await github.getPullRequest(req.params);
const jiraInfo = await getJiraInfo(pullRequest);
return res.render("info.ejs", {
params: req.params,
pullRequest,
jiraInfo,
jiraUrl: jiraInfo.issueKey ? jira.getUrl(jiraInfo.issueKey) : undefined,
badCommit: jiraInfo.badCommit,
checkerGithubUrl: require("./package.json").repository.url,
instructionsUrl: process.env.INSTRUCTIONS_URL,
squashUrl: makeViewUrl("squash", req.params),
});
} catch (err) {
if (err.code) {
return res.sendStatus(err.code);
} else {
console.error(err);
return res.sendStatus(500);
}
}
})
.post("/touch/:owner/:repo/:pull_number", async (req, res) => {
try {
const pullRequest = await github.getPullRequest(req.params);
const jiraInfo = await getJiraInfo(pullRequest);
await touch(pullRequest, jiraInfo);
return res.sendStatus(204);
} catch (err) {
if (err.code) {
return res.sendStatus(err.code);
} else {
console.error(err);
return res.sendStatus(500);
}
}
})
.get("/squash/:owner/:repo/:pull_number", async (req, res) => {
if (!req.session["gh_user"]) {
// Authorize GitHub first
const { url, state } = githubUser.createGithubLoginUrl();
req.session["gh_state"] = state;
req.session["redirect"] = process.env.URL_PREFIX + req.originalUrl;
return res.redirect(url);
}
try {
// Continue to Squash UI
const pullRequest = await github.getPullRequest(req.params);
const jiraInfo = await getJiraInfo(pullRequest);
return res.render("squash.ejs", {
params: req.params,
pullRequest,
jiraInfo,
checkerGithubUrl: require("./package.json").repository.url,
errorCode: req.query.code,
});
} catch (err) {
console.error(err);
if (err.code) {
return res.sendStatus(err.code);
} else {
return res.sendStatus(500);
}
}
})
.get("/github-auth", async (req, res) => {
if (req.query.state !== req.session["gh_state"]) {
// Unexpected state
return res.sendStatus(400);
}
try {
// Exchange code for an OAuth token
const redirectTo = req.session["redirect"];
const token = await githubUser.getToken(req.query);
req.session["gh_user"] = token;
delete req.session["gh_state"];
delete req.session["redirect"];
return res.redirect(redirectTo);
} catch (err) {
console.error(err);
if (err.code) {
return res.sendStatus(err.code);
} else {
return res.sendStatus(500);
}
}
})
.post("/do-squash", async (req, res) => {
if (!req.session["gh_user"]) {
// Unexpected state
return res.sendStatus(400);
}
try {
if (!req.body.confirm) {
return res.status(422).send("Please check the confirmation box!");
}
await squash(req);
return res.redirect(makeViewUrl("info", req.body));
} catch (err) {
if (err.code) {
// If this failed gracefully, send the user to the GitHub login flow. The token could be expired or deactivated or have the wrong scopes.
delete req.session["gh_user"];
return res.redirect(makeViewUrl("squash", req.body) + "?code=" + err.code);
} else {
return res.sendStatus(500);
}
}
})
.post("/hook", async (req, res) => {
if (req.bodyDigest) {
const expectedSignature = "sha1=" + req.bodyDigest;
const actualSignature = req.get("X-Hub-Signature");
if (expectedSignature !== actualSignature) {
console.log("Notice: Ignoring webhook request with bad signature: expected " + expectedSignature + "; got " + actualSignature);
return res.sendStatus(403);
}
}
try {
const pullRequest = req.body.pull_request;
// Check for push event
if (req.body.action === "synchronize") {
await checkForcePush(req.body, pullRequest);
}
const jiraInfo = await getJiraInfo(pullRequest);
await touch(pullRequest, jiraInfo);
return res.sendStatus(204);
} catch (err) {
console.error("Error when processing request:", err);
if (err.status) {
return res.sendStatus(err.status);
} else {
return res.sendStatus(500);
}
}
});
module.exports = {
app: (req, res) => {
return app(req, res);
},
getJiraInfo,
touch
};