|
2 | 2 | (async function () {
|
3 | 3 | const { argv } = require("yargs");
|
4 | 4 |
|
| 5 | + const isDryRun = !!argv.dryRun; |
| 6 | + |
| 7 | + // Force verbose output in dry run mode |
| 8 | + if (isDryRun) { |
| 9 | + argv.verbose = true; |
| 10 | + } |
| 11 | + |
5 | 12 | // Up front validation
|
6 | 13 | const providedRepoSelectors = [argv.org, argv.user, argv.repo].filter(
|
7 | 14 | (v) => v
|
|
41 | 48 | rate: { remaining },
|
42 | 49 | },
|
43 | 50 | } = await octokit.rateLimit.get();
|
44 |
| - console.log(`You have ${remaining} API requests remaining`); |
| 51 | + console.log(`You have ${remaining} API requests remaining\n`); |
45 | 52 | }
|
46 | 53 |
|
47 | 54 | const repos = await getRepos(argv, octokit);
|
48 | 55 |
|
| 56 | + if (argv.listReposOnly) { |
| 57 | + console.log(repos.join("\n")); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
49 | 61 | const old = argv.old || "master";
|
50 | 62 | const target = argv.new || "main";
|
51 | 63 |
|
|
56 | 68 |
|
57 | 69 | const [owner, repo] = r.split("/", 2);
|
58 | 70 | const currentMasterSha = await getBranchSha(owner, repo, old, octokit);
|
59 |
| - const mainBranch = await createBranch( |
60 |
| - owner, |
61 |
| - repo, |
62 |
| - target, |
63 |
| - currentMasterSha, |
64 |
| - octokit |
65 |
| - ); |
| 71 | + |
| 72 | + if (argv.verbose) { |
| 73 | + console.log(`✏️ Creating branch [${target}] at [${currentMasterSha}]`); |
| 74 | + } |
| 75 | + |
| 76 | + if (!isDryRun) { |
| 77 | + const mainBranch = await createBranch( |
| 78 | + owner, |
| 79 | + repo, |
| 80 | + target, |
| 81 | + currentMasterSha, |
| 82 | + octokit |
| 83 | + ); |
| 84 | + } |
66 | 85 |
|
67 | 86 | // List all PRs
|
68 | 87 | let pulls = await octokit.paginate(
|
|
82 | 101 | continue;
|
83 | 102 | }
|
84 | 103 |
|
85 |
| - await octokit.pulls.update({ |
| 104 | + if (argv.verbose) { |
| 105 | + console.log( |
| 106 | + `✏️ Updating pull request [#${pr.number}] in [${repo}] from [${pr.base.ref}] to [${target}]` |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + if (!isDryRun) { |
| 111 | + await octokit.pulls.update({ |
| 112 | + owner, |
| 113 | + repo, |
| 114 | + pull_number: pr.number, |
| 115 | + base: target, |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + if (argv.verbose) { |
| 121 | + console.log(`✏️ Updating default branch to [${target}] in [${repo}]`); |
| 122 | + } |
| 123 | + |
| 124 | + if (!isDryRun) { |
| 125 | + // Update the default branch in the repo |
| 126 | + await octokit.repos.update({ |
86 | 127 | owner,
|
87 | 128 | repo,
|
88 |
| - pull_number: pr.number, |
89 |
| - base: target, |
| 129 | + default_branch: target, |
90 | 130 | });
|
91 | 131 | }
|
92 | 132 |
|
93 |
| - // Update the default branch in the repo |
94 |
| - await octokit.repos.update({ |
95 |
| - owner, |
96 |
| - repo, |
97 |
| - default_branch: target, |
98 |
| - }); |
| 133 | + if (argv.verbose) { |
| 134 | + console.log(`✏️ Deleting old branch [${old}] in ${repo}`); |
| 135 | + } |
99 | 136 |
|
100 |
| - // Remove old branch if required |
101 |
| - if (!argv.keepOld) { |
102 |
| - await removeBranch(owner, repo, old, octokit); |
| 137 | + if (!isDryRun) { |
| 138 | + // Remove old branch if required |
| 139 | + if (!argv.keepOld) { |
| 140 | + await removeBranch(owner, repo, old, octokit); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Add an empty new line to break up the output for each repo |
| 145 | + if (argv.verbose) { |
| 146 | + console.log(""); |
103 | 147 | }
|
104 | 148 | }
|
105 | 149 |
|
|
0 commit comments