From 33f48337f64701a0e8c82c523671638ee9710c0c Mon Sep 17 00:00:00 2001 From: Chris O'Donnell <1666298+codfish@users.noreply.github.com> Date: Wed, 20 Mar 2024 17:14:00 -0400 Subject: [PATCH] feat: improve label matching by making it case-insensitive (#76) Fixes #75 --- README.md | 3 +++ index.js | 5 +++-- index.test.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 55fadd7..ef80a1e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ This action calls the GitHub API to fetch labels for a PR rather than reading `e If successful, any matching labels will be output in `outputs.labels` as a comma separated string. +> [!TIP] +> Label matching is **case-insensitive**. + ## Examples ### Complete example diff --git a/index.js b/index.js index 995e7a2..2879cef 100644 --- a/index.js +++ b/index.js @@ -86,11 +86,12 @@ async function action() { if (labelsAreRegex) { intersection = appliedLabels.filter((appliedLabel) => providedLabels.some((providedLabel) => - new RegExp(providedLabel).test(appliedLabel) + new RegExp(providedLabel, 'i').test(appliedLabel) ) ); } else { - intersection = providedLabels.filter((x) => appliedLabels.includes(x)); + const lowerCasedAppliedLabels = appliedLabels.map((label) => label.toLowerCase()); + intersection = providedLabels.filter((x) => lowerCasedAppliedLabels.includes(x.toLowerCase())); } // Is there an error? diff --git a/index.test.js b/index.test.js index 83ea787..5885656 100644 --- a/index.test.js +++ b/index.test.js @@ -446,6 +446,36 @@ describe("Required Labels", () => { expect(core.setOutput).toBeCalledWith("status", "success"); expect(core.setOutput).toBeCalledWith("labels", "bug"); }); + + it("is case insensitive when matching INPUT_LABELS", async () => { + restoreTest = mockPr({ + INPUT_LABELS: "Do Not Merge", + INPUT_MODE: "exactly", + INPUT_COUNT: "1", + }); + mockLabels(["DO NOT MERGE"]); + + await action(); + expect(core.setOutput).toBeCalledTimes(2); + expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "Do Not Merge"); + }); + + it("is case insensitive when matching INPUT_LABELS using regex", async () => { + restoreTest = mockPr({ + INPUT_LABELS: "needs .*", + INPUT_MODE: "exactly", + INPUT_COUNT: "2", + INPUT_USE_REGEX: "true", + }); + mockLabels(["Needs Code Review", "Needs QA Review", "Do Not Merge"]); + + await action(); + + expect(core.setOutput).toBeCalledTimes(2); + expect(core.setOutput).toBeCalledWith("status", "success"); + expect(core.setOutput).toBeCalledWith("labels", "Needs Code Review,Needs QA Review"); + }); }); describe("configurable exit code", () => {