Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.
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
49 changes: 49 additions & 0 deletions services/report/tests/unit/test_transplant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest
from shared.django_apps.core.tests.factories import (
CommitFactory,
CommitWithReportFactory,
RepositoryFactory,
)
from shared.django_apps.reports.models import ReportLevelTotals

from services.archive import ArchiveService
from services.report.transplant import transplant_commit_report


@pytest.mark.django_db()
def test_transplanting_commit(mock_storage):
repo = RepositoryFactory()
commit_from = CommitWithReportFactory(repository=repo)

archive_service = ArchiveService(repo)
with open("tasks/tests/samples/sample_chunks_1.txt", "rb") as f:
chunks = f.read()
archive_service.write_chunks(commit_from.commitid, chunks)

commit_to = CommitFactory(repository=repo)

transplant_commit_report(
repo_id=repo.repoid, from_sha=commit_from.commitid, to_sha=commit_to.commitid
)
commit_to.refresh_from_db()

from_totals = commit_from.commitreport.reportleveltotals
to_totals = commit_to.commitreport.reportleveltotals

def totals_tuple(totals: ReportLevelTotals):
return (
totals.branches,
totals.coverage,
totals.hits,
totals.lines,
totals.methods,
totals.misses,
totals.partials,
totals.files,
)

assert totals_tuple(from_totals) == totals_tuple(to_totals)

report = commit_to.full_report
file = report.get("tests/__init__.py")
assert file.get(1).coverage == 1
52 changes: 52 additions & 0 deletions services/report/transplant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from shared.django_apps.core.models import Commit
from shared.django_apps.reports.models import CommitReport, ReportType

from services.archive import ArchiveService


def transplant_commit_report(repo_id: int, from_sha: str, to_sha: str):
"""
This copies a `Report` from one commit to another commit.

The `to_sha` commit has to exist already (being auto-created using a git provider sync).

It does so by creating a copy of the underlying `report_json` and `chunks` storage files,
as well as some related DB models.
"""

from_commit = Commit.objects.select_related("repository").get(
repository=repo_id, commitid=from_sha
)
to_commit = Commit.objects.get(repository=repo_id, commitid=to_sha)

archive_service = ArchiveService(from_commit.repository)

chunks = archive_service.read_chunks(from_commit.commitid)
report_json = from_commit.report
totals = from_commit.totals

archive_service.write_chunks(to_commit.commitid, chunks)

to_commit.report = report_json
to_commit.totals = totals
to_commit.state = "complete"
to_commit.save()

if old_commit_report := from_commit.commitreport:
commit_report = CommitReport(
commit=to_commit, report_type=ReportType.COVERAGE.value
)
commit_report.save()

if totals := old_commit_report.reportleveltotals:
# See <https://docs.djangoproject.com/en/5.1/topics/db/queries/#copying-model-instances>
totals.pk = None
totals.id = None
totals._state.adding = True

totals.report = commit_report
totals.save()

# TODO:
# We might also have to create copies of all of `Upload` (aka `Reportsession`),
# `UploadLevelTotals`, `UploadError` and `UploadFlagMembership`
1 change: 1 addition & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
timeseries_backfill_dataset_task,
)
from tasks.timeseries_delete import timeseries_delete_task
from tasks.transplant_report import transplant_report_task
from tasks.trial_expiration import trial_expiration_task
from tasks.trial_expiration_cron import trial_expiration_cron_task
from tasks.update_branches import update_branches_task_name
Expand Down
4 changes: 2 additions & 2 deletions tasks/compute_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from app import celery_app
from database.enums import CompareCommitError, CompareCommitState
from database.models import CompareCommit, CompareComponent, CompareFlag
from database.models.reports import ReportLevelTotals, RepositoryFlag
Copy link
Contributor

Choose a reason for hiding this comment

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

why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, this must be something I forgot to commit in a different PR.
It removes a wrong type, as the totals: ReportLevelTotals down below is wrong.

from database.models.reports import RepositoryFlag
from helpers.comparison import minimal_totals
from helpers.github_installation import get_installation_name_for_owner_for_task
from rollouts import PARALLEL_COMPONENT_COMPARISON
Expand Down Expand Up @@ -216,7 +216,7 @@ def store_flag_comparison(
db_session,
comparison: CompareCommit,
repositoryflag: RepositoryFlag,
totals: ReportLevelTotals,
totals,
):
flag_comparison = CompareFlag(
commit_comparison=comparison,
Expand Down
12 changes: 12 additions & 0 deletions tasks/transplant_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from app import celery_app
from services.report.transplant import transplant_commit_report
from tasks.base import BaseCodecovTask


class TransplantReportTask(BaseCodecovTask, name="app.tasks.reports.transplant_report"):
def run_impl(self, db_session, repo_id: int, from_sha: str, to_sha: str):
transplant_commit_report(repo_id, from_sha, to_sha)

Check warning on line 8 in tasks/transplant_report.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/transplant_report.py#L8

Added line #L8 was not covered by tests


RegisteredTransplantReportTask = celery_app.register_task(TransplantReportTask())
transplant_report_task = celery_app.tasks[RegisteredTransplantReportTask.name]
Loading