|
52 | 52 | default: false,
|
53 | 53 | description: "Run without prompting for confirmation",
|
54 | 54 | },
|
| 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 | + }, |
55 | 80 | })
|
56 | 81 | .example([
|
57 | 82 | ["$0 --pat <token> --repo user/repo", "Rename master to main"],
|
|
114 | 139 | return;
|
115 | 140 | }
|
116 | 141 |
|
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 = { |
118 | 151 | 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); |
120 | 159 |
|
121 | 160 | 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 | + } |
128 | 174 | }
|
129 | 175 |
|
130 | 176 | const repos = await getRepos(argv, octokit);
|
|
208 | 254 | await octokit.repos.update({
|
209 | 255 | owner,
|
210 | 256 | 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, |
211 | 261 | default_branch: target,
|
212 | 262 | });
|
213 | 263 | } else {
|
|
221 | 271 | console.log(`✏️ Changing branch protections`);
|
222 | 272 | }
|
223 | 273 |
|
224 |
| - if (!isDryRun) { |
| 274 | + if (!isDryRun && !argv.skipUpdateBranchProtection) { |
225 | 275 | await updateBranchProtection(owner, repo, old, target, octokit);
|
226 | 276 | }
|
227 | 277 |
|
|
0 commit comments