Skip to content

Commit 75be1f1

Browse files
committed
Initial Commit
0 parents  commit 75be1f1

10 files changed

+894
-0
lines changed

.gitignore

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# Snowpack dependency directory (https://snowpack.dev/)
45+
web_modules/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
.parcel-cache
78+
79+
# Next.js build output
80+
.next
81+
out
82+
83+
# Nuxt.js build / generate output
84+
.nuxt
85+
dist
86+
87+
# Gatsby files
88+
.cache/
89+
# Comment in the public line in if your project uses Gatsby and not Next.js
90+
# https://nextjs.org/blog/next-9-1#public-directory-support
91+
# public
92+
93+
# vuepress build output
94+
.vuepress/dist
95+
96+
# Serverless directories
97+
.serverless/
98+
99+
# FuseBox cache
100+
.fusebox/
101+
102+
# DynamoDB Local files
103+
.dynamodb/
104+
105+
# TernJS port file
106+
.tern-port
107+
108+
# Stores VSCode versions used for testing VSCode extensions
109+
.vscode-test
110+
111+
# yarn v2
112+
.yarn/cache
113+
.yarn/unplugged
114+
.yarn/build-state.yml
115+
.yarn/install-state.gz
116+
.pnp.*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License Copyright (c) 2020 Michael Heap
2+
3+
Permission is hereby granted, free
4+
of charge, to any person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use, copy, modify, merge,
7+
publish, distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to the
9+
following conditions:
10+
11+
The above copyright notice and this permission notice
12+
(including the next paragraph) shall be included in all copies or substantial
13+
portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# GitHub Default Branch
2+
3+
Rename your default branch on GitHub easily. By default it renames `master` to `main`, but is configurable using the `--new` and `--old` flags.
4+
5+
If provided with an `--org` argument, it will run on all repositories within that organisation. Alternatively, you can provide a `--repo` argument to edit a single repo.
6+
7+
For each repo, this tool will:
8+
9+
- Create a new branch at the same commit SHA as the old one
10+
- Update all open pull requests to point at the new branch
11+
- Update the default branch for the repo
12+
- Delete the old branch
13+
14+
## Installation
15+
16+
```bash
17+
npm install -g github-default-branch
18+
```
19+
20+
## Authentication
21+
22+
[Create a personal access token](https://github.com/settings/tokens/new) with the `repo` scope. This is the value for `<token>` in the examples
23+
24+
## Usage
25+
26+
```
27+
# Rename master to main
28+
github-default-branch --pat <token> --repo user/repo
29+
30+
# Rename dev to develop
31+
github-default-branch --pat <token> --repo user/repo --old dev --new develop
32+
```
33+
34+
Run with the `--verbose` flag to see debug information
35+
36+
## Options
37+
38+
| Flag | Description | Default |
39+
| ------------- | -------------------------------- | ------- |
40+
| --pat <token> | GitHub API Token | N/A |
41+
| --repo <name> | user/repo | N/A |
42+
| --org <name> | my-org-name | N/A |
43+
| --keep-old | | false |
44+
| --old | The name of the branch to rename | master |
45+
| --new | The new branch name | main |
46+
47+
## Enhancements
48+
49+
- Error if the target branch already exists
50+
- `--visibility` flag (`all`, `public`, `private`). Default `all`
51+
- Copy branch protections from the old branch to the new one

bin/github-default-branch

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env node
2+
(async function () {
3+
const { argv } = require("yargs");
4+
5+
// Up front validation
6+
if (!argv.org && !argv.repo) {
7+
console.log("❎ You must provide --org or --repo");
8+
return;
9+
}
10+
11+
if (argv.org && argv.repo) {
12+
console.log("❎ Only --org OR --repo is supported");
13+
return;
14+
}
15+
16+
if (!argv.pat && !process.env.GITHUB_TOKEN) {
17+
console.log(
18+
"❎ Provide a GitHub Access Token using --pat <token> or using the GITHUB_TOKEN environment variable"
19+
);
20+
return;
21+
}
22+
23+
const { Octokit } = require("@octokit/rest");
24+
25+
const getRepos = require("../src/get-repos");
26+
const getBranchSha = require("../src/get-branch-sha");
27+
const createBranch = require("../src/create-branch");
28+
const removeBranch = require("../src/remove-branch");
29+
30+
const octokit = new Octokit({
31+
auth: argv.pat || process.env.GITHUB_TOKEN,
32+
});
33+
34+
if (argv.verbose) {
35+
const {
36+
data: {
37+
rate: { remaining },
38+
},
39+
} = await octokit.rateLimit.get();
40+
console.log(`You have ${remaining} API requests remaining`);
41+
}
42+
43+
const repos = await getRepos(argv, octokit);
44+
45+
const old = argv.old || "master";
46+
const target = argv.new || "main";
47+
48+
for (let r of repos) {
49+
if (argv.verbose) {
50+
console.log(`✏️ Processing ${r}`);
51+
}
52+
53+
const [owner, repo] = r.split("/", 2);
54+
const currentMasterSha = await getBranchSha(owner, repo, old, octokit);
55+
const mainBranch = await createBranch(
56+
owner,
57+
repo,
58+
target,
59+
currentMasterSha,
60+
octokit
61+
);
62+
63+
// List all PRs
64+
let pulls = await octokit.paginate(
65+
"GET /repos/:owner/:repo/pulls",
66+
{
67+
owner,
68+
repo,
69+
state: "open",
70+
per_page: 100,
71+
},
72+
(response) => response.data
73+
);
74+
75+
// Update the target branch for all open PRs
76+
for (let pr of pulls) {
77+
if (pr.base.ref != old) {
78+
continue;
79+
}
80+
81+
await octokit.pulls.update({
82+
owner,
83+
repo,
84+
pull_number: pr.number,
85+
base: target,
86+
});
87+
}
88+
89+
// Update the default branch in the repo
90+
await octokit.repos.update({
91+
owner,
92+
repo,
93+
default_branch: target,
94+
});
95+
96+
// Remove old branch if required
97+
if (!argv.keepOld) {
98+
await removeBranch(owner, repo, old, octokit);
99+
}
100+
}
101+
102+
console.log("✅ Complete");
103+
})();

0 commit comments

Comments
 (0)