Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add workflow to comment coverage diff on PRs #14

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Comment coverage diff on PR

on:
pull_request:
paths:
- "mock-responses**"
- "coverage-tool**"
- ".github/workflows/coverage.yml"

jobs:
comment-diff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Find coverage on PR branch
run: python3 coverage-tool/coverage_tool.py --json-output > head.json
- name: Checkout mock-responses from base branch
run: |
rm -rf mock-responses
git checkout ${{ github.event.pull_request.base.sha }} -- mock-responses
- name: Find coverage on base branch
run: python3 coverage-tool/coverage_tool.py --json-output > base.json
- name: Find coverage diff
run: |
{
echo "diff<<EOF"
python3 coverage-tool/diff_coverage.py --no-color base.json head.json
echo EOF
} >> $GITHUB_ENV
- name: Find previous comment
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e
id: find_comment
with:
issue-number: ${{ github.event.number }}
body-includes: Coverage Diff
- name: Comment on PR
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043
with:
issue-number: ${{ github.event.number }}
# if no previous comment, a new comment will be created due to empty comment-id
comment-id: ${{ steps.find_comment.outputs.comment-id }}
edit-mode: replace
body: |
### Coverage Diff
```
${{ env.diff || 'Coverage is identical' }}
```
107 changes: 67 additions & 40 deletions coverage-tool/diff_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@
OLD_KEYWORD = "_old_num"
NEW_KEYWORD = "_new_num"

COLORS = {
"green": "\033[92m",
"blue": "\033[94m",
"yellow": "\033[93m",
"red": "\033[91m",
"end": "\033[0m",
}

EMOJIS = {
"green": "✅",
"blue": "🔵",
"yellow": "🟡",
"red": "❌",
}

COLOR_DESC = {
"green": "total coverage increase",
"blue": "files number increase, total coverage unaffected",
"yellow": "files number decrease, total coverage unaffected",
"red": "total coverage decrease",
}


class DiffCoverage:
def __init__(self, file1, file2, no_color=False, all_fields=False):
Expand All @@ -44,54 +66,59 @@ def find_diff(self, old, new):
if key not in {NUM_KEYWORD, FILES_KEYWORD}
}

def print_output(self, output, indent=0):
"""Print the diff."""

def has_changes(field):
return field[OLD_KEYWORD] != field[NEW_KEYWORD] or any(
has_changes(value)
for key, value in field.items()
if key not in {OLD_KEYWORD, NEW_KEYWORD}
)

colors = {
"green": "\033[92m",
"blue": "\033[94m",
"yellow": "\033[93m",
"red": "\033[91m",
"end": "\033[0m",
}

for key, value in output.items():
if key not in {OLD_KEYWORD, NEW_KEYWORD} and (
self.all_fields or has_changes(value)
):
def get_output(self, diff, indent=0):
"""Return the diff to output as a string."""
output = ""
for key, value in diff.items():
if key not in {OLD_KEYWORD, NEW_KEYWORD}:
percent = "%" if key == TOTAL_KEYWORD else ""
if value[OLD_KEYWORD] == value[NEW_KEYWORD]:
print("| " * indent + f"{key}: {value[OLD_KEYWORD]}{percent}")
inner_output = self.get_output(value, indent + 1)
if inner_output or self.all_fields:
output += (
"| " * indent
+ f"{key}: {value[OLD_KEYWORD]}{percent}\n"
+ inner_output
)
else:
if not self.no_color:
if value[OLD_KEYWORD] == 0:
color = "green"
elif value[NEW_KEYWORD] == 0:
color = "red"
elif value[OLD_KEYWORD] > value[NEW_KEYWORD]:
color = "yellow" if key != TOTAL_KEYWORD else "red"
else:
color = "blue" if key != TOTAL_KEYWORD else "green"

print(
if value[OLD_KEYWORD] == 0:
color = "green"
elif value[NEW_KEYWORD] == 0:
color = "red"
elif value[OLD_KEYWORD] > value[NEW_KEYWORD]:
color = "yellow" if key != TOTAL_KEYWORD else "red"
else:
color = "blue" if key != TOTAL_KEYWORD else "green"
rlazo marked this conversation as resolved.
Show resolved Hide resolved

output += (
"| " * indent
+ f"{key}: "
+ (colors[color] if not self.no_color else "")
+ (COLORS[color] if not self.no_color else "")
+ f"{value[OLD_KEYWORD]}{percent} -> {value[NEW_KEYWORD]}{percent}"
+ (colors["end"] if not self.no_color else "")
+ (COLORS["end"] if not self.no_color else f" {EMOJIS[color]}")
+ "\n"
+ self.get_output(value, indent + 1)
)
self.print_output(value, indent + 1)
return output

def get_legend(self):
"""Return a legend for the colors as a string."""
output = "Legend:\n"
for color, meaning in COLOR_DESC.items():
if not self.no_color:
output += (
f"{COLORS[color]}{color.ljust(6)}{COLORS['end']} : {meaning}\n"
)
else:
output += f"{EMOJIS[color]} : {meaning}\n"
return output

def main(self):
output = self.find_diff(self.file1, self.file2)
self.print_output(output)
diff = self.find_diff(self.file1, self.file2)
output = self.get_output(diff)
if output:
print(output)
print(self.get_legend(), end="")


def get_args():
Expand All @@ -113,7 +140,7 @@ def read_json(file):
"--no-color",
"-n",
action="store_true",
help="Disable color in output",
help="Disable color in output and add emojis",
)
parser.add_argument(
"--all-fields",
Expand Down