Slack's branch_pattern parameter

Hello guys. I want to set up the branch_pattern for my slack/notify jobs in the config.yml file, but there are some issues.

The description for the branch_pattern says:

A comma separated list of regex matchable branch names. Notifications will only be sent if sent from a job from these branches. By default ".+" will be used to match all branches. Pattern must match the full string, no partial matches.

But if I put there “a comma separated list”:

      - slack/notify:
          branch_pattern:
            - /feature\/.*/, 
            - /hotfix\/.*/, 
            - /fix\/.*/, 
            - /h\/.*/, 
            - /f\/.*/
          event: fail
          template: CYPRESS_FAIL_1
          channel: channel

I get a build error: Type error for argument branch_pattern: expected type: string, actual value: \"clojure.lang.LazySeq@29951265\" (type sequence) error.

When I use string:

      - slack/notify:
          branch_pattern: /feature\/.*/, /hotfix\/.*/, /fix\/.*/, /h\/.*/, /f\/.*/
          event: fail
          template: CYPRESS_FAIL_1
          channel: channel

Then the pattern does not match and the notification is not sent:
Current reference "f/axi-1528" does not match any matching parameter
Current matching pattern: /feature\/.*/, /hotfix\/.*/, /fix\/.*/, /h\/.*/, /f\/.*/

Could you please advise how to use multiple regex patterns in the branch_pattern parameter?

The issue comes in part from the fact the writer of the ORB decided to hide the complexity of the slack call by using a single line for the shell script - all 8,143 characters of it.

If you try and decode the shell script which may as well have been put through an obfuscator you end up with the following

- parameters.branch_pattern gets passed to the script as  SLACK_PARAM_BRANCHPATTERN
- SLACK_PARAM_BRANCHPATTERN is used only once as parameter to an inline function called FilterBy
- FilterBy  
   -  checks to make user the passed values are not an empty string.
   -  checks the invert match flag
   -  passes the pattern string as a list separated by commas
        looks for any entry in the list that matches the branch - USING 'grep -E'.

The result is that the description of the match is totally misleading - GREP is used, not the regex check you expect within the CircleCI ecosystem. This is followed up in the docs with the rather stupid statement “Pattern must match the full string, no partial matches.”.

So use the single string you have in your second example, but try dropping all the escaping as an example

echo "f/axi-1528" | grep -E "/f\/.*/"

This returns no match

echo "f/axi-1528" | grep -E "f/.*"

This matches

[updated example to use values from within the original question]

1 Like

That helped. Thank you for explaining it to me.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.