2020-05-05 18:37:31 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-05-05 18:37:31 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Bar} from 'cli-progress';
|
2021-04-08 15:34:55 -04:00
|
|
|
import {types as graphqlTypes} from 'typed-graphqlify';
|
2020-05-05 18:37:31 -04:00
|
|
|
|
2020-05-20 17:28:27 -04:00
|
|
|
import {error, info} from '../../utils/console';
|
2020-10-01 19:06:56 -04:00
|
|
|
import {GitClient} from '../../utils/git/index';
|
2020-05-15 11:21:01 -04:00
|
|
|
import {getPendingPrs} from '../../utils/github';
|
|
|
|
import {exec} from '../../utils/shelljs';
|
2020-05-05 18:37:31 -04:00
|
|
|
|
|
|
|
|
2021-04-08 15:34:55 -04:00
|
|
|
/* Graphql schema for the response body for each pending PR. */
|
2020-05-05 18:37:31 -04:00
|
|
|
const PR_SCHEMA = {
|
|
|
|
headRef: {
|
2021-04-08 15:34:55 -04:00
|
|
|
name: graphqlTypes.string,
|
2020-05-05 18:37:31 -04:00
|
|
|
repository: {
|
2021-04-08 15:34:55 -04:00
|
|
|
url: graphqlTypes.string,
|
|
|
|
nameWithOwner: graphqlTypes.string,
|
2020-05-05 18:37:31 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
baseRef: {
|
2021-04-08 15:34:55 -04:00
|
|
|
name: graphqlTypes.string,
|
2020-05-05 18:37:31 -04:00
|
|
|
repository: {
|
2021-04-08 15:34:55 -04:00
|
|
|
url: graphqlTypes.string,
|
|
|
|
nameWithOwner: graphqlTypes.string,
|
2020-05-05 18:37:31 -04:00
|
|
|
},
|
|
|
|
},
|
2021-04-08 15:34:55 -04:00
|
|
|
updatedAt: graphqlTypes.string,
|
|
|
|
number: graphqlTypes.number,
|
|
|
|
mergeable: graphqlTypes.string,
|
|
|
|
title: graphqlTypes.string,
|
2020-05-05 18:37:31 -04:00
|
|
|
};
|
|
|
|
|
2021-04-08 15:34:55 -04:00
|
|
|
/* Pull Request response from Github Graphql query */
|
2020-05-05 18:37:31 -04:00
|
|
|
type RawPullRequest = typeof PR_SCHEMA;
|
|
|
|
|
|
|
|
/** Convert raw Pull Request response from Github to usable Pull Request object. */
|
|
|
|
function processPr(pr: RawPullRequest) {
|
|
|
|
return {...pr, updatedAt: (new Date(pr.updatedAt)).getTime()};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pull Request object after processing, derived from the return type of the processPr function. */
|
|
|
|
type PullRequest = ReturnType<typeof processPr>;
|
|
|
|
|
|
|
|
/** Name of a temporary local branch that is used for checking conflicts. **/
|
|
|
|
const tempWorkingBranch = '__NgDevRepoBaseAfterChange__';
|
|
|
|
|
|
|
|
/** Checks if the provided PR will cause new conflicts in other pending PRs. */
|
2021-04-08 15:34:55 -04:00
|
|
|
export async function discoverNewConflictsForPr(newPrNumber: number, updatedAfter: number) {
|
|
|
|
/** The singleton instance of the GitClient. */
|
|
|
|
const git = GitClient.getAuthenticatedInstance();
|
2020-05-05 18:37:31 -04:00
|
|
|
// If there are any local changes in the current repository state, the
|
|
|
|
// check cannot run as it needs to move between branches.
|
2020-05-27 18:39:31 -04:00
|
|
|
if (git.hasLocalChanges()) {
|
2020-05-20 17:28:27 -04:00
|
|
|
error('Cannot run with local changes. Please make sure there are no local changes.');
|
2020-05-05 18:37:31 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-06-25 10:15:14 -04:00
|
|
|
/** The active github branch or revision before we performed any Git commands. */
|
|
|
|
const previousBranchOrRevision = git.getCurrentBranchOrRevision();
|
2020-05-05 18:37:31 -04:00
|
|
|
/* Progress bar to indicate progress. */
|
|
|
|
const progressBar = new Bar({format: `[{bar}] ETA: {eta}s | {value}/{total}`});
|
|
|
|
/* PRs which were found to be conflicting. */
|
|
|
|
const conflicts: Array<PullRequest> = [];
|
|
|
|
|
2020-05-20 17:28:27 -04:00
|
|
|
info(`Requesting pending PRs from Github`);
|
2020-05-05 18:37:31 -04:00
|
|
|
/** List of PRs from github currently known as mergable. */
|
2020-08-14 19:49:07 -04:00
|
|
|
const allPendingPRs = (await getPendingPrs(PR_SCHEMA, git)).map(processPr);
|
2020-05-05 18:37:31 -04:00
|
|
|
/** The PR which is being checked against. */
|
|
|
|
const requestedPr = allPendingPRs.find(pr => pr.number === newPrNumber);
|
|
|
|
if (requestedPr === undefined) {
|
2020-05-20 17:28:27 -04:00
|
|
|
error(
|
2020-05-05 18:37:31 -04:00
|
|
|
`The request PR, #${newPrNumber} was not found as a pending PR on github, please confirm`);
|
2020-05-20 17:28:27 -04:00
|
|
|
error(`the PR number is correct and is an open PR`);
|
2020-05-05 18:37:31 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pendingPrs = allPendingPRs.filter(pr => {
|
|
|
|
return (
|
|
|
|
// PRs being merged into the same target branch as the requested PR
|
|
|
|
pr.baseRef.name === requestedPr.baseRef.name &&
|
|
|
|
// PRs which either have not been processed or are determined as mergable by Github
|
|
|
|
pr.mergeable !== 'CONFLICTING' &&
|
|
|
|
// PRs updated after the provided date
|
|
|
|
pr.updatedAt >= updatedAfter);
|
|
|
|
});
|
2020-05-20 17:28:27 -04:00
|
|
|
info(`Retrieved ${allPendingPRs.length} total pending PRs`);
|
|
|
|
info(`Checking ${pendingPrs.length} PRs for conflicts after a merge of #${newPrNumber}`);
|
2020-05-05 18:37:31 -04:00
|
|
|
|
|
|
|
// Fetch and checkout the PR being checked.
|
|
|
|
exec(`git fetch ${requestedPr.headRef.repository.url} ${requestedPr.headRef.name}`);
|
|
|
|
exec(`git checkout -B ${tempWorkingBranch} FETCH_HEAD`);
|
|
|
|
|
|
|
|
// Rebase the PR against the PRs target branch.
|
|
|
|
exec(`git fetch ${requestedPr.baseRef.repository.url} ${requestedPr.baseRef.name}`);
|
|
|
|
const result = exec(`git rebase FETCH_HEAD`);
|
|
|
|
if (result.code) {
|
2020-05-20 17:28:27 -04:00
|
|
|
error('The requested PR currently has conflicts');
|
2020-06-25 10:15:14 -04:00
|
|
|
cleanUpGitState(previousBranchOrRevision);
|
2020-05-05 18:37:31 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the progress bar
|
|
|
|
progressBar.start(pendingPrs.length, 0);
|
|
|
|
|
|
|
|
// Check each PR to determine if it can merge cleanly into the repo after the target PR.
|
|
|
|
for (const pr of pendingPrs) {
|
|
|
|
// Fetch and checkout the next PR
|
|
|
|
exec(`git fetch ${pr.headRef.repository.url} ${pr.headRef.name}`);
|
|
|
|
exec(`git checkout --detach FETCH_HEAD`);
|
|
|
|
// Check if the PR cleanly rebases into the repo after the target PR.
|
|
|
|
const result = exec(`git rebase ${tempWorkingBranch}`);
|
|
|
|
if (result.code !== 0) {
|
|
|
|
conflicts.push(pr);
|
|
|
|
}
|
|
|
|
// Abort any outstanding rebase attempt.
|
|
|
|
exec(`git rebase --abort`);
|
|
|
|
|
|
|
|
progressBar.increment(1);
|
|
|
|
}
|
|
|
|
// End the progress bar as all PRs have been processed.
|
|
|
|
progressBar.stop();
|
2020-05-20 17:28:27 -04:00
|
|
|
info();
|
|
|
|
info(`Result:`);
|
2020-05-05 18:37:31 -04:00
|
|
|
|
2020-06-25 10:15:14 -04:00
|
|
|
cleanUpGitState(previousBranchOrRevision);
|
2020-05-05 18:37:31 -04:00
|
|
|
|
|
|
|
// If no conflicts are found, exit successfully.
|
|
|
|
if (conflicts.length === 0) {
|
2020-05-20 17:28:27 -04:00
|
|
|
info(`No new conflicting PRs found after #${newPrNumber} merging`);
|
2020-05-05 18:37:31 -04:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inform about discovered conflicts, exit with failure.
|
2020-05-20 17:28:27 -04:00
|
|
|
error.group(`${conflicts.length} PR(s) which conflict(s) after #${newPrNumber} merges:`);
|
2020-05-05 18:37:31 -04:00
|
|
|
for (const pr of conflicts) {
|
2020-06-12 11:20:01 -04:00
|
|
|
error(` - #${pr.number}: ${pr.title}`);
|
2020-05-05 18:37:31 -04:00
|
|
|
}
|
2020-05-20 17:28:27 -04:00
|
|
|
error.groupEnd();
|
2020-05-05 18:37:31 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-06-25 10:15:14 -04:00
|
|
|
/** Reset git back to the provided branch or revision. */
|
|
|
|
export function cleanUpGitState(previousBranchOrRevision: string) {
|
2020-05-05 18:37:31 -04:00
|
|
|
// Ensure that any outstanding rebases are aborted.
|
|
|
|
exec(`git rebase --abort`);
|
|
|
|
// Ensure that any changes in the current repo state are cleared.
|
|
|
|
exec(`git reset --hard`);
|
|
|
|
// Checkout the original branch from before the run began.
|
2020-06-25 10:15:14 -04:00
|
|
|
exec(`git checkout ${previousBranchOrRevision}`);
|
2020-05-05 18:37:31 -04:00
|
|
|
// Delete the generated branch.
|
|
|
|
exec(`git branch -D ${tempWorkingBranch}`);
|
|
|
|
}
|