Mergify Conditions

Everything you need to know when writing conditions in Mergify


Conditions are an essential component of Mergify rules, as they define the criteria that must be met for a rule to be applied. By utilizing conditions, you can create highly customizable and flexible rules tailored to your specific workflow requirements. Conditions enable you to target pull requests based on various factors such as their status, properties, labels, and associated statuses or checks.

In this guide, we will explore the structure, syntax, and usage of conditions in Mergify rules. You will learn how to craft effective conditions that help you automate and streamline your pull request management process.

A condition in Mergify rules is composed of three key components: attributes, operators, and values, along with optional modifiers. These components work together to define the criteria that must be met for the condition to be considered true.

The grammar for a condition is as follows:

[ "-" ] [ "#" ] <attribute> [ <operator> <value> ]

  • The optional minus (-) operator at the beginning of the condition negates the result of the condition, acting as a “not” operator.

  • The optional hash (#) operator is used to evaluate the length of a list when the attribute is a list.

Attributes represent the properties or characteristics of a pull request that you want to evaluate in your condition. These can include elements such as the number of approvals, the presence of specific labels, or the status of CI checks.

Operators are used to compare the attribute to a specified value. Mergify supports various comparison operators (e.g., =, !=, <, >).

When the attribute is a list, comparison operators behave as if “any” is used on the list, meaning the condition will return true if any element of the list matches the given operator and value.

Values are the reference points used to evaluate the attribute. They can be strings, numbers, or booleans, depending on the attribute being evaluated.

For example, consider the following condition:

#approved-reviews-by >= 2

In this condition:

  • The attribute is approved-reviews-by, which represents the list of approved reviewers for a pull request.

  • The operator is >=, which means “greater than or equal to.”

  • The value is 2, which indicates the minimum number of approved reviews required for the condition to be true.

  • The hash (#) operator is used to evaluate the length of the list of approved reviewers.

Understanding the structure of a condition is crucial for creating effective Mergify rules that cater to your specific needs and requirements.

Attributes are properties of pull requests or events used to filter and evaluate the conditions within rules. They can be used to create more specific and targeted rules for your repository.

Pull Request Attributes
Attribute nameValue typeDescription
added-fileslist of string

The files that are added by the pull request.

added-lineslist of string

The lines that are added by the pull request. Only usable as #added-lines for the number of added lines.

approved-reviews-bylist of string

The list of GitHub user or team login that approved the pull request. Team logins are prefixed with the @ character and must belong to the repository organization. This only matches reviewers with admin, write or maintain permission on the repository.

assigneelist of string

The list of GitHub user or team login that are assigned to the pull request. Team logins are prefixed with the @ character and must belong to the repository organization.

authorstring

The GitHub user or team login of the author of the pull request.

basestring

The name of the branch the pull request should be pulled into.

bodystring

The content of the pull request description without Markdown/HTML comments.

body-rawstring

The content of the pull request description.

branch-protection-review-decisionboolean

The review decision. This indicates if CODEOWNERS have reviewed the pull request when the Require Review from Code Owners branch protection rule is enabled.

changes-requested-reviews-bylist of string

The list of GitHub user or team login that have requested changes in a review for the pull request.

checklist of string

The list of checks for that pull request.

check-failurelist of string

The list of checks that failed for the pull request. Checks that report being cancelled, timed out, and action required are also considered as failures.

check-neutrallist of string

The list of checks that are neutral for the pull request.

check-pendinglist of string

The list of checks that are pending for the pull request.

check-skippedlist of string

The list of checks that was skipped for the pull request.

check-stalelist of string

The list of checks that are stale for the pull request.

check-successlist of string

The list of checks that successfully passed for the pull request.

check-timed-outlist of string

The list of checks that timed out for the pull request.

closedboolean

Whether the pull request is closed.

closed-atdate-time or null

The time the pull request was closed at.

co-authorslist of CommitAuthor

The list of co-authors on the pull request (excluding merge commits and bots).

commented-reviews-bylist of string

The list of GitHub user that have commented in a review for the pull request. This only matches reviewers with admin, write or maintain permission on the repository.

commitslist of Commit

The list of commits of the pull request. The index 0 is the first commit, while -1 is the last commit.

commits-behindlist of string

The list of commits between the head of the base branch and the base of the pull request. This can only be used with the length operator as #commits-behind.

commits-unverifiedlist of string

The list of commit messages that are marked as unverified by GitHub.

conflictboolean

Whether the pull request is conflicting with its base branch.

created-atdate-time

The time the pull request was created at.

current-datetimedate-time

The current date and time.

deleted-lineslist of string

The lines that are deleted by the pull request. Only usable as #deleted-lines for the number of deleted lines.

dependabot-dependency-namelist of string

The dependency-name value included in the Dependabot commit message.

dependabot-dependency-typelist of string

The dependency-type value included in the Dependabot commit message.

dependabot-update-typelist of string

The update-type value included in the Dependabot commit message.

depends-onlist of string

The list of dependencies to other pull request in the format owner/repo#prnumber.

dismissed-reviews-bylist of string

The list of GitHub user login that have their review dismissed in the pull request.

draftboolean

Whether the pull request is in draft state.

fileslist of string

The files that are modified, deleted or added by the pull request.

headstring

The name of the branch where the pull request changes are implemented.

head-repo-full-namestring

The head branch repository full name (complete version with the organization name).

labellist of string

The list of labels of the pull request.

linear-historyboolean

Whether the pull request commits history is linear (no merge commit).

lockedboolean

Whether the pull request is locked.

merge-commit-shastring or null

The merge commit SHA of the pull request returned by GitHub.

mergedboolean

Whether the pull request is merged.

merged-atdate-time or null

The time the pull request was merged at.

merged-bystring or null

The GitHub user that merged the pull request.

mergify-configuration-changedboolean

Whether the pull request contains changes in the configuration file.

milestonestring or null

The milestone title associated to the pull request.

modified-fileslist of string

The files that are modified by the pull request.

modified-lineslist of string

The lines that are modified by the pull request. Only usable as #modified-lines for the number of modified lines.

numberinteger

The pull request number.

queue-dequeue-reasonNONE, PR_MERGED, PR_DEQUEUED, PR_AHEAD_DEQUEUED, PR_AHEAD_FAILED_TO_MERGE, PR_WITH_HIGHER_PRIORITY_QUEUED, PR_QUEUED_TWICE, SPECULATIVE_CHECK_NUMBER_REDUCED, CHECKS_TIMEOUT, CHECKS_FAILED, QUEUE_RULE_MISSING, UNEXPECTED_QUEUE_CHANGE, PR_FROZEN_NO_CASCADING, BASE_BRANCH_MISSING, BASE_BRANCH_CHANGED, PR_UNEXPECTEDLY_FAILED_TO_MERGE, BATCH_MAX_FAILURE_RESOLUTION_ATTEMPTS, PR_CHECKS_STOPPED_BECAUSE_MERGE_QUEUE_PAUSE, CONFLICT_WITH_BASE_BRANCH, CONFLICT_WITH_PULL_AHEAD, BRANCH_UPDATE_FAILED, DRAFT_PULL_REQUEST_CHANGED, PULL_REQUEST_UPDATED, MERGE_QUEUE_RESET or INCOMPATIBILITY_WITH_BRANCH_PROTECTIONS or null

A dequeue code for when a pull request has been disembarked from the merge queue.

queue-freeze-reasonstring or null

The reason for the queue containing the pull request to be frozen.

queue-frozenboolean

Whether the pull request is in a queue that is frozen.

queue-merge-started-atdate-time or null

The time the pull request mergeability checks have started at. This attribute does not work yet when using partition rules.

queue-namestring or null

The name of the queue containing the pull request.

queue-partition-namestring or null

The name of the partitions the pull request is queued in.

queue-positioninteger

The position of the pull request in its queue if queued. The first pull request in the queue has position 0. The value is set to -1 if the pull request is not queued. If you are using partitions, this returns the maximum position of the pull request from all the partitions it is queued in.

queued-atdate-time or null

The time the pull request was queued at for merge.

removed-fileslist of string

The files that are removed by the pull request.

repository-full-namestring

The current repository full name (complete version with the organization name).

repository-namestring

The current repository name (short version without the organization name).

review-requestedlist of string

The list of GitHub user or team login that were requested to review the pull request. Team logins are prefixed with the @ character. This only matches reviewers with admin, write or maintain permission on the repository.

review-threads-resolvedlist of string

The list of bodies associated to review threads that are marked as resolved by GitHub.

review-threads-unresolvedlist of string

The list of bodies associated to review threads that are NOT marked as resolved by GitHub.

schedulestring

The current time will be compared against this schedule to validate this attribute.

senderstring

The GitHub login of the command author.

sender-permission

GitHubRepositoryPermission

The permission of the command author.

titlestring

The title of the pull request.

updated-atdate-time

The time the pull request was updated at.

Operators allow you to compare attributes with specified values in order to determine if a condition is true or false. Mergify supports several types of operators that cater to different types of comparisons. Familiarizing yourself with these operators is essential for crafting effective conditions in your Mergify rules.

Here is a list of the available operators:

Operator NameSymbolsDescription
Equal

= or :

This operator checks for strict equality. If the target attribute type is a list, each element of the list is compared against the value and the condition is true if any value matches.
Not Equal

!= or

This operator checks for non equality. If the target attribute type is a list, each element of the list is compared against the value and the condition is true if no value matches.
Match

~=

This operator checks for regular expressions matching. If the target attribute type is a list, each element of the list is matched against the value and the condition is true if any value matches.
Greater Than or Equal

>= or

This operator checks for the value to be greater than or equal to the provided value. It’s usually used to compare against the length of a list using the # prefix.
Greater Than

>

This operator checks for the value to be greater than the provided value. It’s usually used to compare against the length of a list using the # prefix.
Lesser Than or Equal

<= or

This operator checks for the value to be lesser then or equal to the provided value. It’s usually used to compare against the length of a list using the # prefix.
Lesser Than

<

This operator checks for the value to be lesser than the provided value. It’s usually used to compare against the length of a list using the # prefix.

For example, let’s consider a condition that requires a pull request to have a specific label:

label = bugfix

In this condition:

  • The attribute is label, which represents the labels associated with a pull request.

  • The operator is =, which means “equal to.”

  • The value is bugfix, which is the label we want the pull request to have.

Some attributes have a type of list. Most operators are able to match value against lists: they will iterate over all the values of the list and return true if any of the value matches.

For example, the label attribute is a list of string containing the names of the label attached to a pull request. With a pull request whose labels are (bug, work-in-progress), then:

  • label = work-in-progress is true because there is a label named work-in-progress.

  • label = enhancement is false because there is no label named enhancement.

  • label != work-in-progress is false because there is a label named work-in-progress.

  • label ~= ^work is true because there is a label matching the regular expression ^work.

  • -label ~= ^work is false because there is a label matching the regular expression ^work but the condition is reversed with the - prefix.

The same applies for the files attribute — which contains the list of modified files:

  • files = README is true if the file README is modified in the pull request.

  • files != README is true if the file README is not modified in the pull request.

  • files ~= ^src/ is true if any files in the src directory is modified in the pull request.

  • -files ~= ^src/ is true if none of the files that are modified are in the src directory.

  • files ~= ^(README.md|CONTRIBUTING.md)$ is true if the file README.md or CONTRIBUTING.md is modified in the pull requests.

Apply the rule only to pull requests with a specific milestone

Section titled Apply the rule only to pull requests with a specific milestone

In this example, we use the = operator to match a specific milestone for the pull request:

- name: Rule for milestone "v1.0"
  conditions:
    - milestone = v1.0
  actions:
    # Your actions here

Apply the rule only when the number of changes is less than or equal to a certain threshold

Section titled Apply the rule only when the number of changes is less than or equal to a certain threshold

In this example, we use the <= operator to check if the number of files modified in the pull request is less than or equal to 50:

- name: Rule for small changes
  conditions:
    - "#files <= 50"
  actions:
    # Your actions here

Apply the rule only when the pull request is labeled with at least one of the specified labels

Section titled Apply the rule only when the pull request is labeled with at least one of the specified labels

In this example, we use the != operator to check if the pull request does not have the specified labels:

- name: Rule when not a bug
  conditions:
    - label != bug
  actions:
    # Your actions here

Combining Conditions Using Logical Operators

Section titled Combining Conditions Using Logical Operators

Mergify allows you to combine multiple conditions using logical operators, making it possible to create more intricate rules that cater to your specific workflow requirements. You can use the and and or logical operators to join conditions together.

The and operator is used to join conditions such that all conditions must be true for the overall condition to be true. In Mergify rules, the and operator is represented by placing conditions on separate lines within the same rule.

Alternatively, you can use the and keyword to explicitly group conditions together.

Example:

pull_request_rules:
  - name: Merge when all conditions are met
    conditions:
      - and:
        - "#approved-reviews-by >= 2"
        - "check-success = test job"
    actions:
      merge:
        method: merge

In this example, the pull request will only be merged if it has at least 2 approved reviews and the test job check passes.

The or operator is used to join conditions such that if any one of the conditions is true, the overall condition is considered true. In Mergify rules, the or operator is represented by using the or keyword and specifying conditions as a list.

Example:

pull_request_rules:
  - name: Merge when either condition is met
    conditions:
      - or:
        - "#approved-reviews-by >= 2"
        - "check-success = test job"
    actions:
      merge:
        method: merge

In this example, the pull request will be merged if it has at least 2 approved reviews or if the test job check passes.

Combining conditions using logical operators can help you create more sophisticated rules tailored to your repository’s workflow and requirements.

Testing and Debugging Conditions

Section titled Testing and Debugging Conditions

When crafting conditions for your Mergify rules, it’s essential to test and debug them to ensure they are working as intended. This section will guide you through the process of testing and debugging conditions in your Mergify configuration.

Use the Mergify Configuration Editor

Section titled Use the Mergify Configuration Editor

The Mergify configuration editor is a useful tool to help you test your conditions and rules on existing pull requests without actually triggering the actions defined in your rules. To access the configuration editor, follow these steps:

  1. Navigate to your Mergify dashboard.
  2. Select the repository you want to test.
  3. Click on the “Config Editor” tab.
Mergify Configuration Editor

In the configuration editor, you can enter your conditions and see how they would be evaluated on your pull requests. You can also test your entire Mergify configuration by copying and pasting it into the configuration editor.

Once you have created and committed your Mergify configuration file, Mergify will evaluate the conditions in your rules for each pull request. To debug your conditions and understand why a rule may not be working as expected, you can analyze the Mergify Checks on a pull request:

  1. Navigate to the “Checks” tab of the pull request.
  2. Look for the “Mergify” check in the list of checks.
  3. Click on “Summary” to view the detailed Mergify report.

In the Mergify report, you can see which rules were evaluated and whether the conditions were met or not. This can help you identify any issues in your conditions and make adjustments as necessary.

Mergify Check Summary