From 8d0aaae7a119c69c6d3988916be5a2e54a28895b Mon Sep 17 00:00:00 2001 From: scott-codecov Date: Thu, 21 Dec 2023 13:33:46 -0500 Subject: [PATCH] feat: Add service to load bundle analysis report (#310) --- requirements.txt | 7 +++- services/bundle_analysis.py | 27 ++++++++++++ services/tests/samples/bundle_report.sqlite | Bin 0 -> 69632 bytes services/tests/test_bundle_analysis.py | 44 ++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 services/bundle_analysis.py create mode 100644 services/tests/samples/bundle_report.sqlite create mode 100644 services/tests/test_bundle_analysis.py diff --git a/requirements.txt b/requirements.txt index df28885a0f..301bd7ef15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 @@ -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 @@ -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 diff --git a/services/bundle_analysis.py b/services/bundle_analysis.py new file mode 100644 index 0000000000..19f8fc6030 --- /dev/null +++ b/services/bundle_analysis.py @@ -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) diff --git a/services/tests/samples/bundle_report.sqlite b/services/tests/samples/bundle_report.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..91a66f741e56d6217f37c31fa6f342f1baf6d600 GIT binary patch literal 69632 zcmeI5dyE@L9mjXQyI!wf<9j5I&$+uLq1?HN?Rzwrgs5CXh-hfis!dBMQLr3)?{4qx zUDs>p(Ne)R`~d=_3J}kKK>Sf8B>sU)RE0kxgai}`A%uifB!nteP)a3|0u>aAnVntR zdw0&2v{Jdtcjde9?mT|Kna|AZdcBW1^S~*yW#|ibvtDlLvqDM`Md7Hf3xbfOe*^T- zU*dE{_BW`CJ?s5elR}|1nn*k=q|^z4e2Zj~`NXxvSmM+1J+bFvi_ufD_i93PAo6i_ zA~N642*N=C1V8`;K;RuB@Z=7nrgJ&*s?jQ6s2P=|wzcAv>UOnVGo0X7>iB~vjy-fj zf9TlzPo2<%ar$Ibr;8g{GON02wTwlhsatkSx7xMZem9z%?H}K0n)Pz?ioRl8(I4v#z1(W6Urm?k2R0Ic@&uy1R&~Srx0>Hj>k7fk z-BS^L(l@og(^WQVAU{>0O?k=swW(i-Ko;ICUW*=1gwv-d`n4#x?l?Zg zjCggrXOM8?u>d{a;iipAo6%@mwdi2EVB0mLY^~`Pre(Iw za;;A+x35pG_mhj}_Qk^K`Ak1hyo!F8k3Xn|)5VOq;*JZC8tbX0o`9&w=;VDLIPnpE zT_WAKyg+@DWz888HGL!_u1Y;NN{-<;rfpGRy{5H|Os`Ga>!2H#TfL(4Rz2M_LU`#S zfmY>V-e9=d5bYZ8GZy-dVT+xm)aXF2mii^xaF}nfKkSwvFciFC#~TbwikcoB6<7E2 z9TeU9SIaHRo@+QzeLvI#z;@1Vm+cwYyc`$Hwf34_R5Vu%httPL`}HQ@c4^vQQ|W;) z!VZSZvYNh|7EyJ3fnWa8g|=0#(eCAS13z2|bOb+iZEDsN{5XBGV=A|5N-mG9cep(K zB)-Z49)9SX=n$s0U~e?OJD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)$?67LhJd++gyr%l^(T{ z)Z6*D*;{AyuB%<@bb|WJZsM>1&Y`&MT+TP!R?Dm#bR;b<#;LiRbo)*AHO1~o?KQk^ z#7bYeCq^BS{^M&iM}7kcPo0ZWO9yVE7A3&+9clqR0Ym`J%sX zyT?6*I;f&81iM&AMO68Qu99r-o+ z1^Eg25qXAuk33Ch2KXBJ0{JZY6nTPNq`wg?kuv!xIYUm76Xbp5Aeo}sv48*wfB*=9 z00@8p2!H?xfB*=9!1f@ZjMKa3rEz79$(SZqpsuE@rRum=*o%xT7>^=Yx;tv3X=u-gR{D0#l#?Jq5kiU=@$qV#OfamGi z|8wN~ylDDSbB)EgMx$IcjcX*rRaLo$!(0`Gt1P=3@*V&P Lg+j8dY1)4QLfsqE literal 0 HcmV?d00001 diff --git a/services/tests/test_bundle_analysis.py b/services/tests/test_bundle_analysis.py new file mode 100644 index 0000000000..faccc41575 --- /dev/null +++ b/services/tests/test_bundle_analysis.py @@ -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)