Skip to content

Commit

Permalink
chore: refactor to add new features (#34)
Browse files Browse the repository at this point in the history
* chore: refactor to add new features

* removed semantic pull request

* refactor to use Promise.all for better performance
  • Loading branch information
fuxingloh authored Jan 16, 2021
1 parent 5cb039d commit ef9c07c
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 299 deletions.
13 changes: 13 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ labels:
files:
count:
gte: 11

checks:
- context: "Semantic Pull Request"
url: "https://github.com/fuxingloh/multi-labeler/blob/main/.github/labeler.yml"
description:
success: Ready for review & merge.
failure: Missing semantic label for merge.
condition:
any:
- feat
- fix
- chore
- docs
37 changes: 1 addition & 36 deletions .github/workflows/ci-use.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: CI

on:
pull_request:
types: [ opened, reopened, synchronize, ready_for_review, labeled, unlabeled ]
types: [ opened, reopened, edited, synchronize, ready_for_review, labeled, unlabeled ]
issues:
issue_comment:

Expand All @@ -11,40 +11,5 @@ jobs:
name: Uses
runs-on: ubuntu-latest
steps:
- if: github.event_name == 'pull_request'
uses: actions/github-script@v3
with:
debug: true
script: |
const sha = context.payload.pull_request.head.sha
const response = await github.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: sha,
state: 'pending',
target_url: 'https://github.com/fuxingloh/multi-labeler/blob/main/.github/workflows/ci-use.yml',
context: 'Semantic Pull Request'
})
- uses: actions/checkout@v2

- uses: ./

- if: github.event_name == 'pull_request'
uses: actions/github-script@v3
with:
debug: true
script: |
const success = context.payload.pull_request.labels
.some(label => ['feat', 'fix', 'chore', 'docs'].includes(label.name))
const sha = context.payload.pull_request.head.sha
await github.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: sha,
state: success ? 'success' : 'failure',
description: success ? 'Ready for review & merge.' : 'PR title validation failed.',
target_url: 'https://github.com/fuxingloh/multi-labeler/blob/main/.github/workflows/ci-use.yml',
context: 'Semantic Pull Request'
})
4 changes: 4 additions & 0 deletions __tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ describe('valid config', () => {
it('basic.yml', function () {
expect(() => parseConfig('basic.yml')).not.toThrowError()
})

it('empty.yml', function () {
expect(() => parseConfig('empty.yml')).not.toThrowError()
})
})

describe('invalid config', () => {
Expand Down
138 changes: 54 additions & 84 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,60 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {run} from '../src/labeler'
import {labels} from '../src/labeler'
import {GitHub} from '@actions/github/lib/utils'
import {getConfig} from '../src/config'
import * as fs from 'fs'

let labels: string[] = []

describe('main core and context', () => {
const client: InstanceType<typeof GitHub> = {
repos: {
// @ts-ignore
getContent(params) {
if (params?.path) {
return {
data: {
content: fs.readFileSync(params.path, 'utf8'),
encoding: 'utf-8'
}
}
}
}
},
pulls: {
listCommits: {
endpoint: {
// @ts-ignore
merge() {
return {}
}
}
},
listFiles: {
endpoint: {
// @ts-ignore
merge() {
return {}
}
}
}
},
// @ts-ignore
paginate(params): Promise<any[]> {
return Promise.resolve([])
}
}

async function runLabels(configPath: string): Promise<string[]> {
const config = await getConfig(client, configPath)
return labels(client, config)
}

describe('labeler', () => {
beforeEach(() => {
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
jest.spyOn(core, 'info').mockImplementation(jest.fn())
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
jest.spyOn(core, 'setFailed').mockImplementation(jest.fn())

// @ts-ignore, mock only required github client features
jest.spyOn(github, 'getOctokit').mockImplementation((token: string) => {
return {
issues: {
addLabels(params) {
labels.push(...(params?.labels || []))
}
},
repos: {
getContent(params) {
if (params?.path) {
return {
data: {
content: fs.readFileSync(params.path, 'utf8'),
encoding: 'utf-8'
}
}
}
}
},
pulls: {
listCommits: {
endpoint: {
// @ts-ignore
merge() {
return {}
}
}
},
listFiles: {
endpoint: {
// @ts-ignore
merge() {
return {}
}
}
}
},
// @ts-ignore
paginate(params): Promise<any[]> {
return Promise.resolve([])
}
}
})

// Mock github context
jest.spyOn(github.context, 'repo', 'get').mockImplementation(() => {
return {
owner: 'owner-name',
Expand All @@ -74,35 +71,12 @@ describe('main core and context', () => {
}
})

afterEach(() => {
labels = []
})

afterAll(() => {
jest.restoreAllMocks()
})

it('should require token, error exit without', async function () {
jest.spyOn(github, 'getOctokit').mockRestore()

await expect(async () => {
// @ts-ignore
await run(undefined, '.github/labeler.yml')
}).rejects.toThrow('Parameter token or opts.auth is required')
})

it('should be able to load default config', async function () {
await run('token', '.github/labeler.yml')
})

it('no issue or pr should fail', async function () {
github.context.payload = {}

await expect(async () => {
await run('token', '.github/labeler.yml')
}).rejects.toThrow(
'Could not get issue_number from pull_request or issue from context'
)
await runLabels('.github/labeler.yml')
})

it('empty should not fail', async function () {
Expand All @@ -114,14 +88,10 @@ describe('main core and context', () => {
}
}

await run('token', '__tests__/fixtures/empty.yml')
await runLabels('__tests__/fixtures/empty.yml')
})

describe('basic.yml', () => {
beforeEach(async () => {
labels = []
})

it('should be empty', async function () {
github.context.payload = {
pull_request: {
Expand All @@ -130,7 +100,7 @@ describe('main core and context', () => {
}
}

await run('token', '__tests__/fixtures/basic.yml')
const labels = await runLabels('__tests__/fixtures/empty.yml')
expect(labels).toEqual([])
})

Expand All @@ -142,7 +112,7 @@ describe('main core and context', () => {
}
}

await run('token', '__tests__/fixtures/basic.yml')
const labels = await runLabels('__tests__/fixtures/basic.yml')
expect(labels).toEqual(['feat'])
})

Expand All @@ -154,7 +124,7 @@ describe('main core and context', () => {
}
}

await run('token', '__tests__/fixtures/basic.yml')
const labels = await runLabels('__tests__/fixtures/basic.yml')
expect(labels).toEqual(['chore'])
})

Expand All @@ -166,7 +136,7 @@ describe('main core and context', () => {
}
}

await run('token', '__tests__/fixtures/basic.yml')
const labels = await runLabels('__tests__/fixtures/basic.yml')
expect(labels).toEqual(['fix'])
})

Expand All @@ -178,7 +148,7 @@ describe('main core and context', () => {
}
}

await run('token', '__tests__/fixtures/basic.yml')
const labels = await runLabels('__tests__/fixtures/basic.yml')
expect(labels).toEqual(['docs'])
})
})
Expand Down
7 changes: 7 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import fs from 'fs'
import {parse} from '../src/config'

it('.github/labeler.yml', function () {
const content = fs.readFileSync(`.github/labeler.yml`, 'utf8')
expect(() => parse(content)).not.toThrowError()
})
Loading

0 comments on commit ef9c07c

Please sign in to comment.