diff --git a/action.yml b/action.yml index d72c2b6..33857da 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,8 @@ branding: inputs: github-token: description: Your GitHub Action token - required: true + required: false + default: '' barecheck-github-app-token: description: Barecheck application token, received after application installation. Would be used instead of `github-token` required: false @@ -40,6 +41,10 @@ inputs: description: "Path to your application. Mostly used for Monorepos if you need to add prefix for all files" default: "" required: false + pull-number: + description: "Pull request number (Optional). For use in non-'pull_request' events" + default: "" + required: false outputs: percentage: diff --git a/dist/index.js b/dist/index.js index f3bcd7f..8af57de 100644 --- a/dist/index.js +++ b/dist/index.js @@ -8435,11 +8435,11 @@ function getDate() { } /** - * Invokes `util.format()` with the specified arguments and writes to stderr. + * Invokes `util.formatWithOptions()` with the specified arguments and writes to stderr. */ function log(...args) { - return process.stderr.write(util.format(...args) + '\n'); + return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n'); } /** @@ -14435,6 +14435,13 @@ const getSendSummaryComment = () => const getWorkspacePath = () => core.getInput("workspace-path"); +const getPullNumber = () => { + const rawValue = core.getInput("pull-number"); + const intValue = parseInt(rawValue, 10); + const isNumber = !Number.isNaN(intValue); + return isNumber && intValue > 0 ? intValue : false; +}; + module.exports = { getShowAnnotations, getGithubToken, @@ -14444,7 +14451,8 @@ module.exports = { getLcovFile, getBaseLcovFile, getSendSummaryComment, - getWorkspacePath + getWorkspacePath, + getPullNumber }; @@ -14532,18 +14540,25 @@ module.exports = { const github = __nccwpck_require__(5438); const { githubApi } = __nccwpck_require__(5396); -const { getBarecheckGithubAppToken, getGithubToken } = __nccwpck_require__(6); +const { + getBarecheckGithubAppToken, + getGithubToken, + getPullNumber +} = __nccwpck_require__(6); let octokit = null; const cleanRef = (fullRef) => fullRef.replace("refs/heads/", ""); const getPullRequestContext = () => { - if (!github.context.payload.pull_request) return false; + const pullNumberInput = getPullNumber(); + + if (!github.context.payload.pull_request && !pullNumberInput) return false; const { owner, repo } = github.context.repo; - const pullNumber = github.context.payload.pull_request.number; + const pullNumber = + pullNumberInput || github.context.payload.pull_request.number; return { owner, diff --git a/src/input.js b/src/input.js index d1c99c6..8432450 100644 --- a/src/input.js +++ b/src/input.js @@ -40,6 +40,13 @@ const getSendSummaryComment = () => const getWorkspacePath = () => core.getInput("workspace-path"); +const getPullNumber = () => { + const rawValue = core.getInput("pull-number"); + const intValue = parseInt(rawValue, 10); + const isNumber = !Number.isNaN(intValue); + return isNumber && intValue > 0 ? intValue : false; +}; + module.exports = { getShowAnnotations, getGithubToken, @@ -49,5 +56,6 @@ module.exports = { getLcovFile, getBaseLcovFile, getSendSummaryComment, - getWorkspacePath + getWorkspacePath, + getPullNumber }; diff --git a/src/lib/github.js b/src/lib/github.js index 939f265..b613843 100644 --- a/src/lib/github.js +++ b/src/lib/github.js @@ -1,18 +1,25 @@ const github = require("@actions/github"); const { githubApi } = require("@barecheck/core"); -const { getBarecheckGithubAppToken, getGithubToken } = require("../input"); +const { + getBarecheckGithubAppToken, + getGithubToken, + getPullNumber +} = require("../input"); let octokit = null; const cleanRef = (fullRef) => fullRef.replace("refs/heads/", ""); const getPullRequestContext = () => { - if (!github.context.payload.pull_request) return false; + const pullNumberInput = getPullNumber(); + + if (!github.context.payload.pull_request && !pullNumberInput) return false; const { owner, repo } = github.context.repo; - const pullNumber = github.context.payload.pull_request.number; + const pullNumber = + pullNumberInput || github.context.payload.pull_request.number; return { owner, diff --git a/test/input.test.js b/test/input.test.js index 1adb3a0..6cc8b04 100644 --- a/test/input.test.js +++ b/test/input.test.js @@ -144,4 +144,30 @@ describe("input", () => { }) ); }); + + describe("getPullNumber", () => { + [ + { input: "foo", expected: false }, + { input: "", expected: false }, + { input: "-1", expected: false }, + { input: "0", expected: false }, + { input: "1", expected: 1 }, + { input: "2", expected: 2 }, + { input: "44", expected: 44 } + ].forEach(({ input, expected }) => + it(`should return ${expected} when 'pull-number=${input}'`, () => { + const expectedRes = input; + const getInput = sinon + .stub() + .withArgs("pull-number") + .returns(expectedRes); + + const { getPullNumber } = inputMock({ getInput }); + + const res = getPullNumber(input); + + assert.equal(res, expected); + }) + ); + }); }); diff --git a/test/lib/github.test.js b/test/lib/github.test.js index b965196..1d40880 100644 --- a/test/lib/github.test.js +++ b/test/lib/github.test.js @@ -3,30 +3,42 @@ const sinon = require("sinon"); const { assert } = require("chai"); const actionsCoreStub = require("../stubs/actionsCore.stub"); +// const { getPullNumber } = require('../../src/input'); const defaultMocks = { ...actionsCoreStub, github: {}, githubApi: {}, getBarecheckGithubAppToken: () => null, - getGithubToken: () => null + getGithubToken: () => null, + getPullNumber: () => false }; const getGitHubLibMock = (mocks) => { - const { github, githubApi, getBarecheckGithubAppToken, getGithubToken } = { + const { + github, + githubApi, + getBarecheckGithubAppToken, + getGithubToken, + getPullNumber + } = { ...defaultMocks, ...mocks }; return proxyquire("../../src/lib/github", { "@actions/github": github, "@barecheck/core": { githubApi }, - "../input": { getBarecheckGithubAppToken, getGithubToken } + "../input": { + getBarecheckGithubAppToken, + getGithubToken, + getPullNumber + } }); }; describe("lib/github", () => { describe("getPullRequestContext()", () => { - it("should return value from github context", () => { + it("should return value from github context and no pull-number input", () => { const github = { context: { repo: { @@ -51,7 +63,7 @@ describe("lib/github", () => { }); }); - it("should return false when there is no Pull request context", () => { + it("should return false when there is no Pull request context and no pull-number input", () => { const github = { context: { repo: { @@ -67,6 +79,60 @@ describe("lib/github", () => { assert.isFalse(pullRequestContext); }); + + it("should return true when there is a pull-number input and no Pull request context", () => { + const github = { + context: { + repo: { + owner: "barecheck", + repo: "barecheck" + }, + payload: {} + } + }; + const getPullNumber = sinon.stub().returns(321); + const { getPullRequestContext } = getGitHubLibMock({ + getPullNumber, + github + }); + + const pullRequestContext = getPullRequestContext(); + + assert.deepEqual(pullRequestContext, { + owner: "barecheck", + repo: "barecheck", + pullNumber: 321 + }); + }); + + it("should return value with explicit pull-number precedence", () => { + const github = { + context: { + repo: { + owner: "barecheck", + repo: "barecheck" + }, + payload: { + pull_request: { + number: 123 + } + } + } + }; + const getPullNumber = sinon.stub().returns(321); + const { getPullRequestContext } = getGitHubLibMock({ + getPullNumber, + github + }); + + const pullRequestContext = getPullRequestContext(); + + assert.deepEqual(pullRequestContext, { + owner: "barecheck", + repo: "barecheck", + pullNumber: 321 + }); + }); }); describe("getOctokit()", () => {