Skip to content

Commit 5012084

Browse files
committed
Update branch protections
Thanks to https://github.com/gr2m/octokit-plugin-rename-branch for the graphql mutation + code required to do this Resolves #4
1 parent 30827b3 commit 5012084

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ For each repo, this tool will:
1111
- Update the default branch for the repo
1212
- Delete the old branch
1313
- Update [known URL patterns](https://github.com/mheap/github-default-branch/blob/main/src/update-content.js) in source files
14+
- Update any branch protections for `$old` to `$new`. (This does **not** work with patterns, it has to be an exact match)
1415

1516
## Installation
1617

bin/github-default-branch

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
const createBranch = require("../src/create-branch");
4040
const removeBranch = require("../src/remove-branch");
4141
const updateContent = require("../src/update-content");
42+
const updateBranchProtection = require("../src/branch-protection");
4243

4344
const old = argv.old || "master";
4445
const target = argv.new || "main";
@@ -144,6 +145,14 @@
144145
});
145146
}
146147

148+
if (argv.verbose) {
149+
console.log(`✏️ Changing branch protections`);
150+
}
151+
152+
if (!isDryRun) {
153+
await updateBranchProtection(owner, repo, old, target, octokit);
154+
}
155+
147156
if (argv.verbose) {
148157
console.log(`✏️ Deleting old branch [${old}] in ${repo}`);
149158
}

src/branch-protection.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = async function (owner, repo, old, target, octokit) {
2+
// Via https://github.com/gr2m/octokit-plugin-rename-branch/blob/5d3c37439515db9f49f049e28510dc596982cb02/src/rename-branch.ts#L56-L90
3+
const query = `query($owner: String!, $repo: String!) {
4+
repository(owner:$owner,name:$repo) {
5+
branchProtectionRules(first:100) {
6+
nodes {
7+
id
8+
pattern
9+
}
10+
}
11+
}
12+
}`;
13+
const {
14+
repository: {
15+
branchProtectionRules: { nodes: branchProtectionRules },
16+
},
17+
} = await octokit.graphql(query, { owner, repo });
18+
19+
// there can only be one protection per pattern
20+
const { id } = branchProtectionRules.find((rule) => rule.pattern === old);
21+
await octokit.graphql(
22+
`mutation($branchProtectionRuleId:ID!,$pattern:String!) {
23+
updateBranchProtectionRule (input:{branchProtectionRuleId:$branchProtectionRuleId,pattern:$pattern}) {
24+
branchProtectionRule {
25+
id,
26+
pattern
27+
}
28+
}
29+
}`,
30+
{
31+
branchProtectionRuleId: id,
32+
pattern: target,
33+
}
34+
);
35+
};

0 commit comments

Comments
 (0)