MCPcopy Create free account
hub / github.com/angular/dev-infra / rebasePr

Function rebasePr

ng-dev/pr/rebase/index.ts:22–169  ·  view source on GitHub ↗
(prNumber: number, interactive: boolean = false)

Source from the content-addressed store, hash-verified

20/**
21 * Rebase the provided PR onto its merge target branch, and push up the resulting
22 * commit to the PRs repository.
23 *
24 * @returns a status code indicating whether the rebase was successful.
25 */
26export async function rebasePr(prNumber: number, interactive: boolean = false): Promise<number> {
27 /** The singleton instance of the authenticated git client. */
28 const git = await AuthenticatedGitClient.get();
29
30 if (git.hasUncommittedChanges()) {
31 Log.error('Cannot perform rebase of PR with local changes.');
32 return 1;
33 }
34
35 /**
36 * The branch or revision originally checked out before this method performed
37 * any Git operations that may change the working branch.
38 */
39 const previousBranchOrRevision = git.getCurrentBranchOrRevision();
40 /* Get the PR information from Github. */
41 const pr = await fetchPullRequestFromGithub(git, prNumber);
42
43 if (pr === null) {
44 Log.error(`Specified pull request does not exist.`);
45 return 1;
46 }
47
48 const headRefName = pr.headRef.name;
49 const baseRefName = pr.baseRef.name;
50 const fullHeadRef = `${pr.headRef.repository.nameWithOwner}:${headRefName}`;
51 const fullBaseRef = `${pr.baseRef.repository.nameWithOwner}:${baseRefName}`;
52 const headRefUrl = addTokenToGitHttpsUrl(pr.headRef.repository.url, git.githubToken);
53 const baseRefUrl = addTokenToGitHttpsUrl(pr.baseRef.repository.url, git.githubToken);
54
55 // Note: Since we use a detached head for rebasing the PR and therefore do not have
56 // remote-tracking branches configured, we need to set our expected ref and SHA. This
57 // allows us to use `--force-with-lease` for the detached head while ensuring that we
58 // never accidentally override upstream changes that have been pushed in the meanwhile.
59 // See:
60 // https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-with-leaseltrefnamegtltexpectgt
61 const forceWithLeaseFlag = `--force-with-lease=${headRefName}:${pr.headRefOid}`;
62 const escapedHeadRefName = `'${headRefName.replace(/'/g, "'\\''")}'`;
63 const escapedForceWithLeaseFlag = `--force-with-lease=${escapedHeadRefName}:${pr.headRefOid}`;
64
65 // If the PR does not allow maintainers to modify it, exit as the rebased PR cannot
66 // be pushed up.
67 if (!pr.maintainerCanModify && !pr.viewerDidAuthor) {
68 Log.error(
69 `Cannot rebase as you did not author the PR and the PR does not allow maintainers` +
70 `to modify the PR`,
71 );
72 return 1;
73 }
74
75 try {
76 // Fetches are done with --deepen=500 increase the likelihood of finding a common ancestor when
77 // a shallow clone is being used.
78
79 // Fetch the PR branch and verify its head SHA matches the queried PR metadata before checkout.

Callers 1

handlerFunction · 0.85

Calls 11

addTokenToGitHttpsUrlFunction · 0.85
getCommitsInRangeFunction · 0.85
hasUncommittedChangesMethod · 0.80
checkoutMethod · 0.80
getMethod · 0.45
errorMethod · 0.45
runMethod · 0.45
pushMethod · 0.45
runGracefulMethod · 0.45

Tested by

no test coverage detected