Skip to content

Commit

Permalink
feat: sync labels
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh committed Jan 18, 2021
1 parent 006ed4e commit 0a7ccef
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 125 deletions.
6 changes: 6 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ labels:
commits: "^docs:.*"

- label: "test:labeler"
sync: true
matcher:
files: ".github/labeler.yml"

- label: "test:dist"
sync: true
matcher:
files: ["dist/*"]

- label: "test:tests"
sync: true
matcher:
files: [ "__tests__/matcher/**", "__tests__/*.ts" ]

Expand All @@ -50,20 +53,23 @@ labels:
comment: "/stale"

- label: "test: s"
sync: true
matcher:
files:
count:
gte: 1
lte: 4

- label: "test: m"
sync: true
matcher:
files:
count:
gte: 5
lte: 10

- label: "test: l"
sync: true
matcher:
files:
count:
Expand Down
92 changes: 7 additions & 85 deletions __tests__/checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as github from '@actions/github'
import {GitHub} from '@actions/github/lib/utils'
import {getConfig} from '../src/config'
import * as fs from 'fs'
import {checks, is, joined, StatusCheck} from '../src/checks'
import {checks, is, StatusCheck} from '../src/checks'

const client: InstanceType<typeof GitHub> = {
repos: {
Expand All @@ -29,64 +29,6 @@ async function runChecks(
return checks(client, config, labels)
}

describe('joined', () => {
it('lhs empty should join', function () {
github.context.payload = {
pull_request: {
number: 1,
labels: []
}
}

const labels = ['a', 'b', 'c']
expect(joined(labels)).toEqual(labels)
})

it('rhs empty should join', function () {
const labels = ['a', 'b', 'c']
github.context.payload = {
pull_request: {
number: 1,
labels: labels.map(value => {
return {name: value}
})
}
}

expect(joined([])).toEqual(labels)
})

it('non empty should join', function () {
const lhs = ['a', 'b', 'c']
const rhs = ['d', 'e', 'f']
github.context.payload = {
pull_request: {
number: 1,
labels: lhs.map(value => {
return {name: value}
})
}
}

expect(joined(rhs).sort()).toEqual([...lhs, ...rhs].sort())
})

it('should dedupe', function () {
const lhs = ['a', 'b', 'c']
const rhs = ['c', 'b', 'f']
github.context.payload = {
pull_request: {
number: 1,
labels: lhs.map(value => {
return {name: value}
})
}
}

expect(joined(rhs).sort()).toEqual(['a', 'b', 'c', 'f'].sort())
})
})

describe('is', () => {
it('should empty true', function () {
expect(
Expand Down Expand Up @@ -443,17 +385,9 @@ describe('checks', () => {
})

it('feat: success state', async function () {
github.context.payload = {
pull_request: {
number: 1,
labels: [{name: 'feat'}]
}
}

const checks = await runChecks(
'__tests__/fixtures/semantic-release.yml',
[]
)
const checks = await runChecks('__tests__/fixtures/semantic-release.yml', [
'feat'
])

expect(checks.length).toBe(1)
expect(checks[0].context).toBe('Semantic Pull Request')
Expand All @@ -465,13 +399,6 @@ describe('checks', () => {
})

it('chore: success state', async function () {
github.context.payload = {
pull_request: {
number: 1,
labels: [{name: 'help'}]
}
}

const checks = await runChecks('__tests__/fixtures/semantic-release.yml', [
'chore'
])
Expand Down Expand Up @@ -500,15 +427,10 @@ describe('checks', () => {
})

it('fix: success state', async function () {
github.context.payload = {
pull_request: {
number: 1,
labels: [{name: 'bug'}, {name: 'fix'}]
}
}

const checks = await runChecks('__tests__/fixtures/semantic-release.yml', [
'none'
'none',
'bug',
'fix'
])

expect(checks.length).toBe(1)
Expand Down
157 changes: 155 additions & 2 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {labels} from '../src/labeler'
import {labels, mergeLabels} from '../src/labeler'
import {GitHub} from '@actions/github/lib/utils'
import {getConfig} from '../src/config'
import {Config, getConfig} from '../src/config'
import * as fs from 'fs'

const client: InstanceType<typeof GitHub> = {
Expand Down Expand Up @@ -153,3 +153,156 @@ describe('labeler', () => {
})
})
})

describe('mergeLabels, empty config', () => {
const config: Config = {
version: 'v1'
}

it('lhs empty should join', function () {
github.context.payload = {
pull_request: {
number: 1,
labels: []
}
}

const labels = ['a', 'b', 'c']
expect(mergeLabels(labels, config)).toEqual(labels)
})

it('rhs empty should join', function () {
const labels = ['a', 'b', 'c']
github.context.payload = {
pull_request: {
number: 1,
labels: labels.map(value => {
return {name: value}
})
}
}

expect(mergeLabels([], config)).toEqual(labels)
})

it('non empty should join', function () {
const lhs = ['a', 'b', 'c']
const rhs = ['d', 'e', 'f']
github.context.payload = {
pull_request: {
number: 1,
labels: lhs.map(value => {
return {name: value}
})
}
}

expect(mergeLabels(rhs, config).sort()).toEqual([...lhs, ...rhs].sort())
})

it('should dedupe', function () {
const lhs = ['a', 'b', 'c']
const rhs = ['c', 'b', 'f']
github.context.payload = {
pull_request: {
number: 1,
labels: lhs.map(value => {
return {name: value}
})
}
}

expect(mergeLabels(rhs, config).sort()).toEqual(['a', 'b', 'c', 'f'].sort())
})
})

describe('mergeLabels, sync config', () => {
const config: Config = {
version: 'v1',
labels: [
{
label: 'sync1',
sync: true,
matcher: {}
},
{
label: 'sync2',
sync: true,
matcher: {}
},
{
label: 'no-sync1',
sync: false,
matcher: {}
},
{
label: 'no-sync2',
matcher: {}
}
]
}

it('lhs empty should join', function () {
github.context.payload = {
pull_request: {
number: 1,
labels: [
{
name: 'no-sync1'
},
{
name: 'sync1'
}
]
}
}

const labels = ['a', 'b', 'c', 'no-sync1']
expect(mergeLabels(labels, config).sort()).toEqual(labels.sort())
})

it('rhs empty should join', function () {
github.context.payload = {
pull_request: {
number: 1,
labels: ['a', 'b', 'c', 'no-sync2', 'sync2'].map(value => {
return {name: value}
})
}
}

expect(mergeLabels([], config).sort()).toEqual(
['a', 'b', 'c', 'no-sync2'].sort()
)
})

it('non empty should join', function () {
github.context.payload = {
pull_request: {
number: 1,
labels: ['a', 'b', 'c', 'no-sync1', 'sync1'].map(value => {
return {name: value}
})
}
}

expect(mergeLabels(['d', 'e', 'f'], config).sort()).toEqual(
['a', 'b', 'c', 'd', 'e', 'f', 'no-sync1'].sort()
)
})

it('should dedupe', function () {
github.context.payload = {
pull_request: {
number: 1,
labels: ['a', 'b', 'c', 'no-sync2', 'sync2'].map(value => {
return {name: value}
})
}
}

expect(mergeLabels(['c', 'b', 'f'], config).sort()).toEqual(
['a', 'b', 'c', 'f', 'no-sync2'].sort()
)
})
})
Loading

0 comments on commit 0a7ccef

Please sign in to comment.