Skip to content

Calculate the Percentage of New Code

Post a comment that indicates what percentage of the PR contains new code.

Percent new code

Configuration Description

Conditions (all must be true):

  • Any PR

Automation Actions:

  • Use the changes custom expression to post a comment that indicates what percentage of the PR is new code.

Calculate the Percentage of New Code

# -*- mode: yaml -*-

manifest:
  version: 1.0

automations:
  percent_new_code:
    if:
      - true
    run: 
      - action: add-comment@v1
        args:
          comment: |
            This PR is {{ changes.ratio }}% new code.

changes:
  # Sum all the lines added/edited in the PR
  additions: {{ branch.diff.files_metadata | map(attr='additions') | sum }}
  # Sum all the line removed in the PR
  deletions: {{ branch.diff.files_metadata | map(attr='deletions') | sum }}
  # Calculate the ratio of new code
  ratio: {{ (changes.additions / (changes.additions + changes.deletions)) * 100 | round(2) }}

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.

Related Automations:

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