Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Update branch protections #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2020
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
1 change: 1 addition & 0 deletions 1 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ For each repo, this tool will:
- Update the default branch for the repo
- Delete the old branch
- Update [known URL patterns](https://github.com/mheap/github-default-branch/blob/main/src/update-content.js) in source files
- Update any branch protections for `$old` to `$new`. (This does **not** work with patterns, it has to be an exact match)

## Installation

Expand Down
9 changes: 9 additions & 0 deletions 9 bin/github-default-branch
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
const createBranch = require("../src/create-branch");
const removeBranch = require("../src/remove-branch");
const updateContent = require("../src/update-content");
const updateBranchProtection = require("../src/branch-protection");

const old = argv.old || "master";
const target = argv.new || "main";
Expand Down Expand Up @@ -144,6 +145,14 @@
});
}

if (argv.verbose) {
console.log(`✏️ Changing branch protections`);
}

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

if (argv.verbose) {
console.log(`✏️ Deleting old branch [${old}] in ${repo}`);
}
Expand Down
35 changes: 35 additions & 0 deletions 35 src/branch-protection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = async function (owner, repo, old, target, octokit) {
// Via https://github.com/gr2m/octokit-plugin-rename-branch/blob/5d3c37439515db9f49f049e28510dc596982cb02/src/rename-branch.ts#L56-L90
const query = `query($owner: String!, $repo: String!) {
repository(owner:$owner,name:$repo) {
branchProtectionRules(first:100) {
nodes {
id
pattern
}
}
}
}`;
const {
repository: {
branchProtectionRules: { nodes: branchProtectionRules },
},
} = await octokit.graphql(query, { owner, repo });

// there can only be one protection per pattern
const { id } = branchProtectionRules.find((rule) => rule.pattern === old);
await octokit.graphql(
`mutation($branchProtectionRuleId:ID!,$pattern:String!) {
updateBranchProtectionRule (input:{branchProtectionRuleId:$branchProtectionRuleId,pattern:$pattern}) {
branchProtectionRule {
id,
pattern
}
}
}`,
{
branchProtectionRuleId: id,
pattern: target,
}
);
};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.