Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit f2f587d

Browse files
committed
redo the whole thing, condense into one class that both views share
1 parent b70b7c9 commit f2f587d

File tree

6 files changed

+68
-411
lines changed

6 files changed

+68
-411
lines changed

codecov_auth/authentication/repo_auth.py

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.http import HttpRequest
99
from django.utils import timezone
1010
from jwt import PyJWTError
11-
from rest_framework import authentication, exceptions
11+
from rest_framework import authentication, exceptions, serializers
1212
from rest_framework.exceptions import NotAuthenticated
1313
from rest_framework.views import exception_handler
1414
from shared.django_apps.codecov_auth.models import Owner
@@ -24,7 +24,6 @@
2424
from core.models import Commit, Repository
2525
from upload.helpers import get_global_tokens, get_repo_with_github_actions_oidc_token
2626
from upload.views.helpers import (
27-
get_repository_and_owner_from_slug_and_commit,
2827
get_repository_and_owner_from_string,
2928
get_repository_from_string,
3029
)
@@ -406,84 +405,54 @@ def authenticate(
406405
)
407406

408407

409-
class TestResultsUploadTokenRequiredAuthenticationCheck(
410-
UploadTokenRequiredAuthenticationCheck
411-
):
412-
"""
413-
Repository and Owner are not in the path for this endpoint, have to get them another way,
414-
then use the same authenticate() as parent class.
415-
"""
416-
417-
def _get_info_from_request_data(
418-
self, request: HttpRequest
419-
) -> tuple[Repository | None, Owner | None]:
420-
from upload.views.test_results import UploadSerializer # circular imports
421-
422-
try:
423-
body = json.loads(str(request.body, "utf8"))
424-
except json.JSONDecodeError:
425-
return None, None # continue to next auth class
426-
427-
serializer = UploadSerializer(data=body)
428-
if not serializer.is_valid():
429-
return None, None # continue to next auth class
430-
431-
# these fields are required=True
432-
slug = serializer.validated_data["slug"]
433-
commitid = serializer.validated_data["commit"]
434-
435-
return get_repository_and_owner_from_slug_and_commit(
436-
slug=slug, commitid=commitid
437-
)
438-
439-
def get_repository_and_owner(
440-
self, request: HttpRequest
441-
) -> tuple[Repository | None, Owner | None]:
442-
return self._get_info_from_request_data(request)
408+
class UploadTokenRequiredGetFromBodySerializer(serializers.Serializer):
409+
slug = serializers.CharField(required=True)
410+
service = serializers.CharField(required=False) # git_service from TA
411+
git_service = serializers.CharField(required=False) # git_service from BA
443412

444413

445-
class BundleAnalysisUploadTokenRequiredAuthenticationCheck(
414+
class UploadTokenRequiredGetFromBodyAuthenticationCheck(
446415
UploadTokenRequiredAuthenticationCheck
447416
):
448417
"""
449-
Repository and Owner are not in the path for this endpoint, have to get them another way,
418+
Get Repository and Owner from request body instead of path,
450419
then use the same authenticate() as parent class.
451420
"""
452421

453-
def _get_info_from_request_data(
422+
def _get_git(self, validated_data):
423+
"""
424+
BA sends this in as git_service, TA sends this in as service.
425+
Use this function so this Check class can be used by both views.
426+
"""
427+
git_service = validated_data.get("git_service")
428+
if not git_service:
429+
git_service = validated_data.get("service")
430+
return git_service
431+
432+
def _get_info_from_request_body(
454433
self, request: HttpRequest
455434
) -> tuple[Repository | None, Owner | None]:
456-
from upload.views.bundle_analysis import UploadSerializer # circular imports
457-
458435
try:
459436
body = json.loads(str(request.body, "utf8"))
460-
except json.JSONDecodeError:
461-
return None, None # continue to next auth class
462437

463-
serializer = UploadSerializer(data=body)
464-
if not serializer.is_valid():
465-
return None, None # continue to next auth class
438+
serializer = UploadTokenRequiredGetFromBodySerializer(data=body)
466439

467-
# these fields are required=True
468-
slug = serializer.validated_data["slug"]
469-
commitid = serializer.validated_data["commit"]
440+
if serializer.is_valid():
441+
git_service = self._get_git(validated_data=serializer.validated_data)
442+
service_enum = Service(git_service)
443+
return get_repository_and_owner_from_string(
444+
service=service_enum,
445+
repo_identifier=serializer.validated_data["slug"],
446+
)
470447

471-
# this field is required=False but is much better for getting repository and owner
472-
service = serializer.validated_data.get("git_service")
473-
if service:
474-
try:
475-
service_enum = Service(service)
476-
except ValueError:
477-
return None, None
478-
return get_repository_and_owner_from_string(
479-
service=service_enum, repo_identifier=slug
480-
)
448+
except (json.JSONDecodeError, ValueError):
449+
# exceptions raised by json.loads() and Service()
450+
# catch rather than raise to continue to next auth class
451+
pass
481452

482-
return get_repository_and_owner_from_slug_and_commit(
483-
slug=slug, commitid=commitid
484-
)
453+
return None, None # continue to next auth class
485454

486455
def get_repository_and_owner(
487456
self, request: HttpRequest
488457
) -> tuple[Repository | None, Owner | None]:
489-
return self._get_info_from_request_data(request)
458+
return self._get_info_from_request_body(request)

0 commit comments

Comments
 (0)