Skip to content

Commit

Permalink
feat: improve label matching by making it case-insensitive (#76)
Browse files Browse the repository at this point in the history
Fixes #75
  • Loading branch information
codfish committed Mar 20, 2024
1 parent fab3b73 commit 33f4833
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions index.js
Expand Up @@ -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?
Expand Down
30 changes: 30 additions & 0 deletions index.test.js
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 33f4833

Please sign in to comment.