Skip to content

Commit 8958b17

Browse files
authored
Merge pull request #9 from mheap/dry-run
2 parents 22d0e66 + a570e6e commit 8958b17

File tree

2 files changed

+75
-29
lines changed

2 files changed

+75
-29
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ Run with the `--verbose` flag to see debug information
4343

4444
## Options
4545

46-
| Flag | Description | Default |
47-
| ------------- | -------------------------------------------------------------- | ------- |
48-
| --pat <token> | GitHub API Token | N/A |
49-
| --repo <name> | The repo to update (format: user/repo) | N/A |
50-
| --user <name> | Update all repos owned by the provided user (example: my-user) | N/A |
51-
| --org <name> | Update all repos in the provided org (example: my-org-name) | N/A |
52-
| --keep-old | Keep the old branch rather than deleting it | false |
53-
| --old | The name of the branch to rename | master |
54-
| --new | The new branch name | main |
46+
| Flag | Description | Default |
47+
| ----------------- | -------------------------------------------------------------- | ------- |
48+
| --pat <token> | GitHub API Token | N/A |
49+
| --repo <name> | The repo to update (format: user/repo) | N/A |
50+
| --user <name> | Update all repos owned by the provided user (example: my-user) | N/A |
51+
| --org <name> | Update all repos in the provided org (example: my-org-name) | N/A |
52+
| --keep-old | Keep the old branch rather than deleting it | false |
53+
| --dry-run | Output log messages only. Do not make any changes | false |
54+
| --list-repos-only | List repos that would be affected, then exit | false |
55+
| --old | The name of the branch to rename | master |
56+
| --new | The new branch name | main |

bin/github-default-branch

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
(async function () {
33
const { argv } = require("yargs");
44

5+
const isDryRun = !!argv.dryRun;
6+
7+
// Force verbose output in dry run mode
8+
if (isDryRun) {
9+
argv.verbose = true;
10+
}
11+
512
// Up front validation
613
const providedRepoSelectors = [argv.org, argv.user, argv.repo].filter(
714
(v) => v
@@ -41,11 +48,16 @@
4148
rate: { remaining },
4249
},
4350
} = await octokit.rateLimit.get();
44-
console.log(`You have ${remaining} API requests remaining`);
51+
console.log(`You have ${remaining} API requests remaining\n`);
4552
}
4653

4754
const repos = await getRepos(argv, octokit);
4855

56+
if (argv.listReposOnly) {
57+
console.log(repos.join("\n"));
58+
return;
59+
}
60+
4961
const old = argv.old || "master";
5062
const target = argv.new || "main";
5163

@@ -56,13 +68,20 @@
5668

5769
const [owner, repo] = r.split("/", 2);
5870
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+
}
6685

6786
// List all PRs
6887
let pulls = await octokit.paginate(
@@ -82,24 +101,49 @@
82101
continue;
83102
}
84103

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({
86127
owner,
87128
repo,
88-
pull_number: pr.number,
89-
base: target,
129+
default_branch: target,
90130
});
91131
}
92132

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+
}
99136

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("");
103147
}
104148
}
105149

0 commit comments

Comments
 (0)