Skip to content

Commit

Permalink
[Fix] get proper required status and output to console
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Ranger11 committed Mar 14, 2022
1 parent 0b1ec30 commit d892910
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 216 deletions.
77 changes: 50 additions & 27 deletions bin/can-merge
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const getMessage = require('../utils/getMessage');
const evaluateCommitStatus = require('../utils/evaluateCommitStatus');
const commitStatus = require('../utils/models/commitStatus');
const filterPullRequest = require('../utils/filterPullRequest');
const getPRWithRequiredChecks = require('../utils/getPRWithRequiredChecks');

const {
GITHUB_TOKEN,
Expand Down Expand Up @@ -126,7 +127,31 @@ const args = Yargs

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

function outputStatus(response) {
function outputChecks(failure, pending) {
if (pending.length > 0) {
console.info(chalk.red(`Pending Checks (${pending.length}): `));
pending.forEach((p) => {
if (p.isRequired) {
console.info(chalk.red(`${p.name} `));
} else {
console.info(chalk.yellow(`${p.name} `));

}
});
}
if (failure.length > 0) {
console.info(chalk.red(`Failed Checks (${failure.length}): `));
failure.forEach((f) => {
if (f.isRequired) {
console.info(chalk.red(`${f.name} `));
} else {
console.info(chalk.yellow(`${f.name} `));
}
});
}
}

function outputStatus(response, params) {
if (NODE_ENV === 'DEBUG') {
console.error(JSON.stringify(response, null, 2));
}
Expand All @@ -142,34 +167,30 @@ 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));
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(', ')}`));
}
}
});
}
}

const { repo, pr, sha } = args;

const options = {
Expand All @@ -191,11 +212,13 @@ 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);
if (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.

0 comments on commit d892910

Please sign in to comment.