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

feat: implement support for additional labels #11

Merged
merged 2 commits into from Oct 10, 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
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@

[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)

Automatically adds labels to pull requests based on [conventionalcommits.org](https://conventionalcommits.org), for the benifit of
Automatically adds labels to pull requests based on [Conventional Commits](https://conventionalcommits.org). These labels can be used with GitHub's
[automatically generated release notes](https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes).

## Configuration
Expand Down Expand Up @@ -37,6 +37,9 @@ changelog:
- title: Exciting New Features 🎉
labels:
- feature
- title: Fixes 🔧
labels:
- fix
- title: Other Changes
labels:
- "*"
Expand Down
7 changes: 6 additions & 1 deletion action.yml
@@ -1,11 +1,16 @@
name: 'conventional-release-labels'
description: 'automatically apply release labels to PR, based on conventionalcommits.org'
author: Google LLC
author: Benjamin Coe
inputs:
token:
description: 'GitHub token for applying labels, defaults to using secrets.GITHUB_TOKEN'
required: false
default: ${{ github.token }}
type_labels:
description: 'what labels to apply to different conventional commit types'
required: false
default: '{"feat": "feature", "fix": "fix", "breaking": "breaking"}'
type: string

runs:
using: 'node12'
Expand Down
36 changes: 29 additions & 7 deletions index.js
Expand Up @@ -5,13 +5,15 @@ const { readFile } = fs.promises
const { parser } = require('@conventional-commits/parser')

const api = module.exports = {
addLabel,
addLabels,
isPullRequest,
main
main,
removeLabels
}

async function main () {
const { visit } = await import('unist-util-visit')
const labelMap = JSON.parse(core.getInput('type_labels'))
if (!process.env.GITHUB_EVENT_PATH) {
console.warn('no event payload found')
return
Expand All @@ -22,7 +24,13 @@ async function main () {
if (!api.isPullRequest(payload)) {
console.info('skipping non pull_request')
}
const titleAst = parser(payload.pull_request.title)
let titleAst
try {
titleAst = parser(payload.pull_request.title)
} catch (err) {
console.warn(err.message)
return
}
const cc = {
breaking: false
}
Expand All @@ -43,22 +51,36 @@ async function main () {
}
})

if (cc.type === 'feat') {
await api.addLabel(cc.type, payload)
const labels = []
if (cc.breaking) labels.push(labelMap.breaking)
if (labelMap[cc.type]) labels.push(labelMap[cc.type])
if (labels.length) {
await api.removeLabels(Object.values(labelMap), payload)
await api.addLabels(labels, payload)
}
}

function isPullRequest (payload) {
return !!payload.pull_request
}

async function addLabel (type, payload) {
async function addLabels (labels, payload) {
const octokit = getOctokit()
await octokit.rest.issues.addLabels({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
labels: ['feature']
labels
})
}

async function removeLabels (labels, payload) {
const octokit = getOctokit()
await octokit.rest.issues.removeLabels({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.pull_request.number,
labels
})
}

Expand Down
15 changes: 12 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -33,6 +33,7 @@
"devDependencies": {
"@vercel/ncc": "^0.31.1",
"c8": "^7.5.0",
"js-yaml": "^4.1.0",
"mocha": "^8.2.1",
"sinon": "^11.1.2",
"standard": "^16.0.4"
Expand Down
35 changes: 30 additions & 5 deletions test/conventional-release-labels.js
@@ -1,19 +1,44 @@
const { describe, it, afterEach } = require('mocha')
const { beforeEach, describe, it, afterEach } = require('mocha')
const api = require('../index.js')
// import * as assert from 'assert'
// import * as core from '@actions/core'
const core = require('@actions/core')
const fs = require('fs')
const sinon = require('sinon')
const yaml = require('js-yaml')

const sandbox = sinon.createSandbox()
process.env.GITHUB_EVENT_PATH = process.env.GITHUB_EVENT_PATH || ''

const metadata = yaml.load(fs.readFileSync('./action.yml', 'utf8'))

describe('conventional-release-labels', () => {
beforeEach(() => {
sandbox.replace(core, 'getInput', (key) => {
return metadata.inputs[key].default
})
})
afterEach(() => {
sandbox.restore()
})
it('handles unconventional commit', async () => {
const addLabels = sandbox.stub(api, 'addLabels').resolves(undefined)
sandbox.stub(process.env, 'GITHUB_EVENT_PATH').value('./test/fixtures/unconventional.json')
await api.main()
sandbox.assert.notCalled(addLabels)
})
it('it adds feature label', async () => {
sandbox.stub(api, 'addLabel').resolves(undefined)
sandbox.stub(process.env, 'GITHUB_EVENT_PATH').value('./test/fixtures/feature-pr.json')
const addLabels = sandbox.stub(api, 'addLabels').resolves(undefined)
const removeLabels = sandbox.stub(api, 'removeLabels').resolves(undefined)
sandbox.stub(process.env, 'GITHUB_EVENT_PATH').value('./test/fixtures/feature.json')
await api.main()
sandbox.assert.calledWith(removeLabels, ['feature', 'fix', 'breaking'], sandbox.match.any)
sandbox.assert.calledWith(addLabels, ['feature'], sandbox.match.any)
})
it('it adds breaking label along with type', async () => {
const addLabels = sandbox.stub(api, 'addLabels').resolves(undefined)
const removeLabels = sandbox.stub(api, 'removeLabels').resolves(undefined)
sandbox.stub(process.env, 'GITHUB_EVENT_PATH').value('./test/fixtures/breaking-fix.json')
await api.main()
sandbox.assert.calledWith(removeLabels, ['feature', 'fix', 'breaking'], sandbox.match.any)
sandbox.assert.calledWith(addLabels, ['breaking', 'fix'], sandbox.match.any)
})
})