diff --git a/__tests__/fixtures/branches.yml b/__tests__/fixtures/branches.yml index cdf9551d..b72c67f2 100644 --- a/__tests__/fixtures/branches.yml +++ b/__tests__/fixtures/branches.yml @@ -3,3 +3,6 @@ test-branch: feature-branch: - branch: "*/feature/*" + +bug-branch: + - branch: "{bug,fix}/*" diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 1c89ea8f..85e85f4c 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -105,7 +105,7 @@ describe("run", () => { }); it("adds labels based on the branch names that match the glob pattern", async () => { - github.context.ref = "test/testing-time"; + github.context.payload.pull_request!.head = { ref: "test/testing-time" }; usingLabelerConfigYaml("branches.yml"); await run(); @@ -119,7 +119,9 @@ describe("run", () => { }); it("adds labels based on branch names that match different glob patterns", async () => { - github.context.ref = "my/feature/that-i-like"; + github.context.payload.pull_request!.head = { + ref: "my/feature/that-i-like", + }; usingLabelerConfigYaml("branches.yml"); await run(); @@ -131,6 +133,20 @@ describe("run", () => { labels: ["feature-branch"], }); }); + + it("it can support multiple branches by batching", async () => { + github.context.payload.pull_request!.head = { ref: "fix/123" }; + usingLabelerConfigYaml("branches.yml"); + await run(); + + expect(addLabelsMock).toHaveBeenCalledTimes(1); + expect(addLabelsMock).toHaveBeenCalledWith({ + owner: "monalisa", + repo: "helloworld", + issue_number: 123, + labels: ["bug-branch"], + }); + }); }); function usingLabelerConfigYaml(fixtureName: keyof typeof yamlFixtures): void { diff --git a/dist/index.js b/dist/index.js index 8f51264c..abbfb3a8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -205,7 +205,7 @@ function checkAll(changedFiles, globs) { } function checkBranch(glob) { const matcher = new minimatch_1.Minimatch(glob); - const branchName = github.context.ref; + const branchName = github.context.payload.pull_request.head.ref; core.debug(` checking "branch" pattern against ${branchName}`); core.debug(` - ${printPattern(matcher)}`); if (!matcher.match(branchName)) { diff --git a/package-lock.json b/package-lock.json index e3e06895..0794ff97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "labeler", "version": "3.0.2", "license": "MIT", "dependencies": { diff --git a/src/labeler.ts b/src/labeler.ts index 301f9313..675e1d91 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -216,10 +216,10 @@ function checkAll(changedFiles: string[], globs: string[]): boolean { function checkBranch(glob: string): boolean { const matcher = new Minimatch(glob); - const branchName = github.context.ref; + const branchName = github.context.payload.pull_request?.head.ref; core.debug(` checking "branch" pattern against ${branchName}`); core.debug(` - ${printPattern(matcher)}`); - if (!matcher.match(branchName)) { + if (branchName || !matcher.match(branchName)) { core.debug(` ${printPattern(matcher)} did not match`); return false; }