Skip to content

Commit

Permalink
bundle analysis: compare with stored compareSha if applicable (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySentry authored Aug 22, 2024
1 parent c84212a commit f460e8d
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 6 deletions.
9 changes: 7 additions & 2 deletions graphql_api/dataloader/bundle_analysis.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Union

from shared.bundle_analysis import (
BundleAnalysisReportLoader,
MissingBaseReportError,
Expand All @@ -14,7 +16,7 @@

def load_bundle_analysis_comparison(
base_commit: Commit, head_commit: Commit
) -> BundleAnalysisComparison:
) -> Union[BundleAnalysisComparison, MissingHeadReport, MissingBaseReport]:
head_report = CommitReport.objects.filter(
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS, commit=head_commit
).first()
Expand All @@ -37,14 +39,17 @@ def load_bundle_analysis_comparison(
loader=loader,
base_report_key=base_report.external_id,
head_report_key=head_report.external_id,
repository=head_commit.repository,
)
except MissingBaseReportError:
return MissingBaseReport()
except MissingHeadReportError:
return MissingHeadReport()


def load_bundle_analysis_report(commit: Commit) -> BundleAnalysisReport:
def load_bundle_analysis_report(
commit: Commit,
) -> Union[BundleAnalysisReport, MissingHeadReport, MissingBaseReport]:
report = CommitReport.objects.filter(
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS, commit=commit
).first()
Expand Down
98 changes: 98 additions & 0 deletions graphql_api/tests/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,104 @@ def test_bundle_analysis_compare(self, get_storage_service):
"bundleChange": {"size": {"uncompress": 36555}},
}

@patch("graphql_api.dataloader.bundle_analysis.get_appropriate_storage_service")
def test_bundle_analysis_compare_with_compare_sha(self, get_storage_service):
"""
This tests creates 3 commits C1 -> C2 -> C3
C1 uses Report1, C2 and C3 uses Report2
Normally when doing a compare of C3, it would select C2 as its parent
then it would show no change, as expected
However the difference is that in C3's Report2 it has the compareSha set to C1.commitid
Now when doing comparison of C3, it would now select C1 as the parent
therefore show correct comparison in numbers between Report1 and Report2
"""
storage = MemoryStorageService({})
get_storage_service.return_value = storage

commit_1 = CommitFactory(
repository=self.repo,
commitid="6ca727b0142bf5625bb82af2555d308862063222",
)
commit_2 = CommitFactory(
repository=self.repo, parent_commit_id=commit_1.commitid
)
commit_3 = CommitFactory(
repository=self.repo, parent_commit_id=commit_2.commitid
)

commit_report_1 = CommitReportFactory(
commit=commit_1,
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS,
)

commit_report_2 = CommitReportFactory(
commit=commit_2,
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS,
)

commit_report_3 = CommitReportFactory(
commit=commit_3,
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS,
)

with open("./services/tests/samples/base_bundle_report.sqlite", "rb") as f:
storage_path = StoragePaths.bundle_report.path(
repo_key=ArchiveService.get_archive_hash(self.repo),
report_key=commit_report_1.external_id,
)
storage.write_file(get_bucket_name(), storage_path, f)

with open("./services/tests/samples/head_bundle_report.sqlite", "rb") as f:
storage_path = StoragePaths.bundle_report.path(
repo_key=ArchiveService.get_archive_hash(self.repo),
report_key=commit_report_2.external_id,
)
storage.write_file(get_bucket_name(), storage_path, f)

with open(
"./services/tests/samples/head_bundle_report_with_compare_sha_6ca727b0142bf5625bb82af2555d308862063222.sqlite",
"rb",
) as f:
storage_path = StoragePaths.bundle_report.path(
repo_key=ArchiveService.get_archive_hash(self.repo),
report_key=commit_report_3.external_id,
)
storage.write_file(get_bucket_name(), storage_path, f)

query = (
query_commit
% """
bundleAnalysisCompareWithParent {
__typename
... on BundleAnalysisComparison {
bundleData {
size {
uncompress
}
}
bundleChange {
size {
uncompress
}
}
}
}
"""
)

variables = {
"org": self.org.username,
"repo": self.repo.name,
"commit": commit_report_3.commit.commitid,
}
data = self.gql_request(query, variables=variables)
commit = data["owner"]["repository"]["commit"]
assert commit["bundleAnalysisCompareWithParent"] == {
"__typename": "BundleAnalysisComparison",
"bundleData": {"size": {"uncompress": 201720}},
"bundleChange": {"size": {"uncompress": 36555}},
}

@patch("graphql_api.dataloader.bundle_analysis.get_appropriate_storage_service")
def test_bundle_analysis_sqlite_file_deleted(self, get_storage_service):
os.system("rm -rf /tmp/bundle_analysis_*")
Expand Down
8 changes: 6 additions & 2 deletions graphql_api/types/pull/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ def resolve_bundle_analysis_compare_with_base(
head_commit_sha = pull.head if pull.head else pull.compared_to

bundle_analysis_comparison = load_bundle_analysis_comparison(
Commit.objects.filter(commitid=pull.compared_to).first(),
Commit.objects.filter(commitid=head_commit_sha).first(),
Commit.objects.filter(
commitid=pull.compared_to, repository=pull.repository
).first(),
Commit.objects.filter(
commitid=head_commit_sha, repository=pull.repository
).first(),
)

# Store the created SQLite DB path in info.context
Expand Down
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ factory-boy
fakeredis
freezegun
https://github.com/codecov/opentelem-python/archive/refs/tags/v0.0.4a1.tar.gz#egg=codecovopentelem
https://github.com/codecov/shared/archive/aacbd7203ffec207adf94d97dadfcbcce55126b7.tar.gz#egg=shared
https://github.com/codecov/shared/archive/e944f586104a274032c7f547400f1cbcae3d4c7d.tar.gz#egg=shared
google-cloud-pubsub
gunicorn>=22.0.0
https://github.com/photocrowd/django-cursor-pagination/archive/f560902696b0c8509e4d95c10ba0d62700181d84.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ sentry-sdk[celery]==1.44.1
# shared
setproctitle==1.1.10
# via -r requirements.in
shared @ https://github.com/codecov/shared/archive/aacbd7203ffec207adf94d97dadfcbcce55126b7.tar.gz
shared @ https://github.com/codecov/shared/archive/e944f586104a274032c7f547400f1cbcae3d4c7d.tar.gz
# via -r requirements.in
simplejson==3.17.2
# via -r requirements.in
Expand Down
2 changes: 2 additions & 0 deletions services/bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,13 @@ def __init__(
loader: BundleAnalysisReportLoader,
base_report_key: str,
head_report_key: str,
repository: Repository,
):
self.comparison = SharedBundleAnalysisComparison(
loader,
base_report_key,
head_report_key,
repository,
)
self.head_report = self.comparison.head_report

Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions services/tests/test_bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def test_bundle_analysis_comparison(self, get_storage_service):
loader,
self.base_commit_report.external_id,
self.head_commit_report.external_id,
self.repo,
)

assert len(bac.bundles) == 5
Expand Down

0 comments on commit f460e8d

Please sign in to comment.