Skip to content

Commit

Permalink
feat: explicit 'pull-number' input (#293)
Browse files Browse the repository at this point in the history
* feat: explicit 'pull-number' input

* feat: explicit 'pull-number' input - dist

* feat: explicit 'pull-number' input - fix getPullNumber

* feat: explicit 'pull-number' input - linting

* feat: explicit 'pull-number' input - dist after lint
  • Loading branch information
garyburgmann authored and vitaliimelnychuk committed Dec 9, 2024
1 parent 6a19a71 commit 0289863
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 16 deletions.
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 21 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -14444,7 +14451,8 @@ module.exports = {
getLcovFile,
getBaseLcovFile,
getSendSummaryComment,
getWorkspacePath
getWorkspacePath,
getPullNumber
};


Expand Down Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,5 +56,6 @@ module.exports = {
getLcovFile,
getBaseLcovFile,
getSendSummaryComment,
getWorkspacePath
getWorkspacePath,
getPullNumber
};
13 changes: 10 additions & 3 deletions src/lib/github.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
26 changes: 26 additions & 0 deletions test/input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
);
});
});
76 changes: 71 additions & 5 deletions test/lib/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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()", () => {
Expand Down

0 comments on commit 0289863

Please sign in to comment.