0

Various posts (including Github API - create branch?) give the API calls to make to create a new branch in a github repo, and can obviously be used in a script. I have a slightly different usecase though: I need to create a release branch (like release-1.2.3) when I already have a branch protection rule to cover release-*. The problem: without the branch protection rule, the linked method worked, while with it I get error:

422 Unprocessable Entity

The branch protection rule is fairly standard: requires a PR, requires a status check (I believe only applies to the PR?), "Restrict who can push to matching branches" and "Restrict pushes that create matching branches" are ticked and set to a group that contains all our main team, but includes the user who corresponds to the Jenkins job from where I am running this from.

My guess is that if I can use the new(ish) "bypass branch protections" and clear the "Do not allow bypassing the above settings" settings in the rule would work - can't actually try as yet because I don't have the rights to create the associated custom role but probably will. Alternatively creating the branch on a local repo and pushing it should work given the permissions (yet to try as more scripting but probably will). Question: has anybody got an easier way of doing this?

johnfo
  • 1,676
  • 2
  • 17
  • 28
  • Update on this. Seems the plan B of "creating the branch on a local repo and pushing it" does not work for this user either - expecting status checks to be done and a review, even though the branch does not exist to start with. – johnfo Sep 20 '22 at 08:54

1 Answers1

0

This script may do what you want.

sh ./create-branch-protection-rule.sh \
    github.com \
    <Org or Owner> \
    <Repo> \
    <Branch> \
    requiresApprovingReviews=true \
    allowsDeletions=false \
    allowsForcePushes=false \
    requiredApprovingReviewCount=1 \
    dismissesStaleReviews=true \
    requiresCommitSignatures=true \
    requiresLinearHistory=true \
    lockBranch=true \
    restrictsPushes=true \
    restrictsReviewDismissals=true \
    requireLastPushApproval=true \
    bypassForcePushActorIds="User,Team" \
    reviewDismissalActorIds="Team,App,User,Team"

An Input Schema Reference is found on the GitHub GraphQL page.

The main challenge is that GitHub haven't implemented full support into the RestAPI.

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • I originally expected to set the branch rules programmatically but my experience, albeit I think a year ago, was that you could create a rule with wildcards via the web UI bit not via the REST API. Sounds like that is still the same. – johnfo Mar 27 '23 at 07:44