|
| 1 | +from shared.django_apps.core.models import Commit |
| 2 | +from shared.django_apps.reports.models import CommitReport, ReportType |
| 3 | + |
| 4 | +from services.archive import ArchiveService |
| 5 | + |
| 6 | + |
| 7 | +def transplant_commit_report(repo_id: int, from_sha: str, to_sha: str): |
| 8 | + """ |
| 9 | + This copies a `Report` from one commit to another commit. |
| 10 | +
|
| 11 | + The `to_sha` commit has to exist already (being auto-created using a git provider sync). |
| 12 | +
|
| 13 | + It does so by creating a copy of the underlying `report_json` and `chunks` storage files, |
| 14 | + as well as some related DB models. |
| 15 | + """ |
| 16 | + |
| 17 | + from_commit = Commit.objects.select_related("repository").get( |
| 18 | + repository=repo_id, commitid=from_sha |
| 19 | + ) |
| 20 | + to_commit = Commit.objects.get(repository=repo_id, commitid=to_sha) |
| 21 | + |
| 22 | + archive_service = ArchiveService(from_commit.repository) |
| 23 | + |
| 24 | + chunks = archive_service.read_chunks(from_commit.commitid) |
| 25 | + report_json = from_commit.report |
| 26 | + totals = from_commit.totals |
| 27 | + |
| 28 | + archive_service.write_chunks(to_commit.commitid, chunks) |
| 29 | + |
| 30 | + to_commit.report = report_json |
| 31 | + to_commit.totals = totals |
| 32 | + to_commit.state = "complete" |
| 33 | + to_commit.save() |
| 34 | + |
| 35 | + if old_commit_report := from_commit.commitreport: |
| 36 | + commit_report = CommitReport( |
| 37 | + commit=to_commit, report_type=ReportType.COVERAGE.value |
| 38 | + ) |
| 39 | + commit_report.save() |
| 40 | + |
| 41 | + if totals := old_commit_report.reportleveltotals: |
| 42 | + # See <https://docs.djangoproject.com/en/5.1/topics/db/queries/#copying-model-instances> |
| 43 | + totals.pk = None |
| 44 | + totals.id = None |
| 45 | + totals._state.adding = True |
| 46 | + |
| 47 | + totals.report = commit_report |
| 48 | + totals.save() |
| 49 | + |
| 50 | + # TODO: |
| 51 | + # We might also have to create copies of all of `Upload` (aka `Reportsession`), |
| 52 | + # `UploadLevelTotals`, `UploadError` and `UploadFlagMembership` |
0 commit comments