From 73531c33e567bbd4e1f53e6d5ebb3eb323c583e8 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Wed, 2 Oct 2019 19:49:03 +0200 Subject: [PATCH 1/8] Add Prettier config --- .prettierrc | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..cbe842ac --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "printWidth": 120, + "semi": false, + "singleQuote": true +} From 30be182cc64da88767fb86225b2e8f026b1fbdba Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Wed, 2 Oct 2019 19:50:15 +0200 Subject: [PATCH 2/8] Refactor Action to use @actions/core and @actions/github instead of deprecated actions-toolkit --- action.js | 66 +++++++++ index.test.js => action.test.js | 83 ++++------- index.js | 51 +------ package-lock.json | 244 +++++++++++++------------------- package.json | 5 +- utils/config.js | 2 +- 6 files changed, 195 insertions(+), 256 deletions(-) create mode 100644 action.js rename index.test.js => action.test.js (54%) diff --git a/action.js b/action.js new file mode 100644 index 00000000..e3041b43 --- /dev/null +++ b/action.js @@ -0,0 +1,66 @@ +const core = require('@actions/core') +const github = require('@actions/github') +const matcher = require('matcher') +const getConfig = require('./utils/config') + +const CONFIG_FILENAME = 'pr-labeler.yml' +const defaults = { + feature: ['feature/*', 'feat/*'], + fix: 'fix/*', + chore: 'chore/*' +} + +async function action(context = github.context) { + try { + const GITHUB_TOKEN = process.env.GITHUB_TOKEN + const octokit = new github.GitHub(GITHUB_TOKEN) + const repoInfo = { + owner: context.payload.repository.owner.login, + repo: context.payload.repository.name + } + + if (!context.payload.pull_request) { + throw new Error( + "Payload doesn't contain `pull_request`. Make sure this Action is being triggered by a pull_request event (https://help.github.com/en/articles/events-that-trigger-workflows#pull-request-event-pull_request)." + ) + } + + const ref = context.payload.pull_request.head.ref + const config = { + ...defaults, + ...(await getConfig(octokit, CONFIG_FILENAME, repoInfo, ref)) + } + + 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) + } + + return labels + }, + [] + ) + + if (labelsToAdd.length > 0) { + await octokit.issues.addLabels({ + number: context.payload.pull_request.number, + labels: labelsToAdd, + ...repoInfo + }) + } + } catch (error) { + if (process.env.NODE_ENV === 'test') { + throw error + } + + core.error(error) + core.setFailed(error.message) + } +} + +module.exports = action diff --git a/index.test.js b/action.test.js similarity index 54% rename from index.test.js rename to action.test.js index c0dfd084..9796d2ab 100644 --- a/index.test.js +++ b/action.test.js @@ -1,29 +1,13 @@ const nock = require('nock') const fs = require('fs') -const { Toolkit } = require('actions-toolkit') +const action = require('./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({ @@ -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({ @@ -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({ @@ -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' }) + }) }) }) @@ -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' } } } diff --git a/index.js b/index.js index 97b95dfb..275a9841 100644 --- a/index.js +++ b/index.js @@ -1,50 +1,3 @@ -const { Toolkit } = require('actions-toolkit') -const getConfig = require('./utils/config') -const matcher = require('matcher') +const action = require('./action') -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)) - } - - 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) - } - - return labels - }, - [] - ) - - if (labelsToAdd.length > 0) { - await tools.github.issues.addLabels({ - number: tools.context.payload.pull_request.number, - labels: labelsToAdd, - ...repoInfo - }) - } - - tools.exit.success() - }, - { event: 'pull_request.opened', secrets: ['GITHUB_TOKEN'] } -) +action() diff --git a/package-lock.json b/package-lock.json index 4580343d..3339188a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,22 @@ { - "name": "pr-labeler", + "name": "pr-labeler-action", "requires": true, "lockfileVersion": 1, "dependencies": { + "@actions/core": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.1.3.tgz", + "integrity": "sha512-2BIib53Jh4Cfm+1XNuZYYGTeRo8yiWEAUMoliMh1qQGMaqTF4VUlhhcsBylTu4qWmUx45DrY0y0XskimAHSqhw==" + }, + "@actions/github": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@actions/github/-/github-1.1.0.tgz", + "integrity": "sha512-cHf6PyoNMdei13jEdGPhKprIMFmjVVW/dnM5/9QmQDJ1ZTaGVyezUSCUIC/ySNLRvDUpeFwPYMdThSEJldSbUw==", + "requires": { + "@octokit/graphql": "^2.0.1", + "@octokit/rest": "^16.15.0" + } + }, "@babel/code-frame": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.0.0.tgz", @@ -527,21 +541,6 @@ "integrity": "sha512-OtUw6JUTgxA2QoqqmrmQ7F2NYqiBPi/L2jqHyFtllhOUvXYQXf0Z1CYUinIfyT4bTCGmrA7gX9FvHA81uzCoVw==", "dev": true }, - "actions-toolkit": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/actions-toolkit/-/actions-toolkit-2.0.0.tgz", - "integrity": "sha512-wzQa0KkN0IUrJdl72hLV1a/oOAUpKLriETV5+/85T6sLfCaIJJQ3YUMtKDc1ctjfsdRfvYO8Qst8OOVi64boKw==", - "requires": { - "@octokit/graphql": "^2.0.1", - "@octokit/rest": "^16.15.0", - "enquirer": "^2.3.0", - "execa": "^1.0.0", - "flat-cache": "^2.0.1", - "js-yaml": "^3.13.0", - "minimist": "^1.2.0", - "signale": "^1.4.0" - } - }, "ajv": { "version": "6.10.0", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.0.tgz", @@ -554,11 +553,6 @@ "uri-js": "^4.2.2" } }, - "ansi-colors": { - "version": "3.2.4", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz", - "integrity": "sha512-hHUXGagefjN2iRrID63xckIvotOXOojhQKWIPUZ4mNUZ9nLZW+7FMNoE1lOkEhNWYsx/7ysGIuJYCiMAA9FnrA==" - }, "ansi-escapes": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-3.2.0.tgz", @@ -575,6 +569,7 @@ "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", + "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -602,6 +597,7 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "dev": true, "requires": { "sprintf-js": "~1.0.2" } @@ -767,7 +763,8 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true }, "base": { "version": "0.11.2", @@ -842,6 +839,7 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -981,6 +979,7 @@ "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", + "dev": true, "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -991,6 +990,7 @@ "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", + "dev": true, "requires": { "has-flag": "^3.0.0" } @@ -1086,6 +1086,7 @@ "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", + "dev": true, "requires": { "color-name": "1.1.3" } @@ -1093,7 +1094,8 @@ "color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", + "dev": true }, "combined-stream": { "version": "1.0.7", @@ -1126,7 +1128,8 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true }, "convert-source-map": { "version": "1.6.0", @@ -1365,18 +1368,11 @@ "once": "^1.4.0" } }, - "enquirer": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.0.tgz", - "integrity": "sha512-RNGUbRVlfnjmpxV+Ed+7CGu0rg3MK7MmlW+DW0v7V2zdAUBC1s4BxCRiIAozbYB2UJ+q4D+8tW9UFb11kF72/g==", - "requires": { - "ansi-colors": "^3.2.1" - } - }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", "integrity": "sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g==", + "dev": true, "requires": { "is-arrayish": "^0.2.1" } @@ -1409,7 +1405,8 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true }, "escodegen": { "version": "1.11.1", @@ -1435,7 +1432,8 @@ "esprima": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", - "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==" + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", + "dev": true }, "estraverse": { "version": "4.2.0", @@ -1649,14 +1647,6 @@ "bser": "^2.0.0" } }, - "figures": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/figures/-/figures-2.0.0.tgz", - "integrity": "sha1-OrGi0qYsi/tDGgyUy3l6L84nyWI=", - "requires": { - "escape-string-regexp": "^1.0.5" - } - }, "fileset": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/fileset/-/fileset-2.0.3.tgz", @@ -1699,21 +1689,6 @@ "locate-path": "^3.0.0" } }, - "flat-cache": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-2.0.1.tgz", - "integrity": "sha512-LoQe6yDuUMDzQAEH8sgmh4Md6oZnc/7PjtwjNFSzveXqSHt6ka9fPBuso7IGf9Rz4uqnSnWiFH2B/zj24a5ReA==", - "requires": { - "flatted": "^2.0.0", - "rimraf": "2.6.3", - "write": "1.0.3" - } - }, - "flatted": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-2.0.0.tgz", - "integrity": "sha512-R+H8IZclI8AAkSBRQJLVOsxwAoHd6WC40b4QTNWIjzAa6BXOBfQcM587MXDTVPeYaopFNWHUFLx7eNmHDSxMWg==" - }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -1749,7 +1724,8 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true }, "fsevents": { "version": "1.2.7", @@ -1771,7 +1747,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -1792,12 +1769,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1812,17 +1791,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -1939,7 +1921,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -1951,6 +1934,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -1965,6 +1949,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -1972,12 +1957,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -1996,6 +1983,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2076,7 +2064,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2088,6 +2077,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2173,7 +2163,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -2209,6 +2200,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2228,6 +2220,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2271,12 +2264,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -2325,6 +2320,7 @@ "version": "7.1.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", + "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -2343,7 +2339,8 @@ "graceful-fs": { "version": "4.1.15", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.15.tgz", - "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==" + "integrity": "sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==", + "dev": true }, "growly": { "version": "1.3.0", @@ -2391,7 +2388,8 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", + "dev": true }, "has-symbols": { "version": "1.0.0", @@ -2486,6 +2484,7 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -2494,7 +2493,8 @@ "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", - "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", + "dev": true }, "invariant": { "version": "2.2.4", @@ -2534,7 +2534,8 @@ "is-arrayish": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz", - "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=" + "integrity": "sha1-d8mYQFJ6qOyxqLppe4BkWnqSap0=", + "dev": true }, "is-buffer": { "version": "1.1.6", @@ -3227,6 +3228,7 @@ "version": "3.13.1", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "dev": true, "requires": { "argparse": "^1.0.7", "esprima": "^4.0.0" @@ -3281,7 +3283,8 @@ "json-parse-better-errors": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz", - "integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==" + "integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==", + "dev": true }, "json-schema": { "version": "0.2.3", @@ -3369,6 +3372,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-4.0.0.tgz", "integrity": "sha1-L19Fq5HjMhYjT9U62rZo607AmTs=", + "dev": true, "requires": { "graceful-fs": "^4.1.2", "parse-json": "^4.0.0", @@ -3550,6 +3554,7 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3557,7 +3562,8 @@ "minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", - "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", + "dev": true }, "mixin-deep": { "version": "1.3.2", @@ -3584,6 +3590,7 @@ "version": "0.5.1", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, "requires": { "minimist": "0.0.8" }, @@ -3591,7 +3598,8 @@ "minimist": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", + "dev": true } } }, @@ -3953,6 +3961,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/parse-json/-/parse-json-4.0.0.tgz", "integrity": "sha1-vjX1Qlvh9/bHRxhPmKeIy5lHfuA=", + "dev": true, "requires": { "error-ex": "^1.3.1", "json-parse-better-errors": "^1.0.1" @@ -3973,12 +3982,14 @@ "path-exists": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-3.0.0.tgz", - "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=" + "integrity": "sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=", + "dev": true }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true }, "path-key": { "version": "2.0.1", @@ -4015,7 +4026,8 @@ "pify": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pify/-/pify-3.0.0.tgz", - "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=" + "integrity": "sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=", + "dev": true }, "pirates": { "version": "4.0.1", @@ -4026,55 +4038,6 @@ "node-modules-regexp": "^1.0.0" } }, - "pkg-conf": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/pkg-conf/-/pkg-conf-2.1.0.tgz", - "integrity": "sha1-ISZRTKbyq/69FoWW3xi6V4Z/AFg=", - "requires": { - "find-up": "^2.0.0", - "load-json-file": "^4.0.0" - }, - "dependencies": { - "find-up": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-2.1.0.tgz", - "integrity": "sha1-RdG35QbHF93UgndaK3eSCjwMV6c=", - "requires": { - "locate-path": "^2.0.0" - } - }, - "locate-path": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", - "integrity": "sha1-K1aLJl7slExtnA3pw9u7ygNUzY4=", - "requires": { - "p-locate": "^2.0.0", - "path-exists": "^3.0.0" - } - }, - "p-limit": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-1.3.0.tgz", - "integrity": "sha512-vvcXsLAJ9Dr5rQOPk7toZQZJApBl2K4J6dANSsEuh6QI41JYcsS/qhTGa9ErIUUgK3WNQoJYvylxvjqmiqEA9Q==", - "requires": { - "p-try": "^1.0.0" - } - }, - "p-locate": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-2.0.0.tgz", - "integrity": "sha1-IKAQOyIqcMj9OcwuWAaA893l7EM=", - "requires": { - "p-limit": "^1.1.0" - } - }, - "p-try": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-1.0.0.tgz", - "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=" - } - } - }, "pkg-dir": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-3.0.0.tgz", @@ -4360,6 +4323,7 @@ "version": "2.6.3", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz", "integrity": "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==", + "dev": true, "requires": { "glob": "^7.1.3" } @@ -4472,16 +4436,6 @@ "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz", "integrity": "sha1-tf3AjxKH6hF4Yo5BXiUTK3NkbG0=" }, - "signale": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/signale/-/signale-1.4.0.tgz", - "integrity": "sha512-iuh+gPf28RkltuJC7W5MRi6XAjTDCAPC/prJUpQoG4vIP3MJZ+GTydVnodXA7pwvTKb2cA0m9OFZW/cdWy/I/w==", - "requires": { - "chalk": "^2.3.2", - "figures": "^2.0.0", - "pkg-conf": "^2.1.0" - } - }, "sisteransi": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.0.tgz", @@ -4686,7 +4640,8 @@ "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", + "dev": true }, "sshpk": { "version": "1.16.1", @@ -4813,7 +4768,8 @@ "strip-bom": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-3.0.0.tgz", - "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=" + "integrity": "sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=", + "dev": true }, "strip-eof": { "version": "1.0.0", @@ -5255,14 +5211,6 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, - "write": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/write/-/write-1.0.3.tgz", - "integrity": "sha512-/lg70HAjtkUgWPVZhZcm+T4hkL8Zbtp1nFNOn3lRrxnlv50SRBv7cR7RqR+GMsd3hUXy9hWBo4CHTbFTcOYwig==", - "requires": { - "mkdirp": "^0.5.1" - } - }, "write-file-atomic": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-2.4.1.tgz", diff --git a/package.json b/package.json index 6fde7e40..788462ae 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "pr-labeler", + "name": "pr-labeler-action", "private": true, "main": "index.js", "scripts": { @@ -7,7 +7,8 @@ "test": "jest" }, "dependencies": { - "actions-toolkit": "^2.0.0", + "@actions/core": "^1.1.1", + "@actions/github": "^1.1.0", "matcher": "^2.0.0" }, "devDependencies": { diff --git a/utils/config.js b/utils/config.js index 82ee3b06..21440d1a 100644 --- a/utils/config.js +++ b/utils/config.js @@ -12,7 +12,7 @@ module.exports = async function getConfig(github, fileName, { owner, repo }, ref owner, repo, path: path.posix.join(CONFIG_PATH, fileName), - ref: ref + ref }) return parseConfig(response.data.content) From 00db8f213df27a71ed88989a42c967ebf82bf7cd Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Wed, 2 Oct 2019 20:28:16 +0200 Subject: [PATCH 3/8] Replace Dockerfile with action.yml --- Dockerfile | 31 ------------------------------- action.yml | 4 ++-- 2 files changed, 2 insertions(+), 33 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index d2edcc46..00000000 --- a/Dockerfile +++ /dev/null @@ -1,31 +0,0 @@ -# Use the latest version of Node.js -# -# You may prefer the full image: -# FROM node -# -# or even an alpine image (a smaller, faster, less-feature-complete image): -# FROM node:alpine -# -# You can specify a version: -# FROM node:10-slim -FROM node:slim - -# Labels for GitHub to read your action -LABEL "com.github.actions.name"="PR Labeler" -LABEL "com.github.actions.description"="Automatically labels your PRs based on branch name patterns like feature/* or fix/*." -# Here are all of the available icons: https://feathericons.com/ -LABEL "com.github.actions.icon"="tag" -# And all of the available colors: https://developer.github.com/actions/creating-github-actions/creating-a-docker-container/#label -LABEL "com.github.actions.color"="white" - -# Copy the package.json and package-lock.json -COPY package*.json ./ - -# Install dependencies -RUN npm ci - -# Copy the rest of your action's code -COPY . . - -# Run `node /index.js` -ENTRYPOINT ["node", "/index.js"] diff --git a/action.yml b/action.yml index 709a99d5..ceb0a177 100644 --- a/action.yml +++ b/action.yml @@ -5,5 +5,5 @@ branding: icon: 'tag' color: 'white' runs: - using: 'docker' - image: 'Dockerfile' + using: 'node12' + main: 'index.js' From 1ab6c59d8fea88dd99b3b31a03c1fa6f091fc773 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Wed, 2 Oct 2019 20:39:23 +0200 Subject: [PATCH 4/8] Use ncc to bundle node_modules --- package-lock.json | 6 ++++++ package.json | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 3339188a..53a69807 100644 --- a/package-lock.json +++ b/package-lock.json @@ -505,6 +505,12 @@ "integrity": "sha512-IsU1TD+8cQCyG76ZqxP0cVFnofvfzT8p/wO8ENT4jbN/KKN3grsHFgHNl/U+08s33ayX4LwI85cEhYXCOlOkMw==", "dev": true }, + "@zeit/ncc": { + "version": "0.20.5", + "resolved": "https://registry.npmjs.org/@zeit/ncc/-/ncc-0.20.5.tgz", + "integrity": "sha512-XU6uzwvv95DqxciQx+aOLhbyBx/13ky+RK1y88Age9Du3BlA4mMPCy13BGjayOrrumOzlq1XV3SD/BWiZENXlw==", + "dev": true + }, "abab": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/abab/-/abab-2.0.0.tgz", diff --git a/package.json b/package.json index 788462ae..e65ba33c 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "pr-labeler-action", "private": true, - "main": "index.js", + "main": "dist/index.js", "scripts": { "start": "node ./index.js", + "build": "ncc build index.js -o dist", "test": "jest" }, "dependencies": { @@ -12,6 +13,7 @@ "matcher": "^2.0.0" }, "devDependencies": { + "@zeit/ncc": "^0.20.5", "jest": "^24.5.0", "js-yaml": "^3.13.1", "nock": "^10.0.6" From 219bfeb206e5d7220deba238c1db0608412069a7 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Wed, 2 Oct 2019 20:42:45 +0200 Subject: [PATCH 5/8] Point main script to dist directory --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index ceb0a177..df73279a 100644 --- a/action.yml +++ b/action.yml @@ -6,4 +6,4 @@ branding: color: 'white' runs: using: 'node12' - main: 'index.js' + main: 'dist/index.js' From 58f6536eb8f42f006eef01742c84a93450059a0a Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Thu, 3 Oct 2019 18:42:28 +0200 Subject: [PATCH 6/8] Show helpful error message when action is being used from master branch --- action.test.js => __tests__/action.test.js | 2 +- action.yml | 2 +- index.js | 26 ++++++++++++++++++++-- package.json | 4 ++-- action.js => src/action.js | 0 src/index.js | 3 +++ {utils => src/utils}/config.js | 0 7 files changed, 31 insertions(+), 6 deletions(-) rename action.test.js => __tests__/action.test.js (98%) rename action.js => src/action.js (100%) create mode 100644 src/index.js rename {utils => src/utils}/config.js (100%) diff --git a/action.test.js b/__tests__/action.test.js similarity index 98% rename from action.test.js rename to __tests__/action.test.js index 9796d2ab..f3bbb3a9 100644 --- a/action.test.js +++ b/__tests__/action.test.js @@ -1,6 +1,6 @@ const nock = require('nock') const fs = require('fs') -const action = require('./action') +const action = require('../src/action') nock.disableNetConnect() diff --git a/action.yml b/action.yml index df73279a..ceb0a177 100644 --- a/action.yml +++ b/action.yml @@ -6,4 +6,4 @@ branding: color: 'white' runs: using: 'node12' - main: 'dist/index.js' + main: 'index.js' diff --git a/index.js b/index.js index 275a9841..83917e91 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,25 @@ -const action = require('./action') +const distPath = './dist/index.js' +const errorMessage = `Please update .github/workflows/pr-labeler.yml to bind to a major version instead of master. -action() +You can do so by changing: +- uses: TimonVS/pr-labeler-action@master + +To: +- uses: TimonVS/pr-labeler-action@v3 +` + +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 + } + + console.error(errorMessage) + throw error +} diff --git a/package.json b/package.json index e65ba33c..4199db4a 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "pr-labeler-action", "private": true, - "main": "dist/index.js", + "main": "index.js", "scripts": { "start": "node ./index.js", - "build": "ncc build index.js -o dist", + "build": "ncc build src/index.js -o dist", "test": "jest" }, "dependencies": { diff --git a/action.js b/src/action.js similarity index 100% rename from action.js rename to src/action.js diff --git a/src/index.js b/src/index.js new file mode 100644 index 00000000..275a9841 --- /dev/null +++ b/src/index.js @@ -0,0 +1,3 @@ +const action = require('./action') + +action() diff --git a/utils/config.js b/src/utils/config.js similarity index 100% rename from utils/config.js rename to src/utils/config.js From e8d403a6b8763a3639ddb748d6bd43e2999b0f36 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Mon, 7 Oct 2019 19:26:40 +0200 Subject: [PATCH 7/8] Update error message --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 83917e91..5257e801 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,13 @@ const distPath = './dist/index.js' -const errorMessage = `Please update .github/workflows/pr-labeler.yml to bind to a major version instead of master. +const errorMessage = `ERROR: pr-labeler-action is configured incorrectly. Please update .github/workflows/pr-labeler.yml to bind to a major version instead of master. You can do so by changing: - uses: TimonVS/pr-labeler-action@master To: - uses: TimonVS/pr-labeler-action@v3 + +Please check the repository (https://github.com/TimonVS/pr-labeler-action) for the latest version. ` try { From 623e1c2398686d22c47fc5e27115b36910a61484 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Mon, 7 Oct 2019 19:39:53 +0200 Subject: [PATCH 8/8] Make error message more clear --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5257e801..5ac0c801 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const distPath = './dist/index.js' -const errorMessage = `ERROR: pr-labeler-action is configured incorrectly. Please update .github/workflows/pr-labeler.yml to bind to a major version instead of master. +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. You can do so by changing: - uses: TimonVS/pr-labeler-action@master