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

Refactor to use @actions/core instead of deprecated actions-toolkit #16

Merged
merged 8 commits into from
Oct 7, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"printWidth": 120,
"semi": false,
"singleQuote": true
}
31 changes: 0 additions & 31 deletions Dockerfile

This file was deleted.

83 changes: 27 additions & 56 deletions index.test.js → __tests__/action.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
const nock = require('nock')
const fs = require('fs')
const { Toolkit } = require('actions-toolkit')
const action = require('../src/action')

nock.disableNetConnect()

describe('branch-autolabeler', () => {
let action, tools

// Mock Toolkit.run to define `action` so we can call it
Toolkit.run = jest.fn(actionFn => {
action = actionFn
})
// Load up our entrypoint file
require('.')

beforeEach(() => {
// Create a new Toolkit instance
tools = new Toolkit()
// Mock methods on it!
tools.exit.success = jest.fn()
})

describe('pr-labeler-action', () => {
it('adds the "fix" label for "fix/510-logging" branch', async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=fix%2F510-logging')
.reply(200, configFixture())
.post('/repos/Codertocat/Hello-World/issues/1/labels', body => {
expect(body).toMatchObject({
Expand All @@ -33,16 +17,15 @@ describe('branch-autolabeler', () => {
})
.reply(200)

tools.context.payload = pullRequestOpenedFixture({ ref: 'fix/510-logging' })

await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(2)
await action({
payload: pullRequestOpenedFixture({ ref: 'fix/510-logging' })
})
expect.assertions(1)
})

it('adds the "feature" label for "feature/sign-in-page/101" branch', async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=feature%2Fsign-in-page%2F101')
.reply(200, configFixture())
.post('/repos/Codertocat/Hello-World/issues/1/labels', body => {
expect(body).toMatchObject({
Expand All @@ -52,18 +35,15 @@ describe('branch-autolabeler', () => {
})
.reply(200)

tools.context.payload = pullRequestOpenedFixture({
ref: 'feature/sign-in-page/101'
await action({
payload: pullRequestOpenedFixture({ ref: 'feature/sign-in-page/101' })
})

await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(2)
expect.assertions(1)
})

it('uses the default config when no config was provided', async () => {
nock('https://api.github.com')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=fix%2F510-logging')
.reply(404)
.post('/repos/Codertocat/Hello-World/issues/1/labels', body => {
expect(body).toMatchObject({
Expand All @@ -73,26 +53,24 @@ describe('branch-autolabeler', () => {
})
.reply(200)

tools.context.payload = pullRequestOpenedFixture({ ref: 'fix/510-logging' })

await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
expect.assertions(2)
await action({
payload: pullRequestOpenedFixture({ ref: 'fix/510-logging' })
})
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')
.get('/repos/Codertocat/Hello-World/contents/.github/pr-labeler.yml?ref=hello_world')
.reply(200, configFixture())
.post('/repos/Codertocat/Hello-World/issues/1/labels', body => {
throw new Error("Shouldn't edit labels")
})
.reply(200)

tools.context.payload = pullRequestOpenedFixture({ ref: 'hello_world' })

await action(tools)
expect(tools.exit.success).toHaveBeenCalled()
await action({
payload: pullRequestOpenedFixture({ ref: 'hello_world' })
})
})
})

Expand All @@ -109,21 +87,14 @@ function configFixture(fileName = 'config.yml') {
path: `.github/${fileName}`,
content: encodeContent(fs.readFileSync(`./fixtures/${fileName}`)),
sha: '3d21ec53a331a6f037a91c368710b99387d012c1',
url:
'https://api.github.com/repos/octokit/octokit.rb/contents/.github/release-drafter.yml',
git_url:
'https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
html_url:
'https://github.com/octokit/octokit.rb/blob/master/.github/release-drafter.yml',
download_url:
'https://raw.githubusercontent.com/octokit/octokit.rb/master/.github/release-drafter.yml',
url: 'https://api.github.com/repos/octokit/octokit.rb/contents/.github/release-drafter.yml',
git_url: 'https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
html_url: 'https://github.com/octokit/octokit.rb/blob/master/.github/release-drafter.yml',
download_url: 'https://raw.githubusercontent.com/octokit/octokit.rb/master/.github/release-drafter.yml',
_links: {
git:
'https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
self:
'https://api.github.com/repos/octokit/octokit.rb/contents/.github/release-drafter.yml',
html:
'https://github.com/octokit/octokit.rb/blob/master/.github/release-drafter.yml'
git: 'https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1',
self: 'https://api.github.com/repos/octokit/octokit.rb/contents/.github/release-drafter.yml',
html: 'https://github.com/octokit/octokit.rb/blob/master/.github/release-drafter.yml'
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ branding:
icon: 'tag'
color: 'white'
runs:
using: 'docker'
image: 'Dockerfile'
using: 'node12'
main: 'index.js'
67 changes: 22 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,27 @@
const { Toolkit } = require('actions-toolkit')
const getConfig = require('./utils/config')
const matcher = require('matcher')
const distPath = './dist/index.js'
const errorMessage = `ERROR: since pr-labeler-action v3, binding to the master version is no longer supported. Please update .github/workflows/pr-labeler.yml to bind to a major version.

const CONFIG_FILENAME = 'pr-labeler.yml'
const defaults = {
feature: ['feature/*', 'feat/*'],
fix: 'fix/*',
chore: 'chore/*'
}

Toolkit.run(
async tools => {
const repoInfo = {
owner: tools.context.payload.repository.owner.login,
repo: tools.context.payload.repository.name
}
const ref = tools.context.payload.pull_request.head.ref
const config = {
...defaults,
...(await getConfig(tools.github, CONFIG_FILENAME, repoInfo, ref))
}
You can do so by changing:
- uses: TimonVS/pr-labeler-action@master

const labelsToAdd = Object.entries(config).reduce(
(labels, [label, patterns]) => {
if (
Array.isArray(patterns)
? patterns.some(pattern => matcher.isMatch(ref, pattern))
: matcher.isMatch(ref, patterns)
) {
labels.push(label)
}
To:
- uses: TimonVS/pr-labeler-action@v3

return labels
},
[]
)
Please check the repository (https://github.com/TimonVS/pr-labeler-action) for the latest version.
`

if (labelsToAdd.length > 0) {
await tools.github.issues.addLabels({
number: tools.context.payload.pull_request.number,
labels: labelsToAdd,
...repoInfo
})
}
try {
require(distPath)
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND') {
// Re-throw not "Module not found" errors
throw error
}
if (error.message.indexOf(distPath) === -1) {
// Re-throw not found errors for other modules
throw error
}

tools.exit.success()
},
{ event: 'pull_request.opened', secrets: ['GITHUB_TOKEN'] }
)
console.error(errorMessage)
throw error
}
Loading