Skip to content

Commit

Permalink
Fix commenting on PR:s (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannseman authored Nov 2, 2021
1 parent 273e3f1 commit d14b63e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
34 changes: 15 additions & 19 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16363,17 +16363,19 @@ async function action(payload) {
filteredFiles: changedFiles,
reportName,
});

const belowThreshold = reports.some((report) => Math.floor(report.total) < minimumCoverage);

if(pullRequestNumber) {
const belowThreshold = reports.some(
(report) => Math.floor(report.total) < minimumCoverage
);

if (pullRequestNumber) {
await addComment(pullRequestNumber, comment, reportName);
}
await addCheck(comment, reportName, commit, belowThreshold);

if (failBelowThreshold && belowThreshold) {
core.setFailed("Minimum coverage requirement was not satisfied");
}
}
}

function markdownReport(reports, commit, options) {
Expand Down Expand Up @@ -16501,10 +16503,10 @@ async function addCheck(body, reportName, sha, belowThreshold) {
name: checkName,
head_sha: sha,
status: "completed",
conclusion: belowThreshold ? "failure": "success",
conclusion: belowThreshold ? "failure" : "success",
output: {
title: checkName,
summary: body
summary: body,
},
...github.context.repo,
});
Expand All @@ -16518,28 +16520,22 @@ async function listChangedFiles(pullRequestNumber) {
return files.data.map((file) => file.filename);
}

/**
*
* @param payload
* @returns {Promise<{pullRequestNumber: number, commit: null}>}
*/
async function pullRequestInfo(payload = {}, event_type) {
async function pullRequestInfo(payload = {}) {
let commit = null;
let pullRequestNumber = core.getInput("pull_request_number", {
required: false,
});

if (pullRequestNumber) {
// use the supplied PR

// Use the supplied PR
pullRequestNumber = parseInt(pullRequestNumber);
const { data } = await client.rest.pulls.get({
pull_number: pullRequestNumber,
...github.context.repo,
});
commit = data.head.sha;
} else if (payload.workflow_run) {
// fetch all open PRs and match the commit hash.
// Fetch all open PRs and match the commit hash.
commit = payload.workflow_run.head_commit.id;
const { data } = await client.rest.pulls.list({
...github.context.repo,
Expand All @@ -16548,13 +16544,13 @@ async function pullRequestInfo(payload = {}, event_type) {
pullRequestNumber = data
.filter((d) => d.head.sha === commit)
.reduce((n, d) => d.number, "");
} else if (payload.after) {
commit = payload.after;
} else if (payload.pull_request) {
// try to find the PR from payload
// Try to find the PR from payload
const { pull_request: pullRequest } = payload;
pullRequestNumber = pullRequest.number;
commit = pullRequest.head.sha;
} else if (payload.after) {
commit = payload.after;
}

return { pullRequestNumber, commit };
Expand Down
18 changes: 6 additions & 12 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,28 +218,22 @@ async function listChangedFiles(pullRequestNumber) {
return files.data.map((file) => file.filename);
}

/**
*
* @param payload
* @returns {Promise<{pullRequestNumber: number, commit: null}>}
*/
async function pullRequestInfo(payload = {}, event_type) {
async function pullRequestInfo(payload = {}) {
let commit = null;
let pullRequestNumber = core.getInput("pull_request_number", {
required: false,
});

if (pullRequestNumber) {
// use the supplied PR

// Use the supplied PR
pullRequestNumber = parseInt(pullRequestNumber);
const { data } = await client.rest.pulls.get({
pull_number: pullRequestNumber,
...github.context.repo,
});
commit = data.head.sha;
} else if (payload.workflow_run) {
// fetch all open PRs and match the commit hash.
// Fetch all open PRs and match the commit hash.
commit = payload.workflow_run.head_commit.id;
const { data } = await client.rest.pulls.list({
...github.context.repo,
Expand All @@ -248,13 +242,13 @@ async function pullRequestInfo(payload = {}, event_type) {
pullRequestNumber = data
.filter((d) => d.head.sha === commit)
.reduce((n, d) => d.number, "");
} else if (payload.after) {
commit = payload.after;
} else if (payload.pull_request) {
// try to find the PR from payload
// Try to find the PR from payload
const { pull_request: pullRequest } = payload;
pullRequestNumber = pullRequest.number;
commit = pullRequest.head.sha;
} else if (payload.after) {
commit = payload.after;
}

return { pullRequestNumber, commit };
Expand Down

0 comments on commit d14b63e

Please sign in to comment.