Skip to content

Commit 48b6357

Browse files
authored
Refactor to atomic transforms + data sources (#34)
1 parent 923f17e commit 48b6357

36 files changed

+6689
-835
lines changed

.eslintrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 2020,
4+
"impliedStrict": true,
5+
"sourceType": "module",
6+
"ecmaFeatures": {
7+
"impliedStrict": true,
8+
"experimentalObjectRestSpread": true
9+
}
10+
},
11+
"plugins": [
12+
"prettier"
13+
],
14+
"rules": {
15+
"prettier/prettier": "error"
16+
}
17+
}

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Node.js CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
matrix:
11+
node-version: [10.x, 12.x, 14.x]
12+
13+
env:
14+
CI: true
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Use Node.js ${{ matrix.node-version }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
- run: npm ci
22+
- run: npm test
23+
- run: npm run lint

README.md

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
Rename your default branch on GitHub easily. By default it renames `master` to `main`, but is configurable using the `--new` and `--old` flags.
44

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.
5+
If provided with an `--org`, `--user` or (`--org` and `--team`) arguments, it will run on all repositories owned by that org, user or team. Alternatively, you can provide a `--repo` argument to edit a single repo. See [Usage](#usage) for more examples.
66

77
For each repo, this tool will:
88

99
- Create a new branch at the same commit SHA as the old one
1010
- Update all open pull requests to point at the new branch
1111
- Update the default branch for the repo
1212
- Delete the old branch
13-
- Update [known URL patterns](https://github.com/mheap/github-default-branch/blob/main/src/update-content.js) in source files
13+
- Update [known URL patterns](https://github.com/mheap/github-default-branch/tree/main/replacements) in source files
1414
- Update any branch protections for `$old` to `$new`. (This does **not** work with patterns, it has to be an exact match)
1515

1616
## Installation
@@ -47,41 +47,60 @@ github-default-branch --pat <token> --org my-org-name
4747
4848
# Rename all repos owned by a user
4949
github-default-branch --pat <token> --user my-user
50+
51+
# Rename all repos owned by a team
52+
github-default-branch --pat <token> --org my-org-name --team my-team-slug
5053
```
5154

52-
Run with the `--verbose` flag to see debug information
55+
Set `DEBUG="ghdb*"` as an environment variable to see debug information
5356

5457
## Options
5558

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-
| --team <name> | Update all repos in the provided team (example: my-team-name), only usable in combination with org parameter | N/A |
63-
| --keep-old | Keep the old branch rather than deleting it | false |
64-
| --dry-run | Output log messages only. Do not make any changes | false |
65-
| --list-repos-only | List repos that would be affected, then exit | false |
66-
| --skip-forks | Skips forked repositories | false |
67-
| --skip-update-branch-protection | Skip updating branch protections | false |
68-
| --old | The name of the branch to rename | master |
69-
| --new | The new branch name | main |
70-
| --confirm | Run without prompting for confirmation | false |
59+
| Flag | Description | Default |
60+
| ------------- | ------------------------------------------------------------------------------------------------------------ | ------- |
61+
| --pat <token> | GitHub API Token | N/A |
62+
| --old | The name of the branch to rename | master |
63+
| --new | The new branch name | main |
64+
| --repo <name> | The repo to update (format: user/repo) | N/A |
65+
| --user <name> | Update all repos owned by the provided user (example: my-user) | N/A |
66+
| --org <name> | Update all repos in the provided org (example: my-org-name) | N/A |
67+
| --team <name> | Update all repos in the provided team (example: my-team-name), only usable in combination with org parameter | N/A |
68+
| --dry-run | Output log messages only. Do not make any changes | false |
69+
| --skip-forks | Skips forked repositories | false |
70+
| --confirm | Run without prompting for confirmation | false |
71+
72+
## Skipping transforms
73+
74+
You might want to skip any of the available transforms (such as deleting the old branch). You can add `--skip-[transform-name]` to disable the transform e.g. `--skip-delete-old-branch`.
75+
76+
## Available transforms
77+
78+
| Transform | Description |
79+
| ---------------------- | ----------------------------------------------------- |
80+
| update-default-branch | Set the default branch of the repo to `$new` |
81+
| retarget-pull-requests | Change the base for any open pull requests |
82+
| branch-protection | Update any branch protection rules to point at `$new` |
83+
| delete-old-branch | Delete the `$old` branch |
84+
85+
Pending transforms:
86+
87+
- Copy branch protections instead of updating if `--skip-delete-old-branch` is used ([#26](https://github.com/mheap/github-default-branch/issues/26))
88+
- Retarget draft releases ([#30](https://github.com/mheap/github-default-branch/issues/30))
89+
- Update GitHub Pages configuration ([#16](https://github.com/mheap/github-default-branch/issues/16))
7190

7291
## Replacements
7392

7493
Part of this script checks for the existence of files and updates their contents. Replacements are the mechanism for these updates.
7594

7695
### How it Works
7796

78-
Each .js file in the src/replacements folder is given a chance to run during the content updating step of the script. Each file in src/replacements is expected to export a function, that function receives all of the options that are available to the outmost script.
97+
Each .js file in the `replacements` folder is given a chance to run during the content updating step of the script. Each file in replacements is expected to export a function, that function receives all of the options that are available to the outmost script.
7998

8099
If there is nothing to replace, then the script moves on to the next replacement.
81100

82101
### How to Add a Replacement
83102

84-
Add a file to src/replacements with a .js extension
103+
Add a file to `replacements` with a .js extension
85104

86105
Like this:
87106

@@ -100,20 +119,16 @@ module.exports = function ({
100119
path: "<path to file in repo>",
101120
replacements: [
102121
{
103-
from: "<from pattern>",
104-
to: "<to pattern>",
122+
from: "<from string>",
123+
to: "<to string>",
105124
},
106125
{
107-
from: "<from pattern>",
108-
to: "<to pattern>",
126+
from: "<from string>",
127+
to: "<to string>",
109128
},
110129
],
111130
};
112131
};
113132
```
114133

115134
The file with the path in your repo will have any line matching `from` be swapped out with `to`
116-
117-
### Known Issues
118-
119-
The replacement system gives you octokit, that's great! Unfortunately replacement functions do not currently support asynchronous calls, that's bad.

0 commit comments

Comments
 (0)