|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Bar} from 'cli-progress'; |
| 10 | +import {types as graphQLTypes} from 'typed-graphqlify'; |
| 11 | + |
| 12 | +import {getConfig, NgDevConfig} from '../utils/config'; |
| 13 | +import {getCurrentBranch, hasLocalChanges} from '../utils/git'; |
| 14 | +import {getPendingPrs} from '../utils/github'; |
| 15 | +import {exec} from '../utils/shelljs'; |
| 16 | + |
| 17 | + |
| 18 | +/* GraphQL schema for the response body for each pending PR. */ |
| 19 | +const PR_SCHEMA = { |
| 20 | + headRef: { |
| 21 | + name: graphQLTypes.string, |
| 22 | + repository: { |
| 23 | + url: graphQLTypes.string, |
| 24 | + nameWithOwner: graphQLTypes.string, |
| 25 | + }, |
| 26 | + }, |
| 27 | + baseRef: { |
| 28 | + name: graphQLTypes.string, |
| 29 | + repository: { |
| 30 | + url: graphQLTypes.string, |
| 31 | + nameWithOwner: graphQLTypes.string, |
| 32 | + }, |
| 33 | + }, |
| 34 | + updatedAt: graphQLTypes.string, |
| 35 | + number: graphQLTypes.number, |
| 36 | + mergeable: graphQLTypes.string, |
| 37 | + title: graphQLTypes.string, |
| 38 | +}; |
| 39 | + |
| 40 | +/* Pull Request response from Github GraphQL query */ |
| 41 | +type RawPullRequest = typeof PR_SCHEMA; |
| 42 | + |
| 43 | +/** Convert raw Pull Request response from Github to usable Pull Request object. */ |
| 44 | +function processPr(pr: RawPullRequest) { |
| 45 | + return {...pr, updatedAt: (new Date(pr.updatedAt)).getTime()}; |
| 46 | +} |
| 47 | + |
| 48 | +/* Pull Request object after processing, derived from the return type of the processPr function. */ |
| 49 | +type PullRequest = ReturnType<typeof processPr>; |
| 50 | + |
| 51 | +/** Name of a temporary local branch that is used for checking conflicts. **/ |
| 52 | +const tempWorkingBranch = '__NgDevRepoBaseAfterChange__'; |
| 53 | + |
| 54 | +/** Checks if the provided PR will cause new conflicts in other pending PRs. */ |
| 55 | +export async function discoverNewConflictsForPr( |
| 56 | + newPrNumber: number, updatedAfter: number, config: Pick<NgDevConfig, 'github'> = getConfig()) { |
| 57 | + // If there are any local changes in the current repository state, the |
| 58 | + // check cannot run as it needs to move between branches. |
| 59 | + if (hasLocalChanges()) { |
| 60 | + console.error('Cannot run with local changes. Please make sure there are no local changes.'); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + /** The active github branch when the run began. */ |
| 65 | + const originalBranch = getCurrentBranch(); |
| 66 | + /* Progress bar to indicate progress. */ |
| 67 | + const progressBar = new Bar({format: `[{bar}] ETA: {eta}s | {value}/{total}`}); |
| 68 | + /* PRs which were found to be conflicting. */ |
| 69 | + const conflicts: Array<PullRequest> = []; |
| 70 | + /* String version of the updatedAfter value, for logging. */ |
| 71 | + const updatedAfterString = new Date(updatedAfter).toLocaleDateString(); |
| 72 | + |
| 73 | + console.info(`Requesting pending PRs from Github`); |
| 74 | + /** List of PRs from github currently known as mergable. */ |
| 75 | + const allPendingPRs = (await getPendingPrs(PR_SCHEMA, config.github)).map(processPr); |
| 76 | + /** The PR which is being checked against. */ |
| 77 | + const requestedPr = allPendingPRs.find(pr => pr.number === newPrNumber); |
| 78 | + if (requestedPr === undefined) { |
| 79 | + console.error( |
| 80 | + `The request PR, #${newPrNumber} was not found as a pending PR on github, please confirm`); |
| 81 | + console.error(`the PR number is correct and is an open PR`); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | + |
| 85 | + const pendingPrs = allPendingPRs.filter(pr => { |
| 86 | + return ( |
| 87 | + // PRs being merged into the same target branch as the requested PR |
| 88 | + pr.baseRef.name === requestedPr.baseRef.name && |
| 89 | + // PRs which either have not been processed or are determined as mergable by Github |
| 90 | + pr.mergeable !== 'CONFLICTING' && |
| 91 | + // PRs updated after the provided date |
| 92 | + pr.updatedAt >= updatedAfter); |
| 93 | + }); |
| 94 | + console.info(`Retrieved ${allPendingPRs.length} total pending PRs`); |
| 95 | + console.info(`Checking ${pendingPrs.length} PRs for conflicts after a merge of #${newPrNumber}`); |
| 96 | + |
| 97 | + // Fetch and checkout the PR being checked. |
| 98 | + exec(`git fetch ${requestedPr.headRef.repository.url} ${requestedPr.headRef.name}`); |
| 99 | + exec(`git checkout -B ${tempWorkingBranch} FETCH_HEAD`); |
| 100 | + |
| 101 | + // Rebase the PR against the PRs target branch. |
| 102 | + exec(`git fetch ${requestedPr.baseRef.repository.url} ${requestedPr.baseRef.name}`); |
| 103 | + const result = exec(`git rebase FETCH_HEAD`); |
| 104 | + if (result.code) { |
| 105 | + console.error('The requested PR currently has conflicts'); |
| 106 | + cleanUpGitState(originalBranch); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | + |
| 110 | + // Start the progress bar |
| 111 | + progressBar.start(pendingPrs.length, 0); |
| 112 | + |
| 113 | + // Check each PR to determine if it can merge cleanly into the repo after the target PR. |
| 114 | + for (const pr of pendingPrs) { |
| 115 | + // Fetch and checkout the next PR |
| 116 | + exec(`git fetch ${pr.headRef.repository.url} ${pr.headRef.name}`); |
| 117 | + exec(`git checkout --detach FETCH_HEAD`); |
| 118 | + // Check if the PR cleanly rebases into the repo after the target PR. |
| 119 | + const result = exec(`git rebase ${tempWorkingBranch}`); |
| 120 | + if (result.code !== 0) { |
| 121 | + conflicts.push(pr); |
| 122 | + } |
| 123 | + // Abort any outstanding rebase attempt. |
| 124 | + exec(`git rebase --abort`); |
| 125 | + |
| 126 | + progressBar.increment(1); |
| 127 | + } |
| 128 | + // End the progress bar as all PRs have been processed. |
| 129 | + progressBar.stop(); |
| 130 | + console.info(`\nResult:`); |
| 131 | + |
| 132 | + cleanUpGitState(originalBranch); |
| 133 | + |
| 134 | + // If no conflicts are found, exit successfully. |
| 135 | + if (conflicts.length === 0) { |
| 136 | + console.info(`No new conflicting PRs found after #${newPrNumber} merging`); |
| 137 | + process.exit(0); |
| 138 | + } |
| 139 | + |
| 140 | + // Inform about discovered conflicts, exit with failure. |
| 141 | + console.error(`${conflicts.length} PR(s) which conflict(s) after #${newPrNumber} merges:`); |
| 142 | + for (const pr of conflicts) { |
| 143 | + console.error(` - ${pr.number}: ${pr.title}`); |
| 144 | + } |
| 145 | + process.exit(1); |
| 146 | +} |
| 147 | + |
| 148 | +/** Reset git back to the provided branch. */ |
| 149 | +export function cleanUpGitState(branch: string) { |
| 150 | + // Ensure that any outstanding rebases are aborted. |
| 151 | + exec(`git rebase --abort`); |
| 152 | + // Ensure that any changes in the current repo state are cleared. |
| 153 | + exec(`git reset --hard`); |
| 154 | + // Checkout the original branch from before the run began. |
| 155 | + exec(`git checkout ${branch}`); |
| 156 | + // Delete the generated branch. |
| 157 | + exec(`git branch -D ${tempWorkingBranch}`); |
| 158 | +} |
0 commit comments