Skip to content

Commit

Permalink
Allow negative patterns to be used in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien THIERIOT committed Jan 7, 2021
1 parent bba1cdc commit f5bc0f6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ 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.

### Negative matcher

You can also use `!` to add a label on all branches that does NOT match the pattern.

Used in a list of patterns, then, the branch name will have to match all patterns.

For example:

```yml
patch: ['update/*', '!update/sbt-*']
no-release: update/sbt-*
```

In this scenario, a branch named `update/sbt-native-package` will only have the `no-release` label

### Default configuration

When no configuration is provided, the following defaults will be used:
Expand Down
16 changes: 16 additions & 0 deletions __tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ describe('pr-labeler-action', () => {
expect.assertions(1)
})

it("adds only one label if the branch matches a negative pattern", async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=release%2Fskip-this-one')
.reply(200, configFixture())
.post('/repos/Codertocat/Hello-World/issues/1/labels', body => {
expect(body).toMatchObject({
labels: ['skip-release']
})
return true
})
.reply(200)

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

it("adds no labels if the branch doesn't match any patterns", async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=hello_world')
Expand Down
3 changes: 2 additions & 1 deletion __tests__/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'🎉 feature': ['feature/*', 'feat/*']
fix: fix/*
chore: chore/*
release: release/*
release: ['release/*', '!release/skip-*']
skip-release: release/skip-*
11 changes: 7 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ async function action(context: Context = github.context) {

function getLabelsToAdd(config: Config, branchName: string): string[] {
return Object.entries(config).reduce(
(labels, [label, patterns]) => {

(labels, [label, pattern]) => {
const patterns = Array.isArray(pattern) ? pattern : [pattern]

if (
Array.isArray(patterns)
? patterns.some(pattern => matcher.isMatch(branchName, pattern))
: matcher.isMatch(branchName, patterns)
patterns.some(pattern => pattern.startsWith("!"))
? patterns.every(pattern => matcher.isMatch(branchName, pattern))
: patterns.some(pattern => matcher.isMatch(branchName, pattern))
) {
labels.push(label)
}
Expand Down

0 comments on commit f5bc0f6

Please sign in to comment.