10
10
from sentry .api .bases .project import ProjectEndpoint , ProjectReleasePermission
11
11
from sentry .debug_files .upload import find_missing_chunks
12
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
13
14
from sentry .tasks .assemble import (
14
15
AssembleTask ,
15
16
ChunkFileState ,
16
- assemble_preprod_artifact ,
17
17
get_assemble_status ,
18
18
set_assemble_status ,
19
19
)
20
20
21
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
+
22
67
@region_silo_endpoint
23
68
class ProjectPreprodArtifactAssembleEndpoint (ProjectEndpoint ):
24
69
owner = ApiOwner .EMERGE_TOOLS
@@ -32,42 +77,9 @@ def post(self, request: Request, project) -> Response:
32
77
Assembles a preprod artifact (mobile build, etc.) and stores it in the database.
33
78
"""
34
79
with sentry_sdk .start_span (op = "preprod_artifact.assemble" ):
35
- schema = {
36
- "type" : "object" ,
37
- "properties" : {
38
- "checksum" : {"type" : "string" , "pattern" : "^[0-9a-f]{40}$" },
39
- "chunks" : {
40
- "type" : "array" ,
41
- "items" : {"type" : "string" , "pattern" : "^[0-9a-f]{40}$" },
42
- },
43
- # Optional metadata
44
- "git_sha" : {"type" : "string" , "pattern" : "^[0-9a-f]{40}$" },
45
- "build_configuration" : {"type" : "string" },
46
- },
47
- "required" : ["checksum" , "chunks" ],
48
- "additionalProperties" : False ,
49
- }
50
-
51
- error_messages = {
52
- "checksum" : "The checksum field is required and must be a 40-character hexadecimal string." ,
53
- "chunks" : "The chunks field is required and must be provided as an array of 40-character hexadecimal strings." ,
54
- "git_sha" : "The git_sha field must be a string." ,
55
- "build_configuration" : "The build_configuration field must be a string." ,
56
- }
57
-
58
- try :
59
- data = orjson .loads (request .body )
60
- jsonschema .validate (data , schema )
61
- except jsonschema .ValidationError as e :
62
- error_message = e .message
63
- # Get the field from the path if available
64
- if e .path :
65
- if field := e .path [0 ]:
66
- error_message = error_messages .get (str (field ), error_message )
67
-
80
+ data , error_message = validate_preprod_artifact_schema (request .body )
81
+ if error_message :
68
82
return Response ({"error" : error_message }, status = 400 )
69
- except (orjson .JSONDecodeError , TypeError ):
70
- return Response ({"error" : "Invalid json body" }, status = 400 )
71
83
72
84
checksum = data .get ("checksum" )
73
85
chunks = data .get ("chunks" , [])
0 commit comments