Skip to content

feat(app-platform): Issue Link UI #12345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/sentry/api/bases/sentryapps.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ def convert_args(self, request, organization_slug, *args, **kwargs):
organization = organizations.get(slug=organization_slug)
except Organization.DoesNotExist:
raise Http404

self.check_object_permissions(request, organization)

kwargs['organization'] = organization
Expand All @@ -199,6 +198,14 @@ class SentryAppInstallationPermission(SentryPermission):
scope_map = {
'GET': ('org:read', 'org:integrations', 'org:write', 'org:admin'),
'DELETE': ('org:integrations', 'org:write', 'org:admin'),
# NOTE(mn): The only POST endpoint right now is to create External
# Issues, which uses this baseclass since it's nested under an
# installation.
#
# The scopes below really only make sense for that endpoint. Any other
# nested endpoints will probably need different scopes - figure out how
# to deal with that when it happens.
'POST': ('org:integrations', 'event:write', 'event:admin'),
}

def has_object_permission(self, request, view, installation):
Expand Down
38 changes: 32 additions & 6 deletions src/sentry/api/endpoints/sentry_app_components.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from __future__ import absolute_import

from rest_framework.response import Response

from sentry.api.bases import OrganizationEndpoint, SentryAppBaseEndpoint
from sentry.api.paginator import OffsetPaginator
from sentry.api.serializers import serialize
from sentry.features.helpers import requires_feature
from sentry.models import SentryAppComponent, SentryApp
from sentry.mediators import sentry_app_components
from sentry.models import Project, SentryAppComponent


class SentryAppComponentsEndpoint(SentryAppBaseEndpoint):
Expand All @@ -21,13 +24,36 @@ def get(self, request, sentry_app):
class OrganizationSentryAppComponentsEndpoint(OrganizationEndpoint):
@requires_feature('organizations:sentry-apps')
def get(self, request, organization):
try:
project = Project.objects.get(
id=request.GET['projectId'],
organization_id=organization.id,
)
except Project.DoesNotExist:
return Response([], status=404)

components = []

for install in organization.sentry_app_installations.all():
_components = SentryAppComponent.objects.filter(
sentry_app_id=install.sentry_app_id,
)

if 'filter' in request.GET:
_components = _components.filter(type=request.GET['filter'])

for component in _components:
sentry_app_components.Preparer.run(
component=component,
install=install,
project=project,
)

components.extend(_components)

return self.paginate(
request=request,
queryset=SentryAppComponent.objects.filter(
sentry_app_id__in=SentryApp.objects.filter(
installations__in=organization.sentry_app_installations.all(),
)
),
queryset=components,
paginator_cls=OffsetPaginator,
on_results=lambda x: serialize(x, request.user),
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ def post(self, request, installation):
actor=request.user):
return Response(status=404)

group_id = request.DATA.get('groupId')
if not group_id:
return Response({'detail': 'groupId is required'}, status=400)
data = request.DATA.copy()

if not set(['groupId', 'action', 'uri']).issubset(data.keys()):
return Response(status=400)

group_id = data.get('groupId')
del data['groupId']

try:
group = Group.objects.get(
Expand All @@ -30,13 +34,19 @@ def post(self, request, installation):
except Group.DoesNotExist:
return Response(status=404)

action = data['action']
del data['action']

uri = data.get('uri')
del data['uri']

try:
external_issue = IssueLinkCreator.run(
install=installation,
group=group,
action=request.DATA.get('action'),
fields=request.DATA.get('fields'),
uri=request.DATA.get('uri'),
action=action,
fields=data,
uri=uri,
)
except Exception:
return Response({'error': 'Error communicating with Sentry App service'}, status=400)
Expand Down
17 changes: 14 additions & 3 deletions src/sentry/api/serializers/models/sentry_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import absolute_import

from sentry.app import env
from sentry.auth.superuser import is_active_superuser
from sentry.api.serializers import Serializer, register
from sentry.models import SentryApp

Expand All @@ -8,7 +10,8 @@
class SentryAppSerializer(Serializer):
def serialize(self, obj, attrs, user):
from sentry.mediators.service_hooks.creator import consolidate_events
return {

data = {
'name': obj.name,
'slug': obj.slug,
'scopes': obj.get_scopes(),
Expand All @@ -19,7 +22,15 @@ def serialize(self, obj, attrs, user):
'webhookUrl': obj.webhook_url,
'redirectUrl': obj.redirect_url,
'isAlertable': obj.is_alertable,
'clientId': obj.application.client_id,
'clientSecret': obj.application.client_secret,
'overview': obj.overview,
}

if is_active_superuser(env.request) or (
hasattr(user, 'get_orgs') and obj.owner in user.get_orgs()
):
data.update({
'clientId': obj.application.client_id,
'clientSecret': obj.application.client_secret,
})

return data
6 changes: 5 additions & 1 deletion src/sentry/api/serializers/models/sentry_app_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ def serialize(self, obj, attrs, user):
'uuid': six.binary_type(obj.uuid),
'type': obj.type,
'schema': obj.schema,
'sentryAppId': obj.sentry_app_id,
'sentryApp': {
'uuid': obj.sentry_app.uuid,
'slug': obj.sentry_app.slug,
'name': obj.sentry_app.name,
}
}
1 change: 1 addition & 0 deletions src/sentry/mediators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
GrantExchanger,
Refresher,
)
from .sentry_app_components import * # NOQA
2 changes: 2 additions & 0 deletions src/sentry/mediators/external_issues/issue_link_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ def _make_external_request(self):

def _format_response_data(self):
web_url = self.response['webUrl']

display_name = u'{}#{}'.format(
self.response['project'],
self.response['identifier'],
)

return [web_url, display_name]

def _create_external_issue(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ def call(self):
return self._make_request()

def _build_url(self):
domain = urlparse(self.sentry_app.webhook_url).netloc
url = u'https://{}{}'.format(domain, self.uri)
return url
urlparts = urlparse(self.sentry_app.webhook_url)
return u'{}://{}{}'.format(urlparts.scheme, urlparts.netloc, self.uri)

def _make_request(self):
req = safe_urlopen(
Expand Down
34 changes: 19 additions & 15 deletions src/sentry/mediators/external_requests/select_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
from uuid import uuid4

from six.moves.urllib.parse import urlparse, urlencode
from six.moves.urllib.parse import urlparse, urlencode, urlunparse
from sentry.http import safe_urlopen, safe_urlread
from sentry.coreapi import APIError
from sentry.mediators import Mediator, Param
Expand Down Expand Up @@ -33,22 +33,26 @@ def call(self):
return self._make_request()

def _build_url(self):
domain = urlparse(self.sentry_app.webhook_url).netloc
url = u'https://{}{}'.format(domain, self.uri)
params = {'installationId': self.install.uuid}
urlparts = list(urlparse(self.sentry_app.webhook_url))
urlparts[2] = self.uri

query = {'installationId': self.install.uuid}

if self.project:
params['projectSlug'] = self.project.slug
url += '?' + urlencode(params)
return url
query['projectSlug'] = self.project.slug

def _make_request(self):
req = safe_urlopen(
url=self._build_url(),
headers=self._build_headers(),
)
urlparts[4] = urlencode(query)
return urlunparse(urlparts)

def _make_request(self):
try:
body = safe_urlread(req)
body = safe_urlread(
safe_urlopen(
url=self._build_url(),
headers=self._build_headers(),
)
)

response = json.loads(body)
except Exception:
logger.info(
Expand Down Expand Up @@ -78,9 +82,9 @@ def _format_response(self, resp):
choices = []

for option in resp:
choices.append([option['label'], option['value']])
choices.append([option['value'], option['label']])
if option.get('default'):
response['default'] = [option['label'], option['value']]
response['defaultValue'] = option['value']

response['choices'] = choices
return response
Expand Down
3 changes: 3 additions & 0 deletions src/sentry/mediators/sentry_app_components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from __future__ import absolute_import

from .preparer import Preparer # NOQA
46 changes: 46 additions & 0 deletions src/sentry/mediators/sentry_app_components/preparer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import absolute_import

from sentry.mediators import Mediator, Param
from sentry.mediators.external_requests import SelectRequester


class Preparer(Mediator):
component = Param('sentry.models.SentryAppComponent')
install = Param('sentry.models.SentryAppInstallation')
project = Param('sentry.models.Project')

def call(self):
if self.component.type == 'issue-link':
return self._prepare_issue_link()

def _prepare_issue_link(self):
schema = self.component.schema.copy()

link = schema.get('link', {})
create = schema.get('create', {})

for field in link.get('required_fields', []):
self._prepare_field(field)

for field in link.get('optional_fields', []):
self._prepare_field(field)

for field in create.get('required_fields', []):
self._prepare_field(field)

for field in create.get('optional_fields', []):
self._prepare_field(field)

def _prepare_field(self, field):
if 'options' in field:
field.update({'choices': field['options']})

if 'uri' in field:
field.update(self._request(field['uri']))

def _request(self, uri):
return SelectRequester.run(
install=self.install,
project=self.project,
uri=uri,
)
Loading