Skip to content
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
13 changes: 11 additions & 2 deletions src/sentry/integrations/github/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,14 @@ def dispatch(self, request: HttpRequest, pipeline: Pipeline) -> HttpResponseBase
if chosen_installation_id == "-1":
return pipeline.next_step()

if not has_scm_multi_org:
# Validate the same org is installing and that they have the multi org feature
installing_organization_slug = pipeline.fetch_state("installing_organization_slug")
is_same_installing_org = (
(installing_organization_slug is not None)
and installing_organization_slug
== self.active_user_organization.organization.slug
)
if not has_scm_multi_org or not is_same_installing_org:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! But should we handle this with a separate error more generally, like break the pipeline if the user is seeing resources from a different organization after re-fetching? I'll leave it to you but I think that switching organizations mid installation is totally fine to break the installation flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooo that makes sense uh, I just would need to figure out how to do that in the frontend :hides:

lifecycle.record_failure(GitHubInstallationError.FEATURE_NOT_AVAILABLE)
return error(
request,
Expand All @@ -954,7 +961,9 @@ def dispatch(self, request: HttpRequest, pipeline: Pipeline) -> HttpResponseBase

pipeline.bind_state("chosen_installation", chosen_installation_id)
return pipeline.next_step()

pipeline.bind_state(
"installing_organization_slug", self.active_user_organization.organization.slug
)
serialized_organization = organization_service.serialize_organization(
id=self.active_user_organization.organization.id,
as_user=(
Expand Down
55 changes: 55 additions & 0 deletions tests/sentry/integrations/github/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1736,3 +1736,58 @@ def test_github_installation_filters_valid_installations(self):
"avatar_url": "https://github.com/knobiknows/all-the-bufo/raw/main/all-the-bufo/bufo-pog.png",
},
]

@with_feature("organizations:integrations-scm-multi-org")
@with_feature("organizations:github-multi-org")
@responses.activate
@patch("sentry.integrations.utils.metrics.EventLifecycle.record_event")
def test_github_installation_validates_installing_organization(self, mock_record):
self._setup_with_existing_installations()
chosen_installation_id = "1"

# Initiate the OAuthView
resp = self.client.get(self.init_path)
assert resp.status_code == 302
redirect = urlparse(resp["Location"])
assert redirect.scheme == "https"
assert redirect.netloc == "github.com"
assert redirect.path == "/login/oauth/authorize"
assert (
redirect.query
== f"client_id=github-client-id&state={self.pipeline.signature}&redirect_uri=http://testserver/extensions/github/setup/"
)

# We just got resp. from GH, continue with OAuthView -> GithubOrganizationSelection
resp = self.client.get(
"{}?{}".format(
self.setup_path,
urlencode({"code": "12345678901234567890", "state": self.pipeline.signature}),
)
)
assert resp.status_code == 200

# Create a new organization and switch to it
self.create_organization(name="new-org", owner=self.user)
self.login_as(self.user)

# Try to continue the installation with the new organization
resp = self.client.get(
"{}?{}".format(
self.setup_path,
urlencode(
{
"code": "12345678901234567890",
"state": self.pipeline.signature,
"chosen_installation_id": chosen_installation_id,
}
),
)
)

self.assertTemplateUsed(resp, "sentry/integrations/github-integration-failed.html")
assert (
b'{"success":false,"data":{"error":"Your organization does not have access to this feature."}}'
in resp.content
)
assert b'window.opener.postMessage({"success":false' in resp.content
assert_failure_metric(mock_record, GitHubInstallationError.FEATURE_NOT_AVAILABLE)
Loading