Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failed non-required checks failing overarching command #67

Merged
merged 5 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 45 additions & 27 deletions bin/can-merge
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const evaluateCommitStatus = require('../utils/evaluateCommitStatus');
const commitStatus = require('../utils/models/commitStatus');
const filterPullRequest = require('../utils/filterPullRequest');
const resolveIndentifier = require('../utils/resolveIndentifier');
const getPRWithRequiredChecks = require('../utils/getPRWithRequiredChecks');

const {
GITHUB_TOKEN,
Expand Down Expand Up @@ -133,7 +134,28 @@ const args = Yargs

const token = args.token || GITHUB_TOKEN || GH_TOKEN;

function outputStatus(response) {
function displayChecks(checks) {
checks.forEach((p) => {
if (p.isRequired) {
console.info(chalk.red(`🔒 ${p.name}`));
} else {
console.info(chalk.yellow(` ${p.name}`));
}
});
}

function outputChecks(failure, pending) {
if (pending.length > 0) {
console.info(chalk.red(`Pending Checks (${pending.length}):`));
displayChecks(pending);
}
if (failure.length > 0) {
console.info(chalk.red(`Failed Checks (${failure.length}):`));
displayChecks(failure);
}
}

function outputStatus(response, params) {
if (NODE_ENV === 'DEBUG') {
console.error(JSON.stringify(response, null, 2));
}
Expand All @@ -149,35 +171,31 @@ function outputStatus(response) {
}
}

const prs = filterPullRequest(response, args.pr);

if (prs.length === 0 && !args.commit) {
if (args.pr) {
console.info(chalk.redBright(`⚠ This remote repository does not contain any pull requests matching number: ${args.pr}`));
getPRWithRequiredChecks(filterPullRequest(response), params).then((prs) => {
if (prs.length === 0 && !args.commit) {
if (params.pr) {
console.info(chalk.redBright(`⚠ This remote repository does not contain any pull requests matching number: ${args.pr}`));
} else {
console.info(chalk.redBright(`⚠ This remote repository does not contain any pull requests matching sha: ${args.sha}`));
}
} else {
console.info(chalk.redBright(`⚠ This remote repository does not contain any pull requests matching sha: ${args.sha}`));
prs.forEach((pr) => {
const requiredChecks = pr.baseRef?.branchProtectionRule?.requiredStatusCheckContexts;
const status = evaluatePullRequest(pr, requiredChecks);
console.info(pr.url, 'by @'.concat(pr.author.login, ':'), pr.title, '\n', getMessage(status), '\n');
if (status !== pullRequestStatus.MERGEABLE) {
process.exitCode = (process.exitCode ?? 0) + 1;
}
const { failure, pending } = evaluateChecks(pr, requiredChecks);

outputChecks(failure, pending);
});
}
} else {
prs.forEach((pr) => {
const status = evaluatePullRequest(pr);
console.info(pr.url, 'by @'.concat(pr.author.login, ':'), pr.title, '\n', getMessage(status));
if (status !== pullRequestStatus.MERGEABLE) {
process.exitCode = (process.exitCode ?? 0) + 1;
}
if (status === pullRequestStatus.STATUS_FAILURE || status === pullRequestStatus.STATUS_PENDING) {
const { failure, pending } = evaluateChecks(pr);
});

if (pending.length > 0) {
console.info(chalk.yellowBright(`Pending Checks (${pending.length}): ${pending.join(', ')}`));
}
if (failure.length > 0) {
console.info(chalk.redBright(`Failed Checks (${failure.length}): ${failure.join(', ')}`));
}
}
});
}
console.error(`API Points: used - ${response.rateLimit.cost}\tremaining - ${response.rateLimit.remaining}`);
}

const { repo, pr, sha } = args;

const options = {
Expand All @@ -199,11 +217,11 @@ https.request(options, (response) => {
console.error('Waiting for all checks to complete ...\n');
watch(args.retryDelay, () => runQuery(params)).then((res) => {
console.clear();
outputStatus(res, args.pr);
outputStatus(res, params);
}).catch((error) => { console.log(chalk.red(error)); });
} else {
runQuery(params).then((res) => {
outputStatus(res, args.pr);
outputStatus(res, params);
}).catch((error) => { console.log(chalk.red(error)); });
}
}).on('error', (err) => {
Expand Down
192 changes: 12 additions & 180 deletions test/mocks/evalPR.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions utils/buildQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const pullRequestQuery = (name, owner, pr, sha) => `
state
url
title
baseRef {
name
branchProtectionRule {
isAdminEnforced
requiredStatusCheckContexts
}
}
author {
login
}
Expand Down Expand Up @@ -39,9 +46,11 @@ const pullRequestQuery = (name, owner, pr, sha) => `
... on CheckRun {
status
name
${pr ? `isRequired(pullRequestNumber: ${pr})` : ''}
conclusion
}
... on StatusContext {
${pr ? `isRequired(pullRequestNumber: ${pr})` : ''}
state
context
description
Expand Down