Skip to content

Commit 3d0e287

Browse files
authored
Merge pull request #45 from fox-forks/hyperupcall-bug-fixes-2
Improve error message and merge heuristic
2 parents 7c66472 + 7f43d23 commit 3d0e287

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function run() {
1717
// Merge if they say they have access
1818
if (context.eventName === "issue_comment" || context.eventName === "pull_request_review") {
1919
const bodyLower = getPayloadBody().toLowerCase();
20-
if (bodyLower.includes("lgtm") && !bodyLower.includes("lgtm but")) {
20+
if (hasValidLgtmSubstring(bodyLower)) {
2121
new Actor().mergeIfHasAccess();
2222
} else if (bodyLower.includes("@github-actions close")) {
2323
new Actor().closePROrIssueIfInCodeowners();
@@ -170,6 +170,12 @@ class Actor {
170170

171171
const { octokit, thisRepo, issue, sender } = this;
172172

173+
// Don't try merge if mergability is not yet known
174+
if (prInfo.data.mergeable === null) {
175+
await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Sorry @${sender}, this PR is still running background checks to compute mergeability. They'll need to complete before this can be merged.` });
176+
return
177+
}
178+
173179
// Don't try merge unmergable stuff
174180
if (!prInfo.data.mergeable) {
175181
await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Sorry @${sender}, this PR has merge conflicts. They'll need to be fixed before this can be merged.` });
@@ -267,6 +273,27 @@ function getFilesNotOwnedByCodeOwner(owner, files, cwd) {
267273
return contents.includes("@" + login.toLowerCase() + " ") || contents.includes("@" + login.toLowerCase() + "\n")
268274
}
269275

276+
/**
277+
*
278+
* @param {string} bodyLower
279+
*/
280+
function hasValidLgtmSubstring(bodyLower) {
281+
if (bodyLower.includes("lgtm")) {
282+
if (bodyLower.includes("lgtm but")) return false
283+
if (bodyLower.includes("lgtm, but")) return false
284+
285+
const charBefore = bodyLower.charAt(bodyLower.indexOf("lgtm") - 1)
286+
const charAfter = bodyLower.charAt(bodyLower.indexOf("lgtm") + "lgtm".length)
287+
if (charBefore === "\"" || charAfter === "\"") return false
288+
if (charBefore === "'" || charAfter === "'") return false
289+
if (charBefore === "`" || charAfter === "`") return false
290+
291+
return true
292+
}
293+
294+
return false
295+
}
296+
270297

271298
/**
272299
*
@@ -343,7 +370,8 @@ async function createOrAddLabel(octokit, repoDeets, labelConfig) {
343370
module.exports = {
344371
getFilesNotOwnedByCodeOwner,
345372
findCodeOwnersForChangedFiles,
346-
githubLoginIsInCodeowners
373+
githubLoginIsInCodeowners,
374+
hasValidLgtmSubstring
347375
}
348376

349377
// @ts-ignore

index.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getFilesNotOwnedByCodeOwner, findCodeOwnersForChangedFiles, githubLoginIsInCodeowners } = require(".");
1+
const { getFilesNotOwnedByCodeOwner, findCodeOwnersForChangedFiles, githubLoginIsInCodeowners, hasValidLgtmSubstring } = require(".");
22

33
test("determine who owns a set of files", () => {
44
const noFiles = findCodeOwnersForChangedFiles(["root-codeowners/one.two.js"], "./test-code-owners-repo");
@@ -53,3 +53,30 @@ describe(githubLoginIsInCodeowners, () => {
5353
expect(noOrt).toEqual(false);
5454
});
5555
})
56+
57+
describe(hasValidLgtmSubstring, () => {
58+
test("allows lgtm", () => {
59+
const isValidSubstring = hasValidLgtmSubstring("this lgtm!");
60+
expect(isValidSubstring).toEqual(true);
61+
});
62+
test("denies lgtm but", () => {
63+
const isValidSubstring = hasValidLgtmSubstring("this lgtm but");
64+
expect(isValidSubstring).toEqual(false);
65+
});
66+
test("denies lgtm but", () => {
67+
const isValidSubstring = hasValidLgtmSubstring("this lgtm, but");
68+
expect(isValidSubstring).toEqual(false);
69+
});
70+
test("denies lgtm in double quotes", () => {
71+
const isValidSubstring = hasValidLgtmSubstring("\"lgtm\"");
72+
expect(isValidSubstring).toEqual(false);
73+
});
74+
test("denies lgtm in single quotes", () => {
75+
const isValidSubstring = hasValidLgtmSubstring("'lgtm");
76+
expect(isValidSubstring).toEqual(false);
77+
});
78+
test("denies lgtm in inline code blocks", () => {
79+
const isValidSubstring = hasValidLgtmSubstring("lgtm`");
80+
expect(isValidSubstring).toEqual(false);
81+
});
82+
})

0 commit comments

Comments
 (0)