|
8 | 8 | from django.http import HttpRequest |
9 | 9 | from django.utils import timezone |
10 | 10 | from jwt import PyJWTError |
11 | | -from rest_framework import authentication, exceptions |
| 11 | +from rest_framework import authentication, exceptions, serializers |
12 | 12 | from rest_framework.exceptions import NotAuthenticated |
13 | 13 | from rest_framework.views import exception_handler |
14 | 14 | from shared.django_apps.codecov_auth.models import Owner |
|
24 | 24 | from core.models import Commit, Repository |
25 | 25 | from upload.helpers import get_global_tokens, get_repo_with_github_actions_oidc_token |
26 | 26 | from upload.views.helpers import ( |
27 | | - get_repository_and_owner_from_slug_and_commit, |
28 | 27 | get_repository_and_owner_from_string, |
29 | 28 | get_repository_from_string, |
30 | 29 | ) |
@@ -406,84 +405,54 @@ def authenticate( |
406 | 405 | ) |
407 | 406 |
|
408 | 407 |
|
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 |
443 | 412 |
|
444 | 413 |
|
445 | | -class BundleAnalysisUploadTokenRequiredAuthenticationCheck( |
| 414 | +class UploadTokenRequiredGetFromBodyAuthenticationCheck( |
446 | 415 | UploadTokenRequiredAuthenticationCheck |
447 | 416 | ): |
448 | 417 | """ |
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, |
450 | 419 | then use the same authenticate() as parent class. |
451 | 420 | """ |
452 | 421 |
|
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( |
454 | 433 | self, request: HttpRequest |
455 | 434 | ) -> tuple[Repository | None, Owner | None]: |
456 | | - from upload.views.bundle_analysis import UploadSerializer # circular imports |
457 | | - |
458 | 435 | try: |
459 | 436 | body = json.loads(str(request.body, "utf8")) |
460 | | - except json.JSONDecodeError: |
461 | | - return None, None # continue to next auth class |
462 | 437 |
|
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) |
466 | 439 |
|
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 | + ) |
470 | 447 |
|
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 |
481 | 452 |
|
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 |
485 | 454 |
|
486 | 455 | def get_repository_and_owner( |
487 | 456 | self, request: HttpRequest |
488 | 457 | ) -> 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