-
Notifications
You must be signed in to change notification settings - Fork 28
/
base.py
83 lines (72 loc) · 2.95 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import logging
from django.conf import settings
from rest_framework.exceptions import ValidationError
from codecov_auth.models import Service
from core.models import Commit, Repository
from reports.models import CommitReport
from upload.views.helpers import get_repository_from_string
log = logging.getLogger(__name__)
class ShelterMixin:
def is_shelter_request(self) -> bool:
"""
Returns true when the incoming request originated from a Shelter.
Shelter adds an `X-Shelter-Token` header which contains a shared secret.
Use of that shared secret allows certain priviledged functionality that normal
uploads cannot access.
"""
shelter_token = self.request.META.get("HTTP_X_SHELTER_TOKEN")
return shelter_token and shelter_token == settings.SHELTER_SHARED_SECRET
class GetterMixin(ShelterMixin):
def get_repo(self) -> Repository:
service = self.kwargs.get("service")
repo_slug = self.kwargs.get("repo")
try:
service_enum = Service(service)
except ValueError:
log.warning(
f"Service not found: {service}", extra=dict(repo_slug=repo_slug)
)
raise ValidationError(f"Service not found: {service}")
repository = get_repository_from_string(service_enum, repo_slug)
if not repository:
log.warning(
"Repository not found",
extra=dict(repo_slug=repo_slug),
)
raise ValidationError(f"Repository not found")
return repository
def get_commit(self, repo: Repository) -> Commit:
commit_sha = self.kwargs.get("commit_sha")
try:
commit = Commit.objects.get(
commitid=commit_sha, repository__repoid=repo.repoid
)
return commit
except Commit.DoesNotExist:
log.warning(
"Commit SHA not found",
extra=dict(repo=repo.name, commit_sha=commit_sha),
)
raise ValidationError("Commit SHA not found")
def get_report(
self, commit: Commit, report_type=CommitReport.ReportType.COVERAGE
) -> CommitReport:
report_code = self.kwargs.get("report_code")
if report_code == "default":
report_code = None
queryset = CommitReport.objects.filter(code=report_code, commit=commit)
if report_type == CommitReport.ReportType.COVERAGE:
queryset = queryset.coverage_reports()
else:
queryset = queryset.filter(report_type=report_type)
report = queryset.first()
if report is None:
log.warning(
"Report not found",
extra=dict(commit_sha=commit.commitid, report_code=report_code),
)
raise ValidationError(f"Report not found")
if report.report_type is None:
report.report_type = CommitReport.ReportType.COVERAGE
report.save()
return report