Skip to content

Commit

Permalink
feat: add match inversion for branch and tag patterns (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRibeiro authored May 5, 2022
1 parent bc6e959 commit 2d33594
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ jobs:
event: always
template: MY_ORB_TEMPLATE
- slack/notify
# Should run for every branch but master
- slack/notify:
branch_pattern: "master"
invert_match: true

workflows:
test-deploy:
Expand Down
7 changes: 7 additions & 0 deletions src/commands/notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ parameters:
A comma separated list of regex matchable tag names. Notifications will only be sent if sent from a job from these branches. By default ".+" will be used to match all tags. Pattern must match the full string, no partial matches.
type: string
default: ".+"
invert_match:
description: |
Invert the branch and tag patterns.
If set to true, notifications will only be sent if sent from a job from branches and tags that do not match the patterns.
type: boolean
default: false
mentions:
description: |
Exports to the "$SLACK_PARAM_MENTIONS" environment variable for use in templates.
Expand Down Expand Up @@ -88,6 +94,7 @@ steps:
SLACK_PARAM_MENTIONS: "<<parameters.mentions>>"
SLACK_PARAM_BRANCHPATTERN: "<<parameters.branch_pattern>>"
SLACK_PARAM_TAGPATTERN: "<<parameters.tag_pattern>>"
SLACK_PARAM_INVERT_MATCH: "<<parameters.invert_match>>"
SLACK_PARAM_CHANNEL: "<<parameters.channel>>"
SLACK_PARAM_IGNORE_ERRORS: "<<parameters.ignore_errors>>"
SLACK_PARAM_DEBUG: "<<parameters.debug>>"
Expand Down
7 changes: 7 additions & 0 deletions src/jobs/on-hold.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ parameters:
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.
type: string
default: ".+"
invert_match:
description: |
Invert the branch and tag patterns.
If set to true, notifications will only be sent if sent from a job from branches and tags that do not match the patterns.
type: boolean
default: false
mentions:
description: |
Exports to the "$SLACK_PARAM_MENTIONS" environment variable for use in templates.
Expand Down Expand Up @@ -53,6 +59,7 @@ steps:
event: always
template: <<parameters.template>>
branch_pattern: <<parameters.branch_pattern>>
invert_match: <<parameters.invert_match>>
custom: <<parameters.custom>>
mentions: <<parameters.mentions>>
channel: <<parameters.channel>>
Expand Down
12 changes: 9 additions & 3 deletions src/scripts/notify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@ FilterBy() {
return
fi

# Add the "invert-match" flag to grep if it is set.
INVERT_MATCH=""
if [ "$SLACK_PARAM_INVERT_MATCH" -eq 1 ]; then
INVERT_MATCH="--invert-match"
fi

# If any pattern supplied matches the current branch or the current tag, proceed; otherwise, exit with message.
# However, if the invert_match parameter is set, invert the match.
FLAG_MATCHES_FILTER="false"
for i in $(echo "$1" | sed "s/,/ /g")
do
if echo "$2" | grep -Eq "^${i}$"; then
for i in $(echo "$1" | sed "s/,/ /g"); do
if echo "$2" | grep -Eq ${INVERT_MATCH:+"$INVERT_MATCH"} "^${i}$"; then
FLAG_MATCHES_FILTER="true"
break
fi
Expand Down

0 comments on commit 2d33594

Please sign in to comment.