From 5235ae6116c2c05aefe3f8bfd5a45f0aed9f9366 Mon Sep 17 00:00:00 2001 From: Tim Jacomb Date: Mon, 1 Aug 2022 20:55:20 +0100 Subject: [PATCH] Handle comments with multiple lines --- app/matchers.js | 12 +++++----- app/matchers.test.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ app/router.js | 1 - 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/app/matchers.js b/app/matchers.js index 137e98b..7697503 100644 --- a/app/matchers.js +++ b/app/matchers.js @@ -1,24 +1,24 @@ export function transferMatcher(text) { - return text.match(/\/transfer ([\dA-Za-z-]+)/); + return text.match(/(?:^| | \r\n|\n)\/transfer ([\dA-Za-z-]+)/); } export function closeMatcher(text) { - return text.match(/\/close (not-planned)|\/close/); + return text.match(/(?:^| | \r\n|\n)\/close (not-planned)|\/close/); } export function reopenMatcher(text) { - return text.match(/\/reopen/); + return text.match(/(?:^| | \r\n|\n)\/reopen/); } export function labelMatcher(text) { - return text.match(/\/label ([\s/A-Za-z\d-,:]+)/); + return text.match(/(?:^| | \r\n|\n)\/label ([ /A-Za-z\d-,:]+)/); } export function removeLabelMatcher(text) { // TODO prevent matching across lines - return text.match(/\/remove-label ([\s/A-Za-z\d-,:]+)/); + return text.match(/(?:^| | \r\n|\n)\/remove-label ([ /A-Za-z\d-,:]+)/); } export function reviewerMatcher(text) { - return text.match(/\/reviewers? ([\s/@A-Za-z\d-,]+)/); + return text.match(/(?:^| | \r\n|\n)\/reviewers? ([ /@A-Za-z\d-,]+)/); } diff --git a/app/matchers.test.js b/app/matchers.test.js index c7fb49c..fda070d 100644 --- a/app/matchers.test.js +++ b/app/matchers.test.js @@ -23,6 +23,19 @@ describe("matchers", () => { test("does not match without a repository name", () => { const result = transferMatcher("/transfer"); + expect(result).toBeFalsy(); + }); + test("only matches the current line", () => { + const result = transferMatcher("/transfer github-comment-ops\nasda"); + + expect(result).toBeTruthy(); + expect(result[1]).toEqual("github-comment-ops"); + }); + test("does not match in the middle of a string", () => { + const result = transferMatcher( + "hello world/transfer github-comment-ops\nasda" + ); + expect(result).toBeFalsy(); }); }); @@ -65,6 +78,12 @@ describe("matchers", () => { expect(result).toBeFalsy(); }); + + test("does not match in the middle of a string", () => { + const result = reopenMatcher("a string/reopen"); + + expect(result).toBeFalsy(); + }); }); describe("label", () => { @@ -74,6 +93,17 @@ describe("matchers", () => { expect(result).toBeTruthy(); expect(result[1]).toEqual("label1"); }); + test("only matches the current line", () => { + const result = labelMatcher("/label label1\nasda"); + + expect(result).toBeTruthy(); + expect(result[1]).toEqual("label1"); + }); + test("does not match in the middle of a string", () => { + const result = labelMatcher("something cool/label label1\nasda"); + + expect(result).toBeFalsy(); + }); test("does not match input without /label", () => { const result = labelMatcher("label label1"); @@ -131,6 +161,19 @@ describe("matchers", () => { expect(result).toBeTruthy(); expect(result[1]).toEqual("label1,label 2 with spaces,label3"); }); + test("only matches the current line", () => { + const result = removeLabelMatcher("/remove-label label1\nasda"); + + expect(result).toBeTruthy(); + expect(result[1]).toEqual("label1"); + }); + test("does not match in the middle of a string", () => { + const result = removeLabelMatcher( + "something cool/remove-label label1\nasda" + ); + + expect(result).toBeFalsy(); + }); }); describe("reviewer", () => { @@ -179,5 +222,17 @@ describe("matchers", () => { expect(result).toBeTruthy(); expect(result[1]).toEqual("reviewer1 @reviewer2 @org/team"); }); + + test("only matches the current line", () => { + const result = reviewerMatcher("/reviewer reviewer1\nasda"); + + expect(result).toBeTruthy(); + expect(result[1]).toEqual("reviewer1"); + }); + test("does not match in the middle of a string", () => { + const result = reviewerMatcher("something cool/reviewer reviewer1\nasda"); + + expect(result).toBeFalsy(); + }); }); }); diff --git a/app/router.js b/app/router.js index d1b418a..ae60fc6 100644 --- a/app/router.js +++ b/app/router.js @@ -14,7 +14,6 @@ export async function router(auth, id, payload, verbose) { const commands = getCommands(id, payload); if (noneMatch(commands)) { - console.log("none match"); if (verbose) { console.log("No match for", payload.comment.body); }