7

i'm trying to set rules for set branches on my repo, but having issues with the pattern to apply only to specific branches. ie rule to apply only to brances master,develop,release

Issue: the pattern isn't picking up the wrong branches

I tried looking through here, but it's working as expected https://ruby-doc.org/core-2.5.1/File.html#method-c-fnmatch

I've tried, but not working with 0 branches or everything selected:

{^.*(master).*$,^.*(develop).*$} [master,develop,release] [master;develop;release]*

ps this one dose the opposite an applied all brances, but the listed ones: *[!master|!develop|!release]*

torek
  • 448,244
  • 59
  • 642
  • 775
ASH
  • 980
  • 1
  • 9
  • 22
  • 2
    My understanding is that the pattern is a glob "expression" and not a regular expression, as you have interpreted it. So you can do (say) release-* but not as you are trying to do = you have to have 3 separate rules. My approach to this, in that I think what would be a single rule changes, is to script it with a github action to set the rules using the API. Surprisingly though, the API does not even have the pattern approach - you have to set it explicitly on each branch you want it for. – johnfo Jul 30 '21 at 07:11
  • I've opened a community feedback post asking for [regex as a new feature](https://github.com/orgs/community/discussions/53523) – Richard Tyler Miles Apr 23 '23 at 05:54

4 Answers4

3

As johnfo says in a comment, GitHub's branch protection rule patterns are not regular expressions. In fact, the GitHub documentation mentions that they are specifically Ruby File::FNM_PATHNAME style expressions. This is highly specific (Git itself uses no Ruby code at all).

I snipped the tag; curiously, you included yourself, which is the right tag for someone who might like to supply the right Ruby expressions here. It looks like GitHub do not set the FNM_EXTMATCH flag, so you probably need multiple match expressions (also noted in the comment above). I wouldn't bother answering except that it seemed useful to add some links.

torek
  • 448,244
  • 59
  • 642
  • 775
2

I have a work around, but i'm sure there is a better solution, or rite solution for this. I basically use the oder of precedence of the rules to have only set branches with the rules i want.

  • I target every branch except the ones I want, and set no rule.
  • Then set all branches to require the rule i want to apply to my branches.
  • The first rule takes president and ensures that only the ones i want, get the rule.

NB there is a conflict raised in the rule, be this seems more like a warning

enter image description here

ASH
  • 980
  • 1
  • 9
  • 22
1

You can try [dm]*[pr] for branches starting with 'd' or 'm' and ending with 'p' or 'm' (for develop and master). I'm sure this can be further refined.

arni
  • 2,152
  • 19
  • 14
1

Since the introduction of ruleset in Apr. 2023, the official documentation is "Using fnmatch syntax"

But also:

Using regular expressions for commit metadata

When you add metadata restrictions, you can use regular expression syntax to define patterns that the relevant metadata, such as the commit message or the branch or tag name, must or must not match.

Rulesets support RE2 syntax. For more information, see Google's syntax guide. To validate your expressions, you can use the validator on regex101.com, selecting the "Golang" flavor in the left sidebar.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250