This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Add new transplant_report task and logic
#1190
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
|
|
||
| RegisteredTransplantReportTask = celery_app.register_task(TransplantReportTask()) | ||
| transplant_report_task = celery_app.tasks[RegisteredTransplantReportTask.name] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
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: ReportLevelTotalsdown below is wrong.