Skip to content

Commit

Permalink
Merge pull request #1 from bentohq/fix-branch-labeler
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdales authored Sep 16, 2021
2 parents 765934f + 27a1d89 commit 7624214
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions __tests__/fixtures/branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ test-branch:

feature-branch:
- branch: "*/feature/*"

bug-branch:
- branch: "{bug,fix}/*"
20 changes: 18 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 7624214

Please sign in to comment.