Ejemplo n.º 1
0
    def put(self, request, group, integration_id):
        if not self._has_issue_feature(group.organization, request.user):
            return Response({'detail': MISSING_FEATURE_MESSAGE}, status=400)

        external_issue_id = request.DATA.get('externalIssue')
        if not external_issue_id:
            return Response({'externalIssue': ['Issue ID is required']},
                            status=400)

        organization_id = group.project.organization_id
        try:
            integration = Integration.objects.get(
                id=integration_id,
                organizations=organization_id,
            )
        except Integration.DoesNotExist:
            return Response(status=404)

        if not (integration.has_feature(IntegrationFeatures.ISSUE_BASIC)
                or integration.has_feature(IntegrationFeatures.ISSUE_SYNC)):
            return Response(
                {
                    'detail':
                    'This feature is not supported for this integration.'
                },
                status=400)

        installation = integration.get_installation(organization_id)
        try:
            data = installation.get_issue(external_issue_id, data=request.DATA)
        except IntegrationFormError as exc:
            return Response(exc.field_errors, status=400)
        except IntegrationError as exc:
            return Response({'non_field_errors': [exc.message]}, status=400)

        defaults = {
            'title': data.get('title'),
            'description': data.get('description'),
            'metadata': data.get('metadata'),
        }

        external_issue_key = installation.make_external_key(data)
        external_issue, created = ExternalIssue.objects.get_or_create(
            organization_id=organization_id,
            integration_id=integration.id,
            key=external_issue_key,
            defaults=defaults,
        )

        if created:
            integration_issue_linked.send_robust(
                integration=integration,
                organization=group.project.organization,
                user=request.user,
                sender=self.__class__,
            )
        else:
            external_issue.update(**defaults)

        try:
            installation.after_link_issue(external_issue, data=request.DATA)
        except IntegrationFormError as exc:
            return Response(exc.field_errors, status=400)
        except IntegrationError as exc:
            return Response({'non_field_errors': [exc.message]}, status=400)

        try:
            with transaction.atomic():
                GroupLink.objects.create(
                    group_id=group.id,
                    project_id=group.project_id,
                    linked_type=GroupLink.LinkedType.issue,
                    linked_id=external_issue.id,
                    relationship=GroupLink.Relationship.references,
                )
        except IntegrityError:
            return Response(
                {'non_field_errors': ['That issue is already linked']},
                status=400)

        # TODO(jess): would be helpful to return serialized external issue
        # once we have description, title, etc
        url = data.get('url') or installation.get_issue_url(external_issue.key)
        context = {
            'id': external_issue.id,
            'key': external_issue.key,
            'url': url,
            'integrationId': external_issue.integration_id,
            'displayName': installation.get_issue_display_name(external_issue),
        }
        return Response(context, status=201)
Ejemplo n.º 2
0
    def put(self, request: Request, group, integration_id) -> Response:
        if not self._has_issue_feature(group.organization, request.user):
            return Response({"detail": MISSING_FEATURE_MESSAGE}, status=400)

        external_issue_id = request.data.get("externalIssue")
        if not external_issue_id:
            return Response({"externalIssue": ["Issue ID is required"]},
                            status=400)

        organization_id = group.project.organization_id
        try:
            integration = Integration.objects.get(
                id=integration_id, organizations=organization_id)
        except Integration.DoesNotExist:
            return Response(status=404)

        if not (integration.has_feature(IntegrationFeatures.ISSUE_BASIC)
                or integration.has_feature(IntegrationFeatures.ISSUE_SYNC)):
            return Response(
                {
                    "detail":
                    "This feature is not supported for this integration."
                },
                status=400)

        installation = integration.get_installation(organization_id)
        try:
            data = installation.get_issue(external_issue_id, data=request.data)
        except IntegrationFormError as exc:
            return Response(exc.field_errors, status=400)
        except IntegrationError as e:
            return Response({"non_field_errors": [str(e)]}, status=400)

        defaults = {
            "title": data.get("title"),
            "description": data.get("description"),
            "metadata": data.get("metadata"),
        }

        external_issue_key = installation.make_external_key(data)
        external_issue, created = ExternalIssue.objects.get_or_create(
            organization_id=organization_id,
            integration_id=integration.id,
            key=external_issue_key,
            defaults=defaults,
        )

        if created:
            integration_issue_linked.send_robust(
                integration=integration,
                organization=group.project.organization,
                user=request.user,
                sender=self.__class__,
            )
        else:
            external_issue.update(**defaults)

        installation.store_issue_last_defaults(group.project, request.user,
                                               request.data)
        try:
            installation.after_link_issue(external_issue, data=request.data)
        except IntegrationFormError as exc:
            return Response(exc.field_errors, status=400)
        except IntegrationError as e:
            return Response({"non_field_errors": [str(e)]}, status=400)

        try:
            with transaction.atomic():
                GroupLink.objects.create(
                    group_id=group.id,
                    project_id=group.project_id,
                    linked_type=GroupLink.LinkedType.issue,
                    linked_id=external_issue.id,
                    relationship=GroupLink.Relationship.references,
                )
        except IntegrityError:
            return Response(
                {"non_field_errors": ["That issue is already linked"]},
                status=400)

        self.create_issue_activity(request, group, installation,
                                   external_issue)

        # TODO(jess): would be helpful to return serialized external issue
        # once we have description, title, etc
        url = data.get("url") or installation.get_issue_url(external_issue.key)
        context = {
            "id": external_issue.id,
            "key": external_issue.key,
            "url": url,
            "integrationId": external_issue.integration_id,
            "displayName": installation.get_issue_display_name(external_issue),
        }
        return Response(context, status=201)
Ejemplo n.º 3
0
    def put(self, request, group, integration_id):
        if not self._has_issue_feature(group.organization, request.user):
            return Response(
                {'detail': MISSING_FEATURE_MESSAGE}, status=400)

        external_issue_id = request.DATA.get('externalIssue')
        if not external_issue_id:
            return Response({'externalIssue': ['Issue ID is required']}, status=400)

        organization_id = group.project.organization_id
        try:
            integration = Integration.objects.get(
                id=integration_id,
                organizations=organization_id,
            )
        except Integration.DoesNotExist:
            return Response(status=404)

        if not (integration.has_feature(IntegrationFeatures.ISSUE_BASIC) or integration.has_feature(
                IntegrationFeatures.ISSUE_SYNC)):
            return Response(
                {'detail': 'This feature is not supported for this integration.'}, status=400)

        installation = integration.get_installation(organization_id)
        try:
            data = installation.get_issue(external_issue_id, data=request.DATA)
        except IntegrationFormError as exc:
            return Response(exc.field_errors, status=400)
        except IntegrationError as exc:
            return Response({'non_field_errors': [exc.message]}, status=400)

        defaults = {
            'title': data.get('title'),
            'description': data.get('description'),
            'metadata': data.get('metadata'),
        }

        external_issue_key = installation.make_external_key(data)
        external_issue, created = ExternalIssue.objects.get_or_create(
            organization_id=organization_id,
            integration_id=integration.id,
            key=external_issue_key,
            defaults=defaults,
        )

        if created:
            integration_issue_linked.send_robust(
                integration=integration,
                organization=group.project.organization,
                user=request.user,
                sender=self.__class__,
            )
        else:
            external_issue.update(**defaults)

        installation.store_issue_last_defaults(group.project_id, request.DATA)
        try:
            installation.after_link_issue(external_issue, data=request.DATA)
        except IntegrationFormError as exc:
            return Response(exc.field_errors, status=400)
        except IntegrationError as exc:
            return Response({'non_field_errors': [exc.message]}, status=400)

        try:
            with transaction.atomic():
                GroupLink.objects.create(
                    group_id=group.id,
                    project_id=group.project_id,
                    linked_type=GroupLink.LinkedType.issue,
                    linked_id=external_issue.id,
                    relationship=GroupLink.Relationship.references,
                )
        except IntegrityError:
            return Response({'non_field_errors': ['That issue is already linked']}, status=400)

        self.create_issue_activity(request, group, installation, external_issue)

        # TODO(jess): would be helpful to return serialized external issue
        # once we have description, title, etc
        url = data.get('url') or installation.get_issue_url(external_issue.key)
        context = {
            'id': external_issue.id,
            'key': external_issue.key,
            'url': url,
            'integrationId': external_issue.integration_id,
            'displayName': installation.get_issue_display_name(external_issue),
        }
        return Response(context, status=201)