|
| 1 | +import jsonschema |
| 2 | +import orjson |
| 3 | +import sentry_sdk |
| 4 | +from rest_framework.request import Request |
| 5 | +from rest_framework.response import Response |
| 6 | + |
| 7 | +from sentry.api.api_owners import ApiOwner |
| 8 | +from sentry.api.api_publish_status import ApiPublishStatus |
| 9 | +from sentry.api.base import region_silo_endpoint |
| 10 | +from sentry.api.bases.project import ProjectEndpoint, ProjectReleasePermission |
| 11 | +from sentry.debug_files.upload import find_missing_chunks |
| 12 | +from sentry.models.orgauthtoken import is_org_auth_token_auth, update_org_auth_token_last_used |
| 13 | +from sentry.preprod.tasks import assemble_preprod_artifact |
| 14 | +from sentry.tasks.assemble import ( |
| 15 | + AssembleTask, |
| 16 | + ChunkFileState, |
| 17 | + get_assemble_status, |
| 18 | + set_assemble_status, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def validate_preprod_artifact_schema(request_body: bytes) -> tuple[dict, str | None]: |
| 23 | + """ |
| 24 | + Validate the JSON schema for preprod artifact assembly requests. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + tuple: (parsed_data, error_message) where error_message is None if validation succeeds |
| 28 | + """ |
| 29 | + schema = { |
| 30 | + "type": "object", |
| 31 | + "properties": { |
| 32 | + "checksum": {"type": "string", "pattern": "^[0-9a-f]{40}$"}, |
| 33 | + "chunks": { |
| 34 | + "type": "array", |
| 35 | + "items": {"type": "string", "pattern": "^[0-9a-f]{40}$"}, |
| 36 | + }, |
| 37 | + # Optional metadata |
| 38 | + "git_sha": {"type": "string", "pattern": "^[0-9a-f]{40}$"}, |
| 39 | + "build_configuration": {"type": "string"}, |
| 40 | + }, |
| 41 | + "required": ["checksum", "chunks"], |
| 42 | + "additionalProperties": False, |
| 43 | + } |
| 44 | + |
| 45 | + error_messages = { |
| 46 | + "checksum": "The checksum field is required and must be a 40-character hexadecimal string.", |
| 47 | + "chunks": "The chunks field is required and must be provided as an array of 40-character hexadecimal strings.", |
| 48 | + "git_sha": "The git_sha field must be a 40-character hexadecimal string.", |
| 49 | + "build_configuration": "The build_configuration field must be a string.", |
| 50 | + } |
| 51 | + |
| 52 | + try: |
| 53 | + data = orjson.loads(request_body) |
| 54 | + jsonschema.validate(data, schema) |
| 55 | + return data, None |
| 56 | + except jsonschema.ValidationError as e: |
| 57 | + error_message = e.message |
| 58 | + # Get the field from the path if available |
| 59 | + if e.path: |
| 60 | + if field := e.path[0]: |
| 61 | + error_message = error_messages.get(str(field), error_message) |
| 62 | + return {}, error_message |
| 63 | + except (orjson.JSONDecodeError, TypeError): |
| 64 | + return {}, "Invalid json body" |
| 65 | + |
| 66 | + |
| 67 | +@region_silo_endpoint |
| 68 | +class ProjectPreprodArtifactAssembleEndpoint(ProjectEndpoint): |
| 69 | + owner = ApiOwner.EMERGE_TOOLS |
| 70 | + publish_status = { |
| 71 | + "POST": ApiPublishStatus.EXPERIMENTAL, |
| 72 | + } |
| 73 | + permission_classes = (ProjectReleasePermission,) |
| 74 | + |
| 75 | + def post(self, request: Request, project) -> Response: |
| 76 | + """ |
| 77 | + Assembles a preprod artifact (mobile build, etc.) and stores it in the database. |
| 78 | + """ |
| 79 | + with sentry_sdk.start_span(op="preprod_artifact.assemble"): |
| 80 | + data, error_message = validate_preprod_artifact_schema(request.body) |
| 81 | + if error_message: |
| 82 | + return Response({"error": error_message}, status=400) |
| 83 | + |
| 84 | + checksum = data.get("checksum") |
| 85 | + chunks = data.get("chunks", []) |
| 86 | + |
| 87 | + # Check if all requested chunks have been uploaded |
| 88 | + missing_chunks = find_missing_chunks(project.organization_id, set(chunks)) |
| 89 | + if missing_chunks: |
| 90 | + return Response( |
| 91 | + { |
| 92 | + "state": ChunkFileState.NOT_FOUND, |
| 93 | + "missingChunks": missing_chunks, |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + # Check current assembly status |
| 98 | + state, detail = get_assemble_status(AssembleTask.PREPROD_ARTIFACT, project.id, checksum) |
| 99 | + if state is not None: |
| 100 | + return Response({"state": state, "detail": detail, "missingChunks": []}) |
| 101 | + |
| 102 | + # There is neither a known file nor a cached state, so we will |
| 103 | + # have to create a new file. Assure that there are checksums. |
| 104 | + # If not, we assume this is a poll and report NOT_FOUND |
| 105 | + if not chunks: |
| 106 | + return Response({"state": ChunkFileState.NOT_FOUND, "missingChunks": []}) |
| 107 | + |
| 108 | + set_assemble_status( |
| 109 | + AssembleTask.PREPROD_ARTIFACT, project.id, checksum, ChunkFileState.CREATED |
| 110 | + ) |
| 111 | + |
| 112 | + assemble_preprod_artifact.apply_async( |
| 113 | + kwargs={ |
| 114 | + "org_id": project.organization_id, |
| 115 | + "project_id": project.id, |
| 116 | + "checksum": checksum, |
| 117 | + "chunks": chunks, |
| 118 | + "git_sha": data.get("git_sha"), |
| 119 | + "build_configuration": data.get("build_configuration"), |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + if is_org_auth_token_auth(request.auth): |
| 124 | + update_org_auth_token_last_used(request.auth, [project.id]) |
| 125 | + |
| 126 | + return Response({"state": ChunkFileState.CREATED, "missingChunks": []}) |
0 commit comments