Skip to content

Commit

Permalink
fix(parse): Outputs clean error message for invalid issue numbers. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Levi-Lesches authored Jul 19, 2022
1 parent 35544f7 commit 51ddc5b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ async function createLabel(label) {
}

async function getIssue(number) {
var json = await octokit.rest.issues.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: number,
}).catch(error => {
console.log(`Failed to get issue #${number}`);
core.setFailed(error);
});
return json.data;
try {
var json = await octokit.rest.issues.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: number,
});
return json.data;
} catch (error) { // RequestError
if (error.status === 404) {
core.setFailed(`Issue not found: #${number}`);
return null; // the invalid reference will be in the comment
} else {
throw Error(`Got an HTTP ${error.status} error while retrieving issue #${number}`);
}
}
}

async function getComments(issueNumber) {
Expand Down
9 changes: 5 additions & 4 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ async function update(pr) {
}

console.log(`PR is blocked by ${blockingIssueNumbers}`);
var openIssues = [];
let openIssues = [], brokenIssues = [];
for (issueNumber of blockingIssueNumbers) {
const issue = await github.getIssue(issueNumber);
if (issue.state !== "open") continue;
openIssues.push(issueNumber);
if (issue === null) brokenIssues.push(issueNumber);
else if (issue.state === "open") openIssues.push(issueNumber);
}
console.log(`PR needs these issues to be closed: ${openIssues}`);
console.warn(`The following issues could not be found: ${brokenIssues}`);

console.log("Writing comment");
const commentText = utils.getCommentText(blockingIssueNumbers, openIssues);
const commentText = utils.getCommentText(blockingIssueNumbers, openIssues, brokenIssues);
await github.writeComment(pr.number, commentText);
console.log("Comment written");

Expand Down
13 changes: 9 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ function getBlockingIssues(body) {
return issues;
}

function getCommentText(blockingIssues, openIssues) {
const isBlocked = openIssues.length > 0
function getCommentText(blockingIssues, openIssues, brokenIssues) {
let status = "Ready to merge :heavy_check_mark:";
if (brokenIssues.length > 0) status = "Error :warning:";
else if (openIssues.length > 0) status = "Blocked :x:";
var result = "";
result += `# Status: ${isBlocked ? "Blocked :x:" : "Ready to merge :heavy_check_mark:"}\n`;
result += `# Status: ${status}\n`;
result += "### Issues blocking this PR: \n";
for (issue of blockingIssues) {
let symbol = ":heavy_check_mark";
if (openIssues.includes(issue)) symbol = ":x:";
else if (brokenIssues.includes(issue)) symbol = ":warning: Issue/PR not found";
var isOpen = openIssues.includes(issue);
result += `- #${issue} ${isOpen ? ":x:" : ":heavy_check_mark:"}\n`;
result += `- #${issue} ${symbol}\n`;
}
result += "----\n";
result += signature;
Expand Down

0 comments on commit 51ddc5b

Please sign in to comment.