Skip to content

Commit

Permalink
Merge pull request #20 from Siemian/feature/allow-direct-pull-number-…
Browse files Browse the repository at this point in the history
…passing

Allow passing pull request number
  • Loading branch information
hannseman authored Sep 14, 2020
2 parents cd8bdd9 + 50c2554 commit b3d1f48
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Only show coverage for changed files.

Use a unique name for the report and comment.

### `pull_request_number` **Optional**

Pull request number associated with the report. This property should be used when workflow trigger is different than `pull_request`.

## Example usage

```yaml
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ inputs:
description: 'Only show coverage for changed files.'
required: true
default: false
pull_request_number:
description: 'Pull request number associated with the report. This property should be used when workflow trigger is different than pull_request.'
required: false
default: ''
runs:
using: 'node12'
main: 'index.js'
Expand Down
42 changes: 28 additions & 14 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ const client = new github.GitHub(
const credits = "Generated by :monkey: cobertura-action";

async function action(payload) {
const { pull_request: pullRequest } = payload || {};
if (pullRequest == null) {
core.error("Found no pull request.");
return;
const pullRequestNumberInput = core.getInput("pull_request_number", {
required: false
});
let pullRequestNumber;
let commit;
if (pullRequestNumberInput != "") {
pullRequestNumber = parseInt(pullRequestNumberInput);
const { data } = await client.pulls.get({
pull_number: pullRequestNumber,
...github.context.repo
});
commit = data.head.sha;
} else {
const { pull_request: pullRequest } = payload || {};
if (pullRequest == null) {
core.error("Found no pull request.");
return;
}
pullRequestNumber = pullRequest.number;
commit = pullRequest.head.sha;
}

const path = core.getInput("path", { required: true });
const skipCovered = JSON.parse(
core.getInput("skip_covered", { required: true })
Expand All @@ -33,13 +50,10 @@ async function action(payload) {
const reportName = core.getInput("report_name", { required: false });

const changedFiles = onlyChangedFiles
? await listChangedFiles(pullRequest)
? await listChangedFiles(pullRequestNumber)
: null;

const report = await processCoverage(path, { skipCovered });
const {
head: { sha: commit }
} = pullRequest;
const comment = markdownReport(report, commit, {
minimumCoverage,
showLine,
Expand All @@ -48,7 +62,7 @@ async function action(payload) {
filteredFiles: changedFiles,
reportName
});
await addComment(pullRequest, comment, reportName);
await addComment(pullRequestNumber, comment, reportName);
}

function markdownReport(report, commit, options) {
Expand Down Expand Up @@ -126,9 +140,9 @@ function markdownReport(report, commit, options) {
return `${titleText}\n\n${table}\n\n${minimumCoverageText}\n\n${footerText}`;
}

async function addComment(pullRequest, body, reportName) {
async function addComment(pullRequestNumber, body, reportName) {
const comments = await client.issues.listComments({
issue_number: pullRequest.number,
issue_number: pullRequestNumber,
...github.context.repo
});
const commentFilter = reportName ? reportName : credits;
Expand All @@ -143,16 +157,16 @@ async function addComment(pullRequest, body, reportName) {
});
} else {
await client.issues.createComment({
issue_number: pullRequest.number,
issue_number: pullRequestNumber,
body: body,
...github.context.repo
});
}
}

async function listChangedFiles(pullRequest) {
async function listChangedFiles(pullRequestNumber) {
const files = await client.pulls.listFiles({
pull_number: pullRequest.number,
pull_number: pullRequestNumber,
...github.context.repo
});
return files.data.map(file => file.filename);
Expand Down
39 changes: 34 additions & 5 deletions src/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ test("action", async () => {
process.env["INPUT_MINIMUM_COVERAGE"] = "100";
process.env["INPUT_SHOW_CLASS_NAMES"] = "false";
process.env["INPUT_ONLY_CHANGED_FILES"] = "false";
process.env["INPUT_PULL_REQUEST_NUMBER"] = "";
const prNumber = 1;
nock("https://api.github.com")
.post(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
Expand All @@ -38,6 +39,33 @@ test("action", async () => {
await action();
});

test("action passing pull request number directly", async () => {
const { action } = require("./action");
const prNumber = 123;
process.env["INPUT_PATH"] = "./src/fixtures/test-branch.xml";
process.env["INPUT_SKIP_COVERED"] = "true";
process.env["INPUT_SHOW_BRANCH"] = "false";
process.env["INPUT_SHOW_LINE"] = "false";
process.env["INPUT_MINIMUM_COVERAGE"] = "100";
process.env["INPUT_SHOW_CLASS_NAMES"] = "false";
process.env["INPUT_ONLY_CHANGED_FILES"] = "false";
process.env["INPUT_PULL_REQUEST_NUMBER"] = prNumber;
nock("https://api.github.com")
.post(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200)
.get(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200, [{ body: "some body", id: 123 }])
.get(`/repos/${owner}/${repo}/pulls/${prNumber}`)
.reply(200, {
head: {
sha: "deadbeef"
}
});
await action({
push: { ref: "master" }
});
});

test("action only changes", async () => {
const { action } = require("./action");
process.env["INPUT_PATH"] = "./src/fixtures/test-branch.xml";
Expand All @@ -47,6 +75,7 @@ test("action only changes", async () => {
process.env["INPUT_MINIMUM_COVERAGE"] = "100";
process.env["INPUT_SHOW_CLASS_NAMES"] = "false";
process.env["INPUT_ONLY_CHANGED_FILES"] = "true";
process.env["INPUT_PULL_REQUEST_NUMBER"] = "";
const prNumber = 1;
nock("https://api.github.com")
.post(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
Expand Down Expand Up @@ -246,7 +275,7 @@ test("addComment", async () => {
.reply(200)
.get(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200, [{ body: "some body", id: 123 }]);
await addComment({ number: prNumber }, "foo", "bar");
await addComment(prNumber, "foo", "bar");
});

test("addComment with update", async () => {
Expand All @@ -270,7 +299,7 @@ _Minimum allowed coverage is \`100%\`_
.reply(200, [{ body: oldComment, id: commentId }])
.patch(`/repos/${owner}/${repo}/issues/comments/${commentId}`)
.reply(200, [{ body: oldComment, id: commentId }]);
await addComment({ number: prNumber }, "foo", "bar");
await addComment(prNumber, "foo", "bar");
});

test("addComment for specific report", async () => {
Expand All @@ -291,7 +320,7 @@ _Minimum allowed coverage is \`100%\`_
.reply(200)
.get(`/repos/${owner}/${repo}/issues/${prNumber}/comments`)
.reply(200, [{ body: report1Comment, id: commentId }]);
await addComment({ number: prNumber }, "foo", "Report2");
await addComment(prNumber, "foo", "Report2");
});

test("addComment with update for specific report", async () => {
Expand Down Expand Up @@ -326,7 +355,7 @@ _Minimum allowed coverage is \`100%\`_
])
.patch(`/repos/${owner}/${repo}/issues/comments/${comment2Id}`)
.reply(200, [{ body: report2Comment, id: comment2Id }]);
await addComment({ number: prNumber }, "foo", "Report2");
await addComment(prNumber, "foo", "Report2");
});

test("listChangedFiles", async () => {
Expand All @@ -339,5 +368,5 @@ test("listChangedFiles", async () => {
filename: "file1.txt"
}
]);
await listChangedFiles({ number: prNumber });
await listChangedFiles(prNumber);
});

0 comments on commit b3d1f48

Please sign in to comment.