From 827e1186a24701f6290172dca500adb71181647a Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 15 Sep 2021 09:05:10 -0800 Subject: [PATCH 1/7] Fix PR branch labeler --- src/labeler.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/labeler.ts b/src/labeler.ts index 301f9313..74034f59 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -218,6 +218,7 @@ function checkBranch(glob: string): boolean { const matcher = new Minimatch(glob); const branchName = github.context.ref; core.debug(` checking "branch" pattern against ${branchName}`); + core.debug(` - but we should be checking ${github.context.payload.pull_request.head.ref}`); core.debug(` - ${printPattern(matcher)}`); if (!matcher.match(branchName)) { core.debug(` ${printPattern(matcher)} did not match`); From 8aa7614bc1641ea54bf1d9db34d3d03c8d144e0a Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 15 Sep 2021 09:16:56 -0800 Subject: [PATCH 2/7] Run build --- dist/index.js | 2 ++ package-lock.json | 1 + src/labeler.ts | 3 ++- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index 8f51264c..a3907fbe 100644 --- a/dist/index.js +++ b/dist/index.js @@ -206,7 +206,9 @@ function checkAll(changedFiles, globs) { function checkBranch(glob) { const matcher = new minimatch_1.Minimatch(glob); const branchName = github.context.ref; + const branchName2 = github.context.payload.pull_request.head.ref; core.debug(` checking "branch" pattern against ${branchName}`); + core.debug(` - but we should be checking ${branchName2}`); core.debug(` - ${printPattern(matcher)}`); if (!matcher.match(branchName)) { core.debug(` ${printPattern(matcher)} did not match`); 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 74034f59..a6cf5eaa 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -217,8 +217,9 @@ function checkAll(changedFiles: string[], globs: string[]): boolean { function checkBranch(glob: string): boolean { const matcher = new Minimatch(glob); const branchName = github.context.ref; + const branchName2 = github.context.payload.pull_request!.head.ref; core.debug(` checking "branch" pattern against ${branchName}`); - core.debug(` - but we should be checking ${github.context.payload.pull_request.head.ref}`); + core.debug(` - but we should be checking ${branchName2}`); core.debug(` - ${printPattern(matcher)}`); if (!matcher.match(branchName)) { core.debug(` ${printPattern(matcher)} did not match`); From cb5f4480b27463774f930d1a0708580fa778272c Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 15 Sep 2021 09:23:41 -0800 Subject: [PATCH 3/7] Use correct branch name and update tests --- __tests__/main.test.ts | 4 ++-- src/labeler.ts | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 1c89ea8f..5a0990ef 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,7 @@ 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(); diff --git a/src/labeler.ts b/src/labeler.ts index a6cf5eaa..93eeef7a 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -216,10 +216,8 @@ function checkAll(changedFiles: string[], globs: string[]): boolean { function checkBranch(glob: string): boolean { const matcher = new Minimatch(glob); - const branchName = github.context.ref; - const branchName2 = github.context.payload.pull_request!.head.ref; + const branchName = github.context.payload.pull_request!.head.ref; core.debug(` checking "branch" pattern against ${branchName}`); - core.debug(` - but we should be checking ${branchName2}`); core.debug(` - ${printPattern(matcher)}`); if (!matcher.match(branchName)) { core.debug(` ${printPattern(matcher)} did not match`); From 2ced1f3255c478d1031e446533020a71011d4b9b Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 15 Sep 2021 09:24:05 -0800 Subject: [PATCH 4/7] Run build --- dist/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index a3907fbe..abbfb3a8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -205,10 +205,8 @@ function checkAll(changedFiles, globs) { } function checkBranch(glob) { const matcher = new minimatch_1.Minimatch(glob); - const branchName = github.context.ref; - const branchName2 = github.context.payload.pull_request.head.ref; + const branchName = github.context.payload.pull_request.head.ref; core.debug(` checking "branch" pattern against ${branchName}`); - core.debug(` - but we should be checking ${branchName2}`); core.debug(` - ${printPattern(matcher)}`); if (!matcher.match(branchName)) { core.debug(` ${printPattern(matcher)} did not match`); From d9ed3e87f696bd7249261358a1d8be66e6bb0a2c Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 15 Sep 2021 09:58:18 -0800 Subject: [PATCH 5/7] Format --- __tests__/main.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 5a0990ef..1c61fc14 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.payload.pull_request!.head = {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.payload.pull_request!.head = {ref: "my/feature/that-i-like"}; + github.context.payload.pull_request!.head = { + ref: "my/feature/that-i-like", + }; usingLabelerConfigYaml("branches.yml"); await run(); From 79c0cc7c0bec2419725a656f8314f11c21899821 Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Wed, 15 Sep 2021 10:14:24 -0800 Subject: [PATCH 6/7] Include a test for branching --- __tests__/fixtures/branches.yml | 3 +++ __tests__/main.test.ts | 14 ++++++++++++++ 2 files changed, 17 insertions(+) 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 1c61fc14..85e85f4c 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -133,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 { From 27a1d89941b6f2a19e52a84db599a5bc6c58104d Mon Sep 17 00:00:00 2001 From: Amiel Martin Date: Thu, 16 Sep 2021 12:22:00 -0800 Subject: [PATCH 7/7] Update src/labeler.ts Safer access of pull_request data. Co-authored-by: Josh Dales <30500472+joshdales@users.noreply.github.com> --- src/labeler.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/labeler.ts b/src/labeler.ts index 93eeef7a..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.payload.pull_request!.head.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; }