ci: remove reliance on Github API for CI setup (#36500)

Previously our CI during the setup process has made requests
to the Github API to determine the target branch and shas.
With this change, this information is now determined via git
commands using pipeline parameters from CircleCI.

PR Close #36500
This commit is contained in:
Joey Perrott
2020-04-07 21:10:04 -07:00
committed by Matias Niemelä
parent 5e79799b89
commit 4480ba3e29
5 changed files with 203 additions and 162 deletions

View File

@ -40,33 +40,26 @@
const util = require('util');
const child_process = require('child_process');
const exec = util.promisify(child_process.exec);
const getRefsAndShasForTarget = require('./utils/get-refs-and-shas-for-target');
const getRefsAndShasForChange = require('./utils/git-get-changeset-refs');
// CLI validation
if (process.argv.length != 4) {
console.error(`This script requires the GitHub repository and PR number as arguments.`);
console.error(`Example: node tools/rebase-pr.js angular/angular 123`);
process.exitCode = 1;
return;
}
// Run
_main(...process.argv.slice(2)).catch(err => {
_main().catch(err => {
console.log('Failed to rebase on top of target branch.\n');
console.error(err);
process.exitCode = 1;
});
// Helpers
async function _main(repository, prNumber) {
const target = await getRefsAndShasForTarget(prNumber);
async function _main() {
const refs = await getRefsAndShasForChange();
// Log known refs and shas
console.log(`--------------------------------`);
console.log(` Target Branch: ${target.base.ref}`);
console.log(` Latest Commit for Target Branch: ${target.latestShaOfTargetBranch}`);
console.log(` Latest Commit for PR: ${target.latestShaOfPrBranch}`);
console.log(` First Common Ancestor SHA: ${target.commonAncestorSha}`);
console.log(` Target Branch: ${refs.base.ref}`);
console.log(` Latest Commit for Target Branch: ${refs.target.latestSha}`);
console.log(` Latest Commit for PR: ${refs.base.latestSha}`);
console.log(` First Common Ancestor SHA: ${refs.commonAncestorSha}`);
console.log(`--------------------------------`);
console.log();
@ -74,27 +67,27 @@ async function _main(repository, prNumber) {
// Get the count of commits between the latest commit from origin and the common ancestor SHA.
const {stdout: commitCount} =
await exec(`git rev-list --count origin/${target.base.ref}...${target.commonAncestorSha}`);
await exec(`git rev-list --count origin/${refs.base.ref}...${refs.commonAncestorSha}`);
console.log(`Checking ${commitCount.trim()} commits for changes in the CircleCI config file.`);
// Check if the files changed between the latest commit from origin and the common ancestor SHA
// includes the CircleCI config.
const {stdout: circleCIConfigChanged} = await exec(`git diff --name-only origin/${
target.base.ref} ${target.commonAncestorSha} -- .circleci/config.yml`);
refs.base.ref} ${refs.commonAncestorSha} -- .circleci/config.yml`);
if (!!circleCIConfigChanged) {
throw Error(`
CircleCI config on ${target.base.ref} has been modified since commit ${
target.commonAncestorSha.slice(0, 7)},
CircleCI config on ${refs.base.ref} has been modified since commit ${
refs.commonAncestorSha.slice(0, 7)},
which this PR is based on.
Please rebase the PR on ${target.base.ref} after fetching from upstream.
Please rebase the PR on ${refs.base.ref} after fetching from upstream.
Rebase instructions for PR Author, please run the following commands:
git fetch upstream ${target.base.ref};
git checkout ${target.head.ref};
git rebase upstream/${target.base.ref};
git fetch upstream ${refs.base.ref};
git checkout ${refs.head.ref};
git rebase upstream/${refs.base.ref};
git push --force-with-lease;
`);
} else {
@ -103,7 +96,7 @@ async function _main(repository, prNumber) {
console.log();
// Rebase the PR.
console.log(`Rebasing current branch on ${target.base.ref}.`);
await exec(`git rebase origin/${target.base.ref}`);
console.log(`Rebasing current branch on ${refs.base.ref}.`);
await exec(`git rebase origin/${refs.base.ref}`);
console.log('Rebase successful.');
}