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

Function checkoutToTargetBranch

ng-dev/pr/checkout/target.ts:16–80  ·  view source on GitHub ↗
(
  prNumber: number,
  target: string,
  {pullRequest}: Awaited<ReturnType<typeof checkOutPullRequestLocally>>,
)

Source from the content-addressed store, hash-verified

14import {checkOutPullRequestLocally} from '../common/checkout-pr.js';
15
16export async function checkoutToTargetBranch(
17 prNumber: number,
18 target: string,
19 {pullRequest}: Awaited<ReturnType<typeof checkOutPullRequestLocally>>,
20) {
21 /** An authenticated git client. */
22 const git = await AuthenticatedGitClient.get();
23 const config = git.config;
24
25 const branchName = `pr-${target.toLowerCase().replaceAll(/[\W_]/gm, '-')}-${prNumber}`;
26 const {owner, name: repo} = config.github;
27 const activeReleaseTrains = await ActiveReleaseTrains.fetch({
28 name: repo,
29 owner: owner,
30 nextBranchName: getNextBranchName(config.github),
31 api: git.github,
32 });
33
34 let targetBranch = target;
35 let targetName = target;
36
37 if (
38 target === 'patch' ||
39 target === 'latest' ||
40 activeReleaseTrains.latest.branchName === target
41 ) {
42 targetName = 'patch';
43 targetBranch = activeReleaseTrains.latest.branchName;
44 } else if (
45 target === 'main' ||
46 target === 'next' ||
47 target === 'minor' ||
48 activeReleaseTrains.next.branchName === target
49 ) {
50 targetName = 'main';
51 targetBranch = activeReleaseTrains.next.branchName;
52 } else if (
53 activeReleaseTrains.releaseCandidate &&
54 (target === 'rc' || activeReleaseTrains.releaseCandidate.branchName === target)
55 ) {
56 targetName = 'rc';
57 targetBranch = activeReleaseTrains.releaseCandidate.branchName;
58 }
59 Log.info(`Targeting '${targetBranch}' branch\n`);
60
61 const baseRefUrl = addTokenToGitHttpsUrl(pullRequest.baseRef.repository.url, git.githubToken);
62
63 git.run(['checkout', '-q', targetBranch]);
64 git.run(['fetch', '-q', baseRefUrl, '--deepen=500', '--', targetBranch]);
65 git.run(['checkout', '-b', branchName]);
66
67 Log.info(`Running cherry-pick`);
68
69 try {
70 const revisionRange = `${pullRequest.baseRefOid}..${pullRequest.headRefOid}`;
71 git.run(['cherry-pick', revisionRange]);
72 Log.info(
73 ` ${green('✔')} Cherry-pick is complete. You can now push to create a new pull request.`,

Callers 1

checkoutPullRequestFunction · 0.85

Calls 5

getNextBranchNameFunction · 0.85
addTokenToGitHttpsUrlFunction · 0.85
fetchMethod · 0.80
getMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected