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

Allow matching on the base branch #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ typings/
.dynamodb/

# End of https://www.gitignore.io/api/node

# build output
dist
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,35 @@ Then if a pull request is opened with the branch name `feature/218-add-emoji-sup

You can use `*` as a wildcard for matching multiple branch names. See https://www.npmjs.com/package/matcher for more information about wildcard options.

### Matching on the base branch

By default, labeler will match specified patterns againts the head branch. If you want to match against the base branch instead, you can do so by using the `base` option.

```yml
website:
base: gh-pages
```

With the above configuration, if a pull request targeting the `gh-pages` branch is opened, the Action will automatically apply the `website` label regardless of its head branch name.

#### Matching on both the head and base branches

When both `head` and `base` options are specified, they will be combined in an "AND" statement. Meaning that the pull request needs to satisfy both head and base patterns in order to be attributed the label.

```yml
fix:
base: master
head: fix/*
hot-fix:
base: release/*
head: fix/*
```

Suppose the above configuration and a pull request whose head branch name is `fix/510-logging`:

- If the PR is opened with `master` as target, the Action will add the "fix" label to it.
- If the PR is opened with `release/1.2.3` as target, the Action will add the "hot-fix" label to it.

### Default configuration

When no configuration is provided, the following defaults will be used:
Expand Down
53 changes: 44 additions & 9 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('pr-labeler-action', () => {
setupEnvironmentVariables();
});

it('adds the "fix" label for "fix/510-logging" branch', async () => {
it('adds the "fix" label to a PR that adds a fix in the master branch', async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=fix%2F510-logging')
.reply(200, configFixture())
Expand All @@ -24,7 +24,23 @@ describe('pr-labeler-action', () => {
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'fix/510-logging' })));
await action(new MockContext(pullRequestOpenedFixture({ head: 'fix/510-logging', base: 'master' })));
expect.assertions(1);
});

it('adds the "hot-fix" label to a PR that adds a fix in a release branch', async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=fix%2F510-logging')
.reply(200, configFixture())
.post('/repos/Codertocat/Hello-World/issues/1/labels', (body) => {
expect(body).toMatchObject({
labels: ['hot-fix'],
});
return true;
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ head: 'fix/510-logging', base: 'release/2.0' })));
expect.assertions(1);
});

Expand All @@ -40,7 +56,7 @@ describe('pr-labeler-action', () => {
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'feature/sign-in-page/101' })));
await action(new MockContext(pullRequestOpenedFixture({ head: 'feature/sign-in-page/101', base: 'master' })));
expect.assertions(1);
});

Expand All @@ -56,7 +72,7 @@ describe('pr-labeler-action', () => {
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'release/2.0' })));
await action(new MockContext(pullRequestOpenedFixture({ head: 'release/2.0', base: 'master' })));
expect.assertions(1);
});

Expand All @@ -72,7 +88,7 @@ describe('pr-labeler-action', () => {
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'fix/510-logging' })));
await action(new MockContext(pullRequestOpenedFixture({ head: 'fix/510-logging', base: 'master' })));
expect.assertions(1);
});

Expand All @@ -88,7 +104,23 @@ describe('pr-labeler-action', () => {
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'release/skip-this-one' })));
await action(new MockContext(pullRequestOpenedFixture({ head: 'release/skip-this-one', base: 'master' })));
expect.assertions(1);
});

it('adds the "website" label to any PR that targets the "gh-pages" branch', async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=any-branch-name')
.reply(200, configFixture())
.post('/repos/Codertocat/Hello-World/issues/1/labels', (body) => {
expect(body).toMatchObject({
labels: ['website'],
});
return true;
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ head: 'any-branch-name', base: 'gh-pages' })));
expect.assertions(1);
});

Expand All @@ -101,7 +133,7 @@ describe('pr-labeler-action', () => {
})
.reply(200);

await action(new MockContext(pullRequestOpenedFixture({ ref: 'hello_world' })));
await action(new MockContext(pullRequestOpenedFixture({ head: 'hello_world', base: 'master' })));
});
});

Expand Down Expand Up @@ -137,12 +169,15 @@ function configFixture(fileName = 'config.yml') {
};
}

function pullRequestOpenedFixture({ ref }: { ref: string }) {
function pullRequestOpenedFixture({ head, base }: { head: string; base: string }) {
return {
pull_request: {
number: 1,
head: {
ref,
ref: head,
},
base: {
ref: base,
},
},
repository: {
Expand Down
11 changes: 9 additions & 2 deletions __tests__/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'🎉 feature': ['feature/*', 'feat/*']
fix: fix/*
chore: chore/*
release: ['release/*', 'hotfix/*', '!release/skip-*']
release: ['release/*', '!release/skip-*']
skip-release: release/skip-*
fix:
base: master
head: fix/*
hot-fix:
base: release/*
head: fix/*
website:
base: gh-pages
Loading