Skip to content

Commit ce5f12c

Browse files
GUImheap
andauthored
Add GitHub Enterprise compatibility (#31)
* Add GitHub Enterprise compatibility - Add `--base-url` argument for pointing at a GitHub Enterprise API URL. - Add `--enterprise-compatibility` flag for loading the [`@octokit/plugin-enterprise-compatibility`](https://github.com/octokit/plugin-enterprise-compatibility.js/) plugin which is needed for the API to work against GitHub Enterprise's API before v2.19. - Add `--skip-update-branch-protection` flag for skipping the update branch protection step. While this perhaps isn't ideal, this part of the GraphQL API wasn't supported until GitHub Enterprise API v2.15. If you weren't using protected branches, then this might be okay to skip. - When running in verbose made, don't make failures to the rate limit API fatal, since GitHub Enterprise API versions may not support this endpoint. - When updating the `default_branch` via the API, also pass the `name` parameter, which appears to be required for some older API versions: https://github.community/t/cannot-change-default-branch/13728/13 Co-authored-by: Michael Heap <m@michaelheap.com>
1 parent 23f67cc commit ce5f12c

File tree

4 files changed

+83
-22
lines changed

4 files changed

+83
-22
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ Run with the `--verbose` flag to see debug information
5353

5454
## Options
5555

56-
| Flag | Description | Default |
57-
| ----------------- | -------------------------------------------------------------- | ------- |
58-
| --pat <token> | GitHub API Token | N/A |
59-
| --repo <name> | The repo to update (format: user/repo) | N/A |
60-
| --user <name> | Update all repos owned by the provided user (example: my-user) | N/A |
61-
| --org <name> | Update all repos in the provided org (example: my-org-name) | N/A |
62-
| --keep-old | Keep the old branch rather than deleting it | false |
63-
| --dry-run | Output log messages only. Do not make any changes | false |
64-
| --list-repos-only | List repos that would be affected, then exit | false |
65-
| --skip-forks | Skips forked repositories | false |
66-
| --old | The name of the branch to rename | master |
67-
| --new | The new branch name | main |
68-
| --confirm | Run without prompting for confirmation | false |
56+
| Flag | Description | Default |
57+
| ------------------------------- | -------------------------------------------------------------- | ------- |
58+
| --pat <token> | GitHub API Token | N/A |
59+
| --repo <name> | The repo to update (format: user/repo) | N/A |
60+
| --user <name> | Update all repos owned by the provided user (example: my-user) | N/A |
61+
| --org <name> | Update all repos in the provided org (example: my-org-name) | N/A |
62+
| --keep-old | Keep the old branch rather than deleting it | false |
63+
| --dry-run | Output log messages only. Do not make any changes | false |
64+
| --list-repos-only | List repos that would be affected, then exit | false |
65+
| --skip-forks | Skips forked repositories | false |
66+
| --skip-update-branch-protection | Skip updating branch protections | false |
67+
| --old | The name of the branch to rename | master |
68+
| --new | The new branch name | main |
69+
| --confirm | Run without prompting for confirmation | false |
6970

7071
## Replacements
7172

bin/github-default-branch

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,31 @@
5252
default: false,
5353
description: "Run without prompting for confirmation",
5454
},
55+
"base-url": {
56+
type: "string",
57+
description:
58+
"Set a custom API base URL for use with GitHub Enterprise (example: https://github.example.com/api/v3)",
59+
},
60+
// Plugin needed to use the old /git/refs/ API instead of the newer
61+
// /git/ref/ API introduced in v2.19.
62+
// https://developer.github.com/enterprise/2.18/v3/git/refs/
63+
// https://developer.github.com/enterprise/2.19/v3/git/refs/
64+
"enterprise-compatibility": {
65+
type: "boolean",
66+
default: false,
67+
description:
68+
"Load Octokit plugin for improving compatibility with older versions of GitHub Enterprise API (needed for GitHub Enterprise versions before v2.19)",
69+
},
70+
// "updateBranchProtectionRule" GraphQL mutation not supported in
71+
// versions of the API prior to 2.15.
72+
// https://developer.github.com/enterprise/2.14/v4/mutation/
73+
// https://developer.github.com/enterprise/2.15/v4/mutation/
74+
"skip-update-branch-protection": {
75+
type: "boolean",
76+
default: false,
77+
description:
78+
"Skip updating branch protection (not supported in GitHub Enterprise API versions before v2.15)",
79+
},
5580
})
5681
.example([
5782
["$0 --pat <token> --repo user/repo", "Rename master to main"],
@@ -114,17 +139,38 @@
114139
return;
115140
}
116141

117-
const octokit = new Octokit({
142+
let OctokitClass = Octokit;
143+
if (argv.enterpriseCompatibility) {
144+
const {
145+
enterpriseCompatibility,
146+
} = require("@octokit/plugin-enterprise-compatibility");
147+
OctokitClass = Octokit.plugin(enterpriseCompatibility);
148+
}
149+
150+
let octokitArgs = {
118151
auth: argv.pat || process.env.GITHUB_TOKEN,
119-
});
152+
};
153+
154+
if (argv.baseUrl) {
155+
octokitArgs.baseUrl = argv.baseUrl;
156+
}
157+
158+
const octokit = new OctokitClass(octokitArgs);
120159

121160
if (verbose) {
122-
const {
123-
data: {
124-
rate: { remaining },
125-
},
126-
} = await octokit.rateLimit.get();
127-
console.log(`You have ${remaining} API requests remaining\n`);
161+
try {
162+
const {
163+
data: {
164+
rate: { remaining },
165+
},
166+
} = await octokit.rateLimit.get();
167+
console.log(`You have ${remaining} API requests remaining\n`);
168+
} catch (e) {
169+
// GitHub Enterprise API versions may not support getting the rate
170+
// limits, so only log this as a warning, rather than treating errors as
171+
// fatal.
172+
console.log(`⚠️ Unable to determine rate limits: ${e.message}\n`);
173+
}
128174
}
129175

130176
const repos = await getRepos(argv, octokit);
@@ -208,6 +254,10 @@
208254
await octokit.repos.update({
209255
owner,
210256
repo,
257+
// The `name` parameter must be included for compatibility with older
258+
// GitHub Enterprise API versions:
259+
// https://github.community/t/cannot-change-default-branch/13728/13
260+
name: repo,
211261
default_branch: target,
212262
});
213263
} else {
@@ -221,7 +271,7 @@
221271
console.log(`✏️ Changing branch protections`);
222272
}
223273

224-
if (!isDryRun) {
274+
if (!isDryRun && !argv.skipUpdateBranchProtection) {
225275
await updateBranchProtection(owner, repo, old, target, octokit);
226276
}
227277

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"author": "Michael Heap <m@michaelheap.com> (https://michaelheap.com)",
1818
"license": "MIT",
1919
"dependencies": {
20+
"@octokit/plugin-enterprise-compatibility": "^1.2.5",
2021
"@octokit/rest": "^18.0.0",
2122
"prompt-confirm": "^2.0.4",
2223
"string.prototype.replaceall": "^1.0.3",

0 commit comments

Comments
 (0)