diff --git a/README.md b/README.md index f87e6b2d..dbf1170f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index 6e506bac..0caf3fae 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/src/action.js b/src/action.js index ceb683f7..98454fc0 100644 --- a/src/action.js +++ b/src/action.js @@ -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 }) @@ -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, @@ -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) { @@ -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; @@ -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); diff --git a/src/action.test.js b/src/action.test.js index 8490ce93..72a2e7a1 100644 --- a/src/action.test.js +++ b/src/action.test.js @@ -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`) @@ -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"; @@ -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`) @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -339,5 +368,5 @@ test("listChangedFiles", async () => { filename: "file1.txt" } ]); - await listChangedFiles({ number: prNumber }); + await listChangedFiles(prNumber); });