Repository: getsentry/sentry

Issue:
AttributeError /api/0/organizations/{organization_slug}/sentry-apps/
## Important Details

How are you running Sentry?

<!-- Please pick one of the following -->
On-Premise w/ Docker, Sentry 21.1.0486d790, based on https://github.com/Rungutan/sentry-performance-monitoring

## Description

Opening of these sub items in "Organization" settings fails:
* Audit Log
* Integrations
* Developer settings

This seems to be the resulting issue in the "Internal" Sentry project:
```
AttributeError: 'NoneType' object has no attribute 'get_allowed_origins'
  File "sentry/api/base.py", line 124, in handle_exception
    response = super(Endpoint, self).handle_exception(exc)
  File "rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "sentry/api/base.py", line 237, in dispatch
    response = handler(request, *args, **kwargs)
  File "sentry/api/bases/sentryapps.py", line 59, in wrapped
    return func(self, *args, **kwargs)
  File "sentry/api/endpoints/organization_sentry_apps.py", line 19, in get
    on_results=lambda x: serialize(x, request.user, access=request.access),
  File "sentry/api/base.py", line 330, in paginate
    results = on_results(cursor_result.results)
  File "sentry/api/endpoints/organization_sentry_apps.py", line 19, in <lambda>
    on_results=lambda x: serialize(x, request.user, access=request.access),
  File "sentry/api/serializers/base.py", line 45, in serialize
    return [serializer(o, attrs=attrs.get(o, {}), user=user, **kwargs) for o in objects]
  File "sentry/api/serializers/base.py", line 45, in <listcomp>
    return [serializer(o, attrs=attrs.get(o, {}), user=user, **kwargs) for o in objects]
  File "sentry/api/serializers/base.py", line 60, in __call__
    return self.serialize(obj, attrs, user, **kwargs)
  File "sentry/api/serializers/models/sentry_app.py", line 31, in serialize
    "allowedOrigins": obj.application.get_allowed_origins(),
```

Last SQL before exception

```
SELECT "sentry_organization"."id", "sentry_organization"."name", "sentry_organization"."slug", "sentry_organization"."status", "sentry_organization"."date_added", "sentry_organization"."default_role", "sentry_organization"."flags" FROM "sentry_organization" WHERE "sentry_organization"."id" = %s
```

### What you expected to happen
Menus should work.

### Possible Solution

[If you have an idea on how this could be solved include that detail here.]



Discussion:
I am unable to reproduce this issue with the latest nightly using getsentry/onpremise repo. You may want to file this issue under https://github.com/Rungutan/sentry-performance-monitoring
Steps to reproduce. I had luck and could reproduce it on my staging environment. 
- create a internal integration e.g for Sentry CLI, just use the default rights needed for CLI access like described in the docs. 
- go to admin - users. 
- delete the cli user from there and check „delete user and all data“
- Go to one of the mentioned menu items and you will get the exception. 

In my case I deleted the cli user because I wanted to create a new one. 
Please fix this, since I am unable to use the menu items mentioned anymore. 

Shall I create a new issue or are you opening it again @BYK?

thx. 
Needless to say I broke my staging environment now as well 😂
Added the needs more info flag so that I don't forget to verify this.
Appreciated 🍻
Sending you some links to the issues on our system, please keep them non-public.

Resolving patch:
diff --git a/src/sentry/api/endpoints/organization_sentry_apps.py b/src/sentry/api/endpoints/organization_sentry_apps.py
--- a/src/sentry/api/endpoints/organization_sentry_apps.py
+++ b/src/sentry/api/endpoints/organization_sentry_apps.py
@@ -7,7 +7,7 @@
 class OrganizationSentryAppsEndpoint(OrganizationEndpoint):
     @add_integration_platform_metric_tag
     def get(self, request, organization):
-        queryset = SentryApp.objects.filter(owner=organization)
+        queryset = SentryApp.objects.filter(owner=organization, application__isnull=False)
 
         return self.paginate(
             request=request,


Test patch: