phpro / grumphp

A PHP code-quality tool
MIT License
4.11k stars 429 forks source link

git_branch_name white list validation is not working #1061

Closed prafullazee closed 1 year ago

prafullazee commented 1 year ago
Q A
Version 1.5.1
Bug? yes
New feature? no
Question? no
Documentation? no

My configuration

 git_branch_name:
            whitelist:
                # allowed branch names: 'feature/JIRA-1234', 'bugfix/JIRA-1234', 'hotfix/JIRA-1234', 'hotfix/v1.0.1', 'release/v1.1.10' etc
                - "/(hotfix|bugfix|feature)\/([JIRA]){4}-([0-9]+){4}$/"
                - "/(hotfix|release)\/([v])([0-9]+){1,2}.([0-9]){1,2}.([0-9]){1,2}$/"
                - "main"

Steps to reproduce:

./vendor/bin/grumphp run

Result:

git_branch_name
===============

Whitelist rule not matched: /(hotfix|bugfix|feature)/([JIRA]){4}-([0-9]+){4}$/
Whitelist rule not matched: /(hotfix|release)/([v])([0-9]+){1,2}.([0-9]){1,2}.([0-9]){1,2}$/

Here is the regex test: https://regex101.com/r/zlHJL6/1 which is passing the branch names.

techdaddies-kevin commented 1 year ago

You need to DOUBLE escape your forward slashes, because what's happening is that the string is being escaped before being passed to preg_match(), so what reaches preg_match() is actually an invalid regex pattern. I just finished dealing with this and came here to see if anyone else had had the problem.

 git_branch_name:
            whitelist:
                # allowed branch names: 'feature/JIRA-1234', 'bugfix/JIRA-1234', 'hotfix/JIRA-1234', 'hotfix/v1.0.1', 'release/v1.1.10' etc
                - "/(hotfix|bugfix|feature)\\/([JIRA]){4}-([0-9]+){4}$/"
                - "/(hotfix|release)\\/([v])([0-9]+){1,2}.([0-9]){1,2}.([0-9]){1,2}$/"
                - "main"
prafullazee commented 1 year ago

@techdaddies-kevin awesome, thank you so much.