Skip to content

Commit

Permalink
Merge pull request #37 from athieriot/negative-matching
Browse files Browse the repository at this point in the history
Allow negative patterns to be used in an array
  • Loading branch information
TimonVS committed Nov 8, 2021
2 parents 06f3e74 + 8625492 commit 449c7ed
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -36,7 +36,6 @@ For example:
feature: ['feature/*', 'feat/*']
fix: fix/*
chore: chore/*
fixed-branch: fixed-branch-name
```

Then if a pull request is opened with the branch name `feature/218-add-emoji-support` the Action will automatically apply the `feature` label.
Expand Down
16 changes: 16 additions & 0 deletions __tests__/action.test.ts
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
@@ -1,4 +1,5 @@
'🎉 feature': ['feature/*', 'feat/*']
fix: fix/*
chore: chore/*
release: release/*
release: ['release/*', 'hotfix/*', '!release/skip-*']
skip-release: release/skip-*
31 changes: 15 additions & 16 deletions src/action.ts
Expand Up @@ -3,11 +3,12 @@ import * as github from '@actions/github'
import { Context } from '@actions/github/lib/context'
import matcher from 'matcher'
import getConfig, { Config } from './utils/config'
import { arrayify } from './utils/arrayify'

const defaultConfig = {
feature: ['feature/*', 'feat/*'],
fix: 'fix/*',
chore: 'chore/*'
chore: 'chore/*',
}

async function action(context: Context = github.context) {
Expand All @@ -30,7 +31,7 @@ async function action(context: Context = github.context) {
await octokit.issues.addLabels({
...context.repo,
number: context.payload.pull_request.number,
labels: labelsToAdd
labels: labelsToAdd,
})
}
} catch (error) {
Expand All @@ -44,20 +45,18 @@ async function action(context: Context = github.context) {
}

function getLabelsToAdd(config: Config, branchName: string): string[] {
return Object.entries(config).reduce(
(labels, [label, patterns]) => {
if (
Array.isArray(patterns)
? patterns.some(pattern => matcher.isMatch(branchName, pattern))
: matcher.isMatch(branchName, patterns)
) {
labels.push(label)
}

return labels
},
[] as string[]
)
const labelsToAdd: string[] = []

for (const label in config) {
const patterns = arrayify(config[label])
const matches = matcher([branchName], patterns)

if (matches.length > 0) {
labelsToAdd.push(label)
}
}

return labelsToAdd
}

export default action
3 changes: 3 additions & 0 deletions src/utils/arrayify.ts
@@ -0,0 +1,3 @@
export function arrayify<T>(x: T | T[]): T[] {
return Array.isArray(x) ? x : [x]
}

0 comments on commit 449c7ed

Please sign in to comment.