Skip to content

Commit c755d11

Browse files
committed
Add additional logging and --dry-run flag
Resolves #8
1 parent 22d0e66 commit c755d11

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Run with the `--verbose` flag to see debug information
4646
| Flag | Description | Default |
4747
| ------------- | -------------------------------------------------------------- | ------- |
4848
| --pat <token> | GitHub API Token | N/A |
49+
| --dry-run | Output log messages only. Do not make any changes | N/A |
4950
| --repo <name> | The repo to update (format: user/repo) | N/A |
5051
| --user <name> | Update all repos owned by the provided user (example: my-user) | N/A |
5152
| --org <name> | Update all repos in the provided org (example: my-org-name) | N/A |

bin/github-default-branch

Lines changed: 59 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,7 +48,7 @@
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);
@@ -56,13 +63,20 @@
5663

5764
const [owner, repo] = r.split("/", 2);
5865
const currentMasterSha = await getBranchSha(owner, repo, old, octokit);
59-
const mainBranch = await createBranch(
60-
owner,
61-
repo,
62-
target,
63-
currentMasterSha,
64-
octokit
65-
);
66+
67+
if (argv.verbose) {
68+
console.log(`✏️ Creating branch [${target}] at [${currentMasterSha}]`);
69+
}
70+
71+
if (!isDryRun) {
72+
const mainBranch = await createBranch(
73+
owner,
74+
repo,
75+
target,
76+
currentMasterSha,
77+
octokit
78+
);
79+
}
6680

6781
// List all PRs
6882
let pulls = await octokit.paginate(
@@ -82,24 +96,49 @@
8296
continue;
8397
}
8498

85-
await octokit.pulls.update({
99+
if (argv.verbose) {
100+
console.log(
101+
`✏️ Updating pull request [#${pr.number}] in [${repo}] from [${pr.base.ref}] to [${target}]`
102+
);
103+
}
104+
105+
if (!isDryRun) {
106+
await octokit.pulls.update({
107+
owner,
108+
repo,
109+
pull_number: pr.number,
110+
base: target,
111+
});
112+
}
113+
}
114+
115+
if (argv.verbose) {
116+
console.log(`✏️ Updating default branch to [${target}] in [${repo}]`);
117+
}
118+
119+
if (!isDryRun) {
120+
// Update the default branch in the repo
121+
await octokit.repos.update({
86122
owner,
87123
repo,
88-
pull_number: pr.number,
89-
base: target,
124+
default_branch: target,
90125
});
91126
}
92127

93-
// Update the default branch in the repo
94-
await octokit.repos.update({
95-
owner,
96-
repo,
97-
default_branch: target,
98-
});
128+
if (argv.verbose) {
129+
console.log(`✏️ Deleting old branch [${old}] in ${repo}`);
130+
}
131+
132+
if (!isDryRun) {
133+
// Remove old branch if required
134+
if (!argv.keepOld) {
135+
await removeBranch(owner, repo, old, octokit);
136+
}
137+
}
99138

100-
// Remove old branch if required
101-
if (!argv.keepOld) {
102-
await removeBranch(owner, repo, old, octokit);
139+
// Add an empty new line to break up the output for each repo
140+
if (argv.verbose) {
141+
console.log("");
103142
}
104143
}
105144

0 commit comments

Comments
 (0)