Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve label matching by making it case-insensitive #76

Merged
merged 1 commit into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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