Skip to content

Commit

Permalink
feat: Add service to load bundle analysis report (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-codecov authored Dec 21, 2023
1 parent aabaeae commit 8d0aaae
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
7 changes: 5 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile requirements.in
# pip-compile
#
aiodataloader==0.2.0
# via -r requirements.in
Expand Down Expand Up @@ -140,6 +140,7 @@ freezegun==1.1.0
# via -r requirements.in
google-api-core[grpc]==2.11.1
# via
# google-api-core
# google-cloud-core
# google-cloud-pubsub
# google-cloud-storage
Expand Down Expand Up @@ -359,7 +360,9 @@ requests==2.31.0
# google-cloud-storage
# stripe
rfc3986[idna2008]==1.4.0
# via httpx
# via
# httpx
# rfc3986
rsa==4.7.2
# via google-auth
s3transfer==0.5.0
Expand Down
27 changes: 27 additions & 0 deletions services/bundle_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Optional

from shared.bundle_analysis import BundleAnalysisReport, BundleAnalysisReportLoader
from shared.storage import get_appropriate_storage_service

from core.models import Commit
from reports.models import CommitReport
from services.archive import ArchiveService


def load_report(
commit: Commit, report_code: Optional[str] = None
) -> Optional[BundleAnalysisReport]:
storage = get_appropriate_storage_service()

commit_report = commit.reports.filter(
report_type=CommitReport.ReportType.BUNDLE_ANALYSIS,
code=report_code,
).first()
if commit_report is None:
return None

loader = BundleAnalysisReportLoader(
storage_service=storage,
repo_key=ArchiveService.get_archive_hash(commit.repository),
)
return loader.load(commit_report.external_id)
Binary file added services/tests/samples/bundle_report.sqlite
Binary file not shown.
44 changes: 44 additions & 0 deletions services/tests/test_bundle_analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from unittest.mock import patch

import pytest
from shared.bundle_analysis import BundleAnalysisReport, StoragePaths
from shared.bundle_analysis.storage import get_bucket_name
from shared.storage.memory import MemoryStorageService

from core.tests.factories import CommitFactory, RepositoryFactory
from reports.models import CommitReport
from reports.tests.factories import CommitReportFactory
from services.archive import ArchiveService
from services.bundle_analysis import load_report


@pytest.mark.django_db
@patch("services.bundle_analysis.get_appropriate_storage_service")
def test_load_report(get_storage_service):
storage = MemoryStorageService({})
get_storage_service.return_value = storage

repo = RepositoryFactory()
commit = CommitFactory(repository=repo)

# no commit report record
assert load_report(commit) is None

commit_report = CommitReportFactory(
commit=commit, report_type=CommitReport.ReportType.BUNDLE_ANALYSIS
)

storage_path = StoragePaths.bundle_report.path(
repo_key=ArchiveService.get_archive_hash(repo),
report_key=commit_report.external_id,
)

# nothing in storage
assert load_report(commit) is None

with open("./services/tests/samples/bundle_report.sqlite", "rb") as f:
storage.write_file(get_bucket_name(), storage_path, f)

report = load_report(commit)
assert report is not None
assert isinstance(report, BundleAnalysisReport)

0 comments on commit 8d0aaae

Please sign in to comment.