Skip to content

Automation - Integrate gitStream with Python

Auto-Approve Python Log Output Changes

Approve changes to Python files that only affect lines of code that invoke a specified logging object.

approve Python log output

Configuration Description

Conditions (all must be true):

  • All files must end in .py
  • The changes only affect lines of code that invoke a logger object. This should be customized to your environment.

Automation Actions:

  • Apply a log-output-only label
  • Approve the PR
  • Post a comment explaining that the change only affects logging output.

Approve Python Log Output Changes

# -*- mode: yaml -*-

manifest:
  version: 1.0

automations:
  approve_python_log_output:
    # Triggered for python changes that only affect lines of code that invoke a logger object. 
    # Modify 'logger' to match your dev environment.
    if: 
      - {{ files | match(regex=r/\.py$/) | every }}
      - {{ source.diff.files | matchDiffLines(regex=r/^[+-].*logger\.(trace|debug|info|warn|error)/, ignoreWhiteSpaces=true) | every }}
    run: 
      - action: add-label@v1
        args:
          label: 'log-output-only'
      - action: approve@v1
      - action: add-comment@v1
        args:
          comment: |
            This PR has been approved because it only contains changes to log output

Auto-Approve Python Formatting Changes

Approve PRs that only contain formatting changes to Python files.

Approve Python Formatting Changes

Configuration Description

Conditions (all must be true):

  • All of the files end in .py.
  • All changes are non-functional

Automation Actions:

  • Approve the PR
  • Apply a code-formatting label.
  • Post a comment that explains the automation.

Approve Python Formatting Changes

# -*- mode: yaml -*-

manifest:
  version: 1.0

automations:
  approve_python_formatting:
    if:
      - {{ files | extensions | match(list=['py']) | every }}
      - {{ source.diff.files | isFormattingChange }}
    run:
      - action: approve@v1
      - action: add-label@v1
        args:
          label: code-formatting
      - action: add-comment@v1
        args:
          comment: |
            This PR only contains formatting changes and has been approved.

Review Missing Python Tests

Automatically request changes for Python PRs that lack test files.

Review Missing Python Tests

Configuration Description

Conditions (all must be true):

  • The PR creates one or more new Python files
  • The PR lacks new test files that match the name of the Python files

Automation Actions:

  • Apply a red missing-tests label
  • Request changes and post a comment listing the files that need tests.

Review Missing Python Tests

# -*- mode: yaml -*-
manifest:
  version: 1.0

newFiles: {{ source.diff.files | filter(attr='new_file', regex=r/^src\/(?!test_.*\.py$).*\.py$/) | filter(attr='original_file', regex=r/^$/) | map(attr='new_file') }}
newTests: {{ source.diff.files | filter(attr='new_file', regex=r/^tests\/test_.*\.py$/) | filter(attr='original_file', regex=r/^$/) | map(attr='new_file') }}

newFilesCount: {{ source.diff.files | filter(attr='new_file', regex=r/^src\/(?!test_.*\.py$).*\.py$/) | filter(attr='original_file', regex=r/^$/) | length }}
newTestsCount: {{ source.diff.files | filter(attr='new_file', regex=r/^tests\/test_.*\.py$/) | filter(attr='original_file', regex=r/^$/) | length }}

automations:
  review_missing_python_tests:
    if:
      - {{ newFilesCount != newTestsCount }}
    run: 
      - action: add-label@v1
        args:
            label: "⚠️ Missing Tests"
            color: {{ colors.orange }}    
      - action: request-changes@v1
        args:
          comment: |
            Some of your new Python files are missing corresponding tests. Please ensure that all new files have a corresponding test file.

            **New Files**: {{ newFilesCount }}
            {{ newFiles }}

            **New Tests**: {{ newTestsCount }}
            {{ newTests }}
colors:
  orange: 'd93f0b'

Review Python Test Name

Automatically request changes for Python test files that fail to match the required naming convention.

Review Python Test Name

Configuration Description

Conditions (all must be true):

  • The PR creates one or more new Python test files
  • The Python test fails to match the required naming convention.

Automation Actions:

  • Request changes and post a comment that explains the Python test name requirements.

Review Python Test Name

# -*- mode: yaml -*-
manifest:
  version: 1.0

newTests: {{ source.diff.files | filter(attr='new_file', regex=r/^tests/) }}
newTestsCount: {{ source.diff.files | filter(attr='new_file', regex=r/^tests/) | length }}

automations:
  review_python_test_name:
    if:
      - {{ newTestsCount > 0}}     
      - {{ source.diff.files | filter(attr='new_file', regex=r/^tests/) | match(attr='new_file', regex=r/test_.*\.py$/) | nope }}   
    run: 
      - action: request-changes@v1
        args:
          comment: |
              The test file name does not follow the Python test name conventions. A test file name needs to have the prefix test_ before class name. For example, if you are testing a class file called Data.py then the test file name has to be test_data.py.

Additional Resources

gitStream is a workflow automation tool that enables you to use YAML configuration files to optimize your code review process. Add context to PRs, find code experts for reviews, and automate the merge process to maximize developer productivity.

Learn More about how gitStream Works.

More Automations can be found on the Automation Library and Integrations pages.