In this page

Conditional executions

When implementing automation rules, you may want to run some of those with limited scope: only in a subset of your repositories or only for a subset of your branches, for example. These scopes can be easily implemented using the Advanced Compare Condition in the rule:

For more complicated decisions you can check issue fields and attachments, filter issues with JQL or create if-else switches using other condition types.

The following sections give step-by-step guides to the most frequent conditional situations. They assume that there is an already working automation rule that you want to make conditional now.

Please note that even if the condition evaluates to false and the execution ends, there is a new entry with the "NO ACTIONS PERFORMED" result added to the audit log. This is actually helpful as it indicates that the rule was triggered (and ended) as expected.

Execute automation rule only in repository A and B

The idea is to insert an Advanced compare condition block before your action in the rule. The condition matches a regular expression to the repository ID, counts the matches, and continues the execution only if there is at least one match.

Steps:

  1. Edit your automation rule.
  2. Move your mouse between the trigger and the action on the left, and click Add component.
  3. Click New condition, and select Advanced compare condition.
  4. Enter one of these expressions to the First value field:
    ## Bitbucket: in the original (non-fork) repositories only
    {{changesetHelper.matchRepository(devops.changeset,"[^~]+").size}}
    
    ## in the "foo" repository only
    {{changesetHelper.matchRepository(devops.changeset,".*[\\\/]foo(.git)?").size}}
    
    ## in two specific repositories
    {{changesetHelper.matchRepository(devops.changeset,".*[\\\/](repo-a|repo-b)(.git)?").size}}
    
    ## in all repositories whose name contains android
    {{changesetHelper.matchRepository(devops.changeset,".*android.*").size}}
    
    ## the same checks work for commits, too (in case of the Genius Commit Created trigger)
    {{changesetHelper.matchRepository(devops.commit,".*[\\\/]foo(.git)?").size}}
    
    Notes:
    • You can use regular expressions as the matcher patterns in green. Don't forget the "\" escaping character before the "/" character!
  5. Choose the does not equal option from the Condition dropdown.
  6. Enter "0" in the Second value field.
  7. Click Save, then Publish changes.
How to find out the repository ID?

The repository ID is a unique identifier of a repository. It uses a proprietary format which depends on the computer that hosts the repository, on the repository URL and on the repository location. It may not be very intuitive for the first sight, therefore here is a guide to figure this out.

The idea is to create an automation action that writes the repository ID to the rule audit log when a changeset is accepted. You can then go to the audit log and see the repository ID there.

Steps:

  1. Login to Jira as admin, go to AdministrationSystemAutomation rules.
  2. Click Create rule.
  3. Select the trigger Changeset accepted (from the DevOps category).
  4. Click Save.
  5. Click New action.
  6. Select the action Log action.
    1. Enter {{devops.changeset.repositoryId}} in the Log message field.
  7. Click Save.
  8. Name your automation rule intuitively, and click Turn it on.
  9. Create an arbitrary commit.
  10. Go to the Audit log screen of the rule.
  11. Click the Show more link of the latest execution on the right.
  12. The repository ID is shown with green background color:
    • Remote repositories: it is the repository URL like http://admin@localhost:7990/scm/my-project/myrepo.git
    • Local repositories: it is in the format "hostname:repository location" like DESKTOP-Q3395CE:d:\my-project\myrepo
  13. If you gave the rule an intuitive name like "Log repository ID", then you could just disable it and keep it for future use. (If you are sure that you won't need it anymore, you could now delete the rule, as its only purpose was to find out the repository ID.)

Please note that, because you are typically hosting all your repositories on the same server, only that fragment of the ID that depends on the repository name will be varying.

Execute automation rule only for branch A and B

The idea is to insert an Advanced compare condition block before your action in the rule. The condition matches a regular expression to the branch name, counts the matches, and continues the execution only if there is at least one match.

Steps:

  1. Edit your automation rule.
  2. Move your mouse between the trigger and the action on the left, and click Add component.
  3. Click New condition, and select Advanced compare condition.
  4. Enter one of these expressions to the First value field:
    ## for the "master" branch only
    {{changesetHelper.matchBranches(devops.changeset,"master").size}}
    
    ## for two specific branches
    {{changesetHelper.matchBranches(devops.changeset,"(branch-a|branch-b)").size}}
    
    ## for all feature branches
    {{changesetHelper.matchBranches(devops.changeset,"feature\/.*").size}}
    
    Notes:
    • If you are using the Genius Commit created trigger, replace devops.changeset with devops.commit in the expressions above.
    • You can use regular expressions as the matcher patterns in green. Don't forget the "\" escaping character before the "/" character!
  5. Choose the does not equal option from the Condition dropdown.
  6. Enter "0" in the Second value field.
  7. Click Save, then Publish changes.

Execute automation rule only for committer A and B

The idea is to insert an Advanced compare condition block before your action in the rule. The condition matches a regular expression to the committer name, counts the matches, and continues the execution only if there is at least one match.

Steps:

  1. Edit your automation rule.
  2. Move your mouse between the trigger and the action on the left, and click Add component.
  3. Click New condition, and select Advanced compare condition.
  4. Enter one of these expressions to the First value field:
    ## for "alice" only
    {{changesetHelper.matchCommitters(devops.changeset,"alice").size}}
    
    ## for "alice" and "bob" only
    {{changesetHelper.matchCommitters(devops.changeset,"(alice|bob)").size}}
    
    ## for all users whose name starts with "ext-"
    {{changesetHelper.matchCommitters(devops.changeset,"ext-.*").size}}
    
    Notes:
    • If you are using the Genius Commit created trigger, replace devops.changeset with devops.commit in the expressions above.
    • You can use regular expressions as the matcher patterns in green.
  5. Choose the does not equal option from the Condition dropdown.
  6. Enter "0" in the Second value field.
  7. Click Save, then Publish changes.

Execute automation rule only in directory A and B

The idea is to insert an Advanced compare condition block before your action in the rule. The condition matches a regular expression to the changed file paths, counts the matches, and continues the execution only if there is at least one match.

Steps:

  1. Edit your automation rule.
  2. Move your mouse between the trigger and the action on the left, and click Add component.
  3. Click New condition, and select Advanced compare condition.
  4. Enter one of these expressions to the First value field:
    ## for the "src" directory only
    {{changesetHelper.matchPaths(devops.changeset,"src\/.*").size}}
    
    ## for the "src" and "test/java" directories only
    {{changesetHelper.matchPaths(devops.changeset,"(src\/.*|test\/java\/.*)").size}}
    
    ## for file modifications in the "test/java" directory
    {{changesetHelper.matchPaths(devops.changeset,"test\/java\/.*", "m").size}}
    
    ## for file additions or deletions in any directory (note the argument "a,d"!)
    {{changesetHelper.matchPaths(devops.changeset,".*", "a,d").size}}
    
    Notes:
    • If you are using the Genius Commit created trigger, replace devops.changeset with devops.commit in the expressions above.
    • You can use regular expressions as the matcher patterns in green. Don't forget the "\" escaping character before the "/" character!
    • For available values of actions, see the action codes for files in the smart value reference table.
  5. Choose the does not equal option from the Condition dropdown.
  6. Enter "0" in the Second value field.
  7. Click Save, then Publish changes.

Execute automation rule only for file A and B

Follow the instructions in the execute rule only in directory A and B recipe above, with the only difference of using a regular expression in step 4 that matches file names instead of directories:

## for the "build.xml" file only
{{changesetHelper.matchPaths(devops.changeset,"build\.xml").size}}

## for the "pom.xml" and "img/logo.jpg" files only
{{changesetHelper.matchPaths(devops.changeset,"(pom\.xml|img\/logo\.jpg)").size}}

## for all Java files
{{changesetHelper.matchPaths(devops.changeset,"(.*\.java)").size}}

## for the deletion of any Java file (note the argument "d"!)
{{changesetHelper.matchPaths(devops.changeset,"(.*\.java)","d").size}}

Questions?

Ask us any time.