Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 20

Branch Protection on multiple branches

Solved! Go to Solution.

I'm wondering if there's a way to apply branch protections in a regex kind of way. Here's an example, I have multiple branches like release-1.0, release-1.1 and I'd like them to all have the same branch protections. More importantly, when I create a new release-2.0 I'd like it to automatically be given branch protection with the same configuration as release-1.0 and release-1.1.

 

One way I've done this in other SCM management tools is by specifying a regular expression. Something to the effect of /^release-/

 

Is this possible?

19 Replies
Community Manager
Message 2 of 20

Re: Branch Protection on multiple branches

Hi @clphillips,

 

Currently it is not possible to apply branch protections automatically in that way. However, we are looking to improve this feature over time, so I will pass on your feedback.

In the meantime, you can use the protected branches API endpoint to script some automatic branch protection based on the branch name.

 

Hope this helps!

Solution
Copilot Lvl 3
Message 3 of 20

Re: Branch Protection on multiple branches

It looks like it has been introduced this weekend. I can now create a protection with wildcards for something like release/* and it will automatically show all branches it affects, presumably also all future ones. Long overdue feature as other repo management systems had this for a while longer already, I'm glad it is here now.

Highlighted
Copilot Lvl 2
Message 4 of 20

Re: Branch Protection on multiple branches

Hello Sfynx,

 

seeing this new feature, I tried the same approach as you. However despite having many release branches, the branch restriction page is saying that "Applies to 0 branches".

Must I do something special to make it work ? As suggested I set the pattern to "release/*".

 

Many thanks

Community Manager
Message 5 of 20

Re: Branch Protection on multiple branches

Hi @OPeyrusse,

 

Branch protection rule patterns are based on fnmatch syntax. You could use releases/v?.? to automatically protect branches like releases/v1.0releases/v2.0, and releases/v2.1. And [1-9]-[0-9]-stable could automatically protect branches like 1-0-stable2-0-stable, and 2-1-stable. To learn more about branch protection checkout configuring protected branches.

 

I hope this helps!

Best,

Andrea

Thanks for being here, sharing, and keeping our community awesome!
Best,
AndreaG

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Copilot Lvl 2
Message 6 of 20

Re: Branch Protection on multiple branches

Thanks @AndreaGriffiths11 for your answer.

It appears that `/` are treated specially. For me, the pattern `releases*` was not matching "releases/v1" but `releases/*` or any of your suggestions were working.

I also tested this on longer branch names, like "hotfix/client/project/1.7" and only the pattern `hotfix/*/*/*` was working. Removing intermediate "/" was breaking the pattern. It puzzled me as fnmatch documentation and its examples seem to imply that a standard `hotfix/*` or at least `hotfix/**` should have worked.

Thanks a lot for your help.

Ground Controller Lvl 1
Message 7 of 20

Re: Branch Protection on multiple branches

I am not able to use github api to add branch protection for a branch that does not exists or a branch with wildcard. 

 

Wanted to automate adding restrictions for all release branches (release/*) for all repos. Seems it does not work today and its a pain to add it manually as we have 100s of repos.

 

Ex: 

https://api.github.com/repos/:owner/:repo/branches/release/*/protection

 

Is there a way to do this automagically :) ?? 

 

 

Moderator
Message 8 of 20

Re: Branch Protection on multiple branches

That's not possible at this time. However, I will share your feedback with the appropriate teams. I can see how this would be valuable! 

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Copilot Lvl 2
Message 9 of 20

Apply branch protection to two branches with entirely different names.

This would be great. Please!

 

I read in the another post that `{master,develop}` is not enabled in fnmatch. Can you provide another mechanism? Perhaps a UI with checkboxes as an alternative to pattern matching? That would be great!

Copilot Lvl 2
Message 10 of 20

Re: Apply branch protection to two branches with entirely different names.

+1 on adding support for this.

 

Looking more at the fnmatch documentation, it looks like there's a File::FNM_EXTGLOB option that can be passed to enable{master,develop} to work. This option is disabled by default. Enabling it would save so much time for our organization when new feature branches are created that multiple people will work off of.