|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import jsonschema |
| 4 | +from rest_framework.response import Response |
| 5 | + |
| 6 | +from sentry.api.bases.organization import OrganizationReleasesBaseEndpoint |
| 7 | +from sentry.api.exceptions import ResourceDoesNotExist |
| 8 | +from sentry.models import Release |
| 9 | +from sentry.tasks.assemble import get_assemble_status, set_assemble_status, \ |
| 10 | + AssembleTask, ChunkFileState |
| 11 | +from sentry.utils import json |
| 12 | + |
| 13 | + |
| 14 | +class OrganizationReleaseAssembleEndpoint(OrganizationReleasesBaseEndpoint): |
| 15 | + def post(self, request, organization, version): |
| 16 | + """ |
| 17 | + Handle an artifact bundle and merge it into the release |
| 18 | + ``````````````````````````````````````````````````````` |
| 19 | +
|
| 20 | + :auth: required |
| 21 | + """ |
| 22 | + |
| 23 | + try: |
| 24 | + release = Release.objects.get( |
| 25 | + organization_id=organization.id, |
| 26 | + version=version, |
| 27 | + ) |
| 28 | + except Release.DoesNotExist: |
| 29 | + raise ResourceDoesNotExist |
| 30 | + |
| 31 | + if not self.has_release_permission(request, organization, release): |
| 32 | + raise ResourceDoesNotExist |
| 33 | + |
| 34 | + schema = { |
| 35 | + "type": "object", |
| 36 | + "properties": { |
| 37 | + "checksum": { |
| 38 | + "type": "string", |
| 39 | + "pattern": "^[0-9a-f]{40}$", |
| 40 | + }, |
| 41 | + "chunks": { |
| 42 | + "type": "array", |
| 43 | + "items": { |
| 44 | + "type": "string", |
| 45 | + "pattern": "^[0-9a-f]{40}$", |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + "required": ["checksum", "chunks"], |
| 50 | + "additionalProperties": False, |
| 51 | + } |
| 52 | + |
| 53 | + try: |
| 54 | + data = json.loads(request.body) |
| 55 | + jsonschema.validate(data, schema) |
| 56 | + except jsonschema.ValidationError as e: |
| 57 | + return Response({'error': str(e).splitlines()[0]}, |
| 58 | + status=400) |
| 59 | + except BaseException as e: |
| 60 | + return Response({'error': 'Invalid json body'}, |
| 61 | + status=400) |
| 62 | + |
| 63 | + checksum = data.get('checksum', None) |
| 64 | + chunks = data.get('chunks', []) |
| 65 | + |
| 66 | + state, detail = get_assemble_status(AssembleTask.ARTIFACTS, organization.id, checksum) |
| 67 | + if state == ChunkFileState.OK: |
| 68 | + return Response({ |
| 69 | + 'state': state, |
| 70 | + 'detail': None, |
| 71 | + 'missingChunks': [], |
| 72 | + }, status=200) |
| 73 | + elif state is not None: |
| 74 | + return Response({ |
| 75 | + 'state': state, |
| 76 | + 'detail': detail, |
| 77 | + 'missingChunks': [], |
| 78 | + }) |
| 79 | + |
| 80 | + # There is neither a known file nor a cached state, so we will |
| 81 | + # have to create a new file. Assure that there are checksums. |
| 82 | + # If not, we assume this is a poll and report NOT_FOUND |
| 83 | + if not chunks: |
| 84 | + return Response({ |
| 85 | + 'state': ChunkFileState.NOT_FOUND, |
| 86 | + 'missingChunks': [], |
| 87 | + }, status=200) |
| 88 | + |
| 89 | + set_assemble_status(AssembleTask.ARTIFACTS, organization.id, checksum, |
| 90 | + ChunkFileState.CREATED) |
| 91 | + |
| 92 | + from sentry.tasks.assemble import assemble_artifacts |
| 93 | + assemble_artifacts.apply_async( |
| 94 | + kwargs={ |
| 95 | + 'org_id': organization.id, |
| 96 | + 'version': version, |
| 97 | + 'checksum': checksum, |
| 98 | + 'chunks': chunks, |
| 99 | + } |
| 100 | + ) |
| 101 | + |
| 102 | + return Response({ |
| 103 | + 'state': ChunkFileState.CREATED, |
| 104 | + 'missingChunks': [], |
| 105 | + }, status=200) |
0 commit comments