|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from dataclasses import dataclass |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import sentry_sdk |
| 7 | +import test_results_parser |
| 8 | +from shared.config import get_config |
| 9 | +from shared.django_apps.core.models import Commit, Repository |
| 10 | +from shared.django_apps.reports.models import ReportSession, UploadError |
| 11 | + |
| 12 | +from services.archive import ArchiveService |
| 13 | +from services.test_analytics.ta_timeseries import get_flaky_tests_set, insert_testrun |
| 14 | +from services.yaml import UserYaml, read_yaml_field |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class TAProcInfo: |
| 19 | + repository: Repository |
| 20 | + branch: str | None |
| 21 | + user_yaml: UserYaml |
| 22 | + |
| 23 | + |
| 24 | +def handle_file_not_found(upload: ReportSession): |
| 25 | + upload.state = "processed" |
| 26 | + upload.save() |
| 27 | + UploadError.objects.create( |
| 28 | + report_session=upload, |
| 29 | + error_code="file_not_in_storage", |
| 30 | + error_params={}, |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +def handle_parsing_error(upload: ReportSession, exc: Exception): |
| 35 | + sentry_sdk.capture_exception(exc, tags={"upload_state": upload.state}) |
| 36 | + upload.state = "processed" |
| 37 | + upload.save() |
| 38 | + UploadError.objects.create( |
| 39 | + report_session=upload, |
| 40 | + error_code="unsupported_file_format", |
| 41 | + error_params={"error_message": str(exc)}, |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def get_ta_processing_info( |
| 46 | + repoid: int, |
| 47 | + commitid: str, |
| 48 | + commit_yaml: dict[str, Any], |
| 49 | +) -> TAProcInfo: |
| 50 | + repository = Repository.objects.get(repoid=repoid) |
| 51 | + |
| 52 | + commit = Commit.objects.get(repository=repository, commitid=commitid) |
| 53 | + branch = commit.branch |
| 54 | + if branch is None: |
| 55 | + raise ValueError("Branch is None") |
| 56 | + |
| 57 | + user_yaml: UserYaml = UserYaml(commit_yaml) |
| 58 | + return TAProcInfo( |
| 59 | + repository, |
| 60 | + branch, |
| 61 | + user_yaml, |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def should_delete_archive_settings(user_yaml: UserYaml) -> bool: |
| 66 | + if get_config("services", "minio", "expire_raw_after_n_days"): |
| 67 | + return True |
| 68 | + return not read_yaml_field(user_yaml, ("codecov", "archive", "uploads"), _else=True) |
| 69 | + |
| 70 | + |
| 71 | +def rewrite_or_delete_upload( |
| 72 | + archive_service: ArchiveService, |
| 73 | + user_yaml: UserYaml, |
| 74 | + upload: ReportSession, |
| 75 | + readable_file: bytes, |
| 76 | +): |
| 77 | + if should_delete_archive_settings(user_yaml): |
| 78 | + archive_url = upload.storage_path |
| 79 | + if archive_url and not archive_url.startswith("http"): |
| 80 | + archive_service.delete_file(archive_url) |
| 81 | + else: |
| 82 | + archive_service.write_file(upload.storage_path, bytes(readable_file)) |
| 83 | + |
| 84 | + |
| 85 | +def insert_testruns_timeseries( |
| 86 | + repoid: int, |
| 87 | + commitid: str, |
| 88 | + branch: str | None, |
| 89 | + upload: ReportSession, |
| 90 | + parsing_infos: list[test_results_parser.ParsingInfo], |
| 91 | +): |
| 92 | + flaky_test_set = get_flaky_tests_set(repoid) |
| 93 | + |
| 94 | + for parsing_info in parsing_infos: |
| 95 | + insert_testrun( |
| 96 | + timestamp=upload.created_at, |
| 97 | + repo_id=repoid, |
| 98 | + commit_sha=commitid, |
| 99 | + branch=branch, |
| 100 | + upload_id=upload.id, |
| 101 | + flags=upload.flag_names, |
| 102 | + parsing_info=parsing_info, |
| 103 | + flaky_test_ids=flaky_test_set, |
| 104 | + ) |
0 commit comments