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

Bundle Analysis: Display comparison for file paths on PR comments #952

Merged
merged 10 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
https://github.com/codecov/test-results-parser/archive/c840502d1b4dd7d05b2efc2c1328affaf2acd27c.tar.gz#egg=test-results-parser
https://github.com/codecov/shared/archive/8c5de3fadd3c987304043c6cfd64864f372d674f.tar.gz#egg=shared
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be updated once shared PR is merged.

https://github.com/codecov/shared/archive/0af6ba6385c84446beaf7f9917bf69ff1564c262.tar.gz#egg=shared
https://github.com/codecov/timestring/archive/d37ceacc5954dff3b5bd2f887936a98a668dda42.tar.gz#egg=timestring
asgiref>=3.7.2
analytics-python==1.3.0b1
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ sentry-sdk==2.13.0
# shared
setuptools==75.6.0
# via nodeenv
shared @ https://github.com/codecov/shared/archive/8c5de3fadd3c987304043c6cfd64864f372d674f.tar.gz#egg=shared
shared @ https://github.com/codecov/shared/archive/0af6ba6385c84446beaf7f9917bf69ff1564c262.tar.gz#egg=shared
# via -r requirements.in
six==1.16.0
# via
Expand Down
60 changes: 58 additions & 2 deletions services/bundle_analysis/notify/messages/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sentry_sdk
from asgiref.sync import async_to_sync
from django.template import loader
from shared.bundle_analysis import BundleAnalysisComparison, BundleChange
from shared.bundle_analysis import BundleAnalysisComparison, BundleChange, RouteChange
from shared.torngit.exceptions import TorngitClientError
from shared.validation.types import BundleThreshold

Expand Down Expand Up @@ -34,6 +34,14 @@ class BundleRow(TypedDict):
is_change_outside_threshold: bool


class BundleRouteRow(TypedDict):
route_name: str
change_size_readable: str
percentage_change_readable: str
change_icon: str
route_size: str


class BundleCommentTemplateContext(TypedDict):
pull_url: str
total_size_delta: int
Expand All @@ -42,6 +50,7 @@ class BundleCommentTemplateContext(TypedDict):
status_level: Literal["INFO"] | Literal["WARNING"] | Literal["ERROR"]
warning_threshold_readable: str
bundle_rows: list[BundleRow]
bundle_route_data: dict[str, list[BundleRouteRow]]
has_cached_bundles: bool


Expand Down Expand Up @@ -70,13 +79,17 @@ def build_default_message(
bundle_rows = self._create_bundle_rows(
context.bundle_analysis_comparison, warning_threshold
)
bundle_route_data = self._create_bundle_route_data(
context.bundle_analysis_comparison
)
if warning_threshold.type == "absolute":
warning_threshold_readable = bytes_readable(warning_threshold.threshold)
else:
warning_threshold_readable = str(round(warning_threshold.threshold)) + "%"
context = BundleCommentTemplateContext(
has_cached=any(row["is_cached"] for row in bundle_rows),
bundle_rows=bundle_rows,
bundle_route_data=bundle_route_data,
pull_url=get_bundle_analysis_pull_url(pull=context.pull.database_pull),
total_size_delta=total_size_delta,
status_level=context.commit_status_level.name,
Expand Down Expand Up @@ -159,7 +172,7 @@ def _create_bundle_rows(

change_size = bundle_change.size_delta
if change_size == 0:
# Don't include bundles that were not changes in the table
# Don't include bundles that were not changed in the table
continue
icon = ""
if change_size > 0:
Expand All @@ -184,3 +197,46 @@ def _create_bundle_rows(
)

return bundle_rows

def _create_bundle_route_data(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we use this computation/stats elsewhere in the code, like in GQL for example? In case it makes sense for this to live in Shared

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, these computations are really for PR comments as it figures out the icons to use and how to display the size (eg bytes, MB, etc). On the app side these are taken care of by gazebo

self,
comparison: BundleAnalysisComparison,
) -> dict[str, list[BundleRouteRow]]:
"""
Translate BundleRouteComparison dict data into a template compatible dict data
"""
bundle_route_data = {}
changes_dict = comparison.bundle_routes_changes()

for bundle_name, route_changes in changes_dict.items():
rows = []
for route_change in route_changes:
change_size, icon = route_change.size_delta, ""
size = (
"(removed)"
if route_change.change_type == RouteChange.ChangeType.REMOVED
else bytes_readable(route_change.size_head)
)

if change_size == 0:
# Don't include bundles that were not changes in the table
continue
elif change_size > 0:
icon = ":arrow_up:"
elif change_size < 0:
icon = ":arrow_down:"

rows.append(
BundleRouteRow(
route_name=route_change.route_name,
change_size_readable=bytes_readable(change_size),
percentage_change_readable=f"{route_change.percentage_delta}%",
change_icon=icon,
route_size=size,
)
)

# Only include bundles that have routes
if rows:
bundle_route_data[bundle_name] = rows
return bundle_route_data
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Bundle size has no change :white_check_mark:
{% if bundle_rows %}{% include "bundle_analysis_notify/bundle_table.md" %}{% if has_cached %}

ℹ️ *Bundle size includes cached data from a previous commit
{%endif%}{% endif %}
{%endif%}{% endif %}
{% if bundle_route_data %}{% include "bundle_analysis_notify/bundle_route_table.md" %}{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% for bundle_name, routes in bundle_route_data.items %}
<details>
<summary>View changes by path for bundle: {{ bundle_name }}</summary>

| File path | Size | Change |
| --------- | ---- | ------ |{% for bundle_route_row in routes %}
| {{bundle_route_row.route_name}} | {{bundle_route_row.route_size}} | {{bundle_route_row.change_size_readable}} ({{bundle_route_row.percentage_change_readable}}) {{bundle_route_row.change_icon}}{{bundle_route_row.}} |{% endfor %}

</details>
{% endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def test_build_message_from_samples(self, dbsession, mocker, mock_storage):
| @codecov/bundler-plugin-core-cjs | 43.32kB | 611 bytes (1.43%) :arrow_up: |
| @codecov/example-next-app-server-cjs | (removed) | 342.32kB (-100.0%) :arrow_down: |

</details>""").format(
</details>
""").format(
pullid=enriched_pull.database_pull.pullid,
owner=head_commit.repository.owner.username,
repo=head_commit.repository.name,
Expand Down
Loading
Loading