Skip to content

Instantly share code, notes, and snippets.

@Mr-istov
Last active May 30, 2021 09:49
Show Gist options
  • Save Mr-istov/eabcb24196a62a32064b022a6613dbc9 to your computer and use it in GitHub Desktop.
Save Mr-istov/eabcb24196a62a32064b022a6613dbc9 to your computer and use it in GitHub Desktop.
Delete protected branches on github
import { graphql } from "@octokit/graphql";
const OWNER = process.env.GITHUB_OWNER;
const REPO = process.env.GITHUB_REPO;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
/**
* Return the last {num} branch IDs that start with the {prefix}
*
* Note: This will only retrieve the ref (branch) ID
*/
const getBranches = async (gql, prefix, num) => {
const branches = await gql(
`
query findRefs($owner: String!, $repo: String!, $prefix: String!, $num: Int = 5) {
repository(owner: $owner, name: $repo) {
refs(refPrefix: $prefix, first: $num) {
edges {
node {
name
id
}
}
}
}
}`,
{
owner: OWNER,
repo: REPO,
prefix: `refs/heads/${prefix}`,
num,
}
);
return branches.repository.refs.edges.map((edge) => edge.node.id);
};
/**
* A branch is just a git ref, so we must pass the ref ID to delete it
* Use the function getBranches to get the ref ID of a branch
*/
const deleteBranch = async (gql, refId) => {
const data = await gql(
`
mutation deleteBranch($refId: ID!) {
deleteRef(input: {refId: $refId}) {
clientMutationId
}
}
`,
{
refId,
}
);
return data;
};
/**
* Run a mutation on the branch rule to update the `Allow deletions` option
*/
const allowDeleteProtectedBranch = async (gql, branchRuleID, allow) => {
const data = await gql(
`
mutation UpdateBranchProtectionRuleInput($branchRuleID: ID!, $allow: Boolean) {
updateBranchProtectionRule(input: { branchProtectionRuleId: $branchRuleID, allowsDeletions: $allow }) {
branchProtectionRule {
allowsDeletions
}
}
}`,
{
branchRuleID,
allow,
}
);
return data;
};
/**
* There is no filtering available when getting a branch rule
* This function will get branch rules and will filter them
* Return the ID of the one we search using the prefix argument
*/
const getBranchProtectionRuleID = async (gql, prefix) => {
// This will get the first 10 branch protection rules
// If you have more than 10 rules defined, you need to update the number
const branchProtectionRules = await gql(
`
query getBranchRuleID($owner: String!, $repo: String!, $first: Int = 10) {
repository(owner: $owner, name: $repo) {
branchProtectionRules(first: $first) {
edges {
node {
id
pattern
}
}
}
}
}
`,
{
owner: OWNER,
repo: REPO,
}
);
// Find the branch role node that starts with the @prefix arg
const branchProtectionNode =
branchProtectionRules.repository.branchProtectionRules.edges.find((edge) =>
edge.node.pattern.startsWith(prefix)
);
return branchProtectionNode.node.id;
};
async function main() {
const gql = graphql.defaults({
headers: {
authorization: `token ${GITHUB_TOKEN}`,
},
});
const branchProtectionRuleID = await getBranchProtectionRuleID(
gql,
"release/"
);
await allowDeleteProtectedBranch(gql, branchProtectionRuleID, true);
const branches = await getBranches(gql, "release/", 5);
for (let branchId of branches) {
await deleteBranch(gql, branchId);
}
await allowDeleteProtectedBranch(gql, branchProtectionRuleID, false);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment