Skip to content

Commit 7f43d23

Browse files
committed
Improve heuristic for detecting "false" LGTM strings
1 parent a8187bf commit 7f43d23

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

index.js

+24-2
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();
@@ -273,6 +273,27 @@ function getFilesNotOwnedByCodeOwner(owner, files, cwd) {
273273
return contents.includes("@" + login.toLowerCase() + " ") || contents.includes("@" + login.toLowerCase() + "\n")
274274
}
275275

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+
276297

277298
/**
278299
*
@@ -349,7 +370,8 @@ async function createOrAddLabel(octokit, repoDeets, labelConfig) {
349370
module.exports = {
350371
getFilesNotOwnedByCodeOwner,
351372
findCodeOwnersForChangedFiles,
352-
githubLoginIsInCodeowners
373+
githubLoginIsInCodeowners,
374+
hasValidLgtmSubstring
353375
}
354376

355377
// @ts-ignore

index.test.js

+28-1
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)