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 negative patterns to be used in an array #37

Merged
merged 2 commits into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]
}