Sraw
Sraw

Reputation: 20214

How to apply one github branch rule to multiple branches?

I mean I want to create one rule and specify multiple branches like dev|master. But after seeing the doc, I think it is impossible?? Do I have to create two rules just in order to use the same rule to protect two branches?

Upvotes: 144

Views: 78312

Answers (9)

Rodrigo Arias
Rodrigo Arias

Reputation: 1

Based on the explanations above, [mds][aet][iva][neg]* works for develop, staging, and main. Of course, it also matches other combinations.

Upvotes: -3

dandush03
dandush03

Reputation: 255

Following z4-tear great explination This will cover development master and staging

[dms][tea][avs]*[iet][ne][gtr]

Upvotes: 8

Jackie Santana
Jackie Santana

Reputation: 1458

to apply to all, add --> **/**

Upvotes: 9

Z4-tier
Z4-tier

Reputation: 7978

I found a rather ugly way to do this that at least gets in the ballpark (although it would be a lot better if @GitHub would give us something better than fnmatch with all options off...).

You can use character sets to specify the beginning characters in the repo name, like this:

(Using "main" branch): [dm][ea][vi]*
(Using "master" branch): [dm][ea][vs]*

It will match dev and main/master which is what you want, but the second one will also match "mastodon-rules" and "devo-is-my-favorite-band" due to the wildcard. I don't think fnmatch give you a "zero-or-one" quantifier like the regex ? so it's pretty restrictive.

Github fnmatch does allow the negation of a character set, so if a rule is catching branches you don't want to include, you might be able to get around that:

(using "main" branch): [dm][ea][vi][!o]*
(using "master" branch): [dm][ea][vs][!o]*

This will miss the dev branch (it will catch develop and main/master though...), but it excludes "devo" so at least 'whip it' won't start playing during your next all-night thrash session with your metalhead buddies.

Admittedly, this is not a very satisfying solution. But with fnmatch this might be the best option available.


What You Should Not Do

There are multiple other answers claiming that this pattern (or a similar variant) will work just fine:

[main,qa,stage,master]*

DO NOT BE LURED BY THIS SIRENS SONG

The engine treats characters enclosed in square [] brackets as just that: individual characters. Adding commas (or semicolons, or any other "separator") does not change that behavior.

Square Brackets: "match any one of the enclosed characters"
Star: "match any string of any length"

So, while this pattern will certainly match the words in the brackets, it will also match any string of any length that starts with one of the characters in the brackets: [aegimnqrst,].

Upvotes: 158

Tarun Saxena
Tarun Saxena

Reputation: 5

Enable it on develop, master but not test

[develop;master]*[!test]*

Upvotes: -6

Antony Fagundez
Antony Fagundez

Reputation: 29

For anyone that need a rule for covering only dev and main, its possible with this syntax:

https://ruby-doc.org/core-2.5.1/File.html#method-c-fnmatch

[cd]*[vd]

CONS

Will match with everithing that starts with c or d, and ends with v and d

Upvotes: 2

alacret
alacret

Reputation: 619

They already enable wildcards. So this pattern works:

[main,qa,stage,master]*

Upvotes: -19

chharvey
chharvey

Reputation: 9326

According to the GitHub documentation, they use the fnmatch library for the pattern field. That syntax allows an alternation:

{a,b}

Matches pattern a and pattern b if File::FNM_EXTGLOB flag is enabled. Behaves like a Regexp union ((?:a|b)).

For your problem, the pattern you’re looking for might be {dev,master}.

I don’t know what they mean by “if File::FNM_EXTGLOB flag is enabled”, so this might not work.

Upvotes: -14

foakesm
foakesm

Reputation: 923

Have also been trying to get my head around this this this morning, I believe you(/we) may have to create two identical rules for each branch oddly. At least that's what I believe after reading through:

https://github.community/t5/How-to-use-Git-and-GitHub/Apply-a-single-branch-protection-rule-to-both-master-and-release/td-p/11587

Comment from Moderator:

"No, there isn't a way to do that in the "Apply rule to" box. As stated in the protected branches documentation, we use the fnmatch library to match branch names to the match expression. There is a feature that would allow for matching two rules like that if there is a flag enabled but we don't enable that flag in our environment."

OR you could use this solution if you want to apply one rule to all branches beginning with or including the same matching phrase:

https://github.community/t5/How-to-use-Git-and-GitHub/Branch-Protection-on-multiple-branches/td-p/10519

Comment from Community Manager:

Branch protection rule patterns are based on fnmatch syntax. You could use releases/v?.? to automatically protect branches like releases/v1.0, releases/v2.0, and releases/v2.1. And [1-9]-[0-9]-stable could automatically protect branches like 1-0-stable, 2-0-stable, and 2-1-stable.

Upvotes: 13

Related Questions