Skip to content

Commit

Permalink
fix(wizard) Fix possibly unbound variable in setup wizard (#59934)
Browse files Browse the repository at this point in the history
There have been a few UnboundLocalError events captured which wouldn't
happen with this approach.

Fixes SENTRY-180Q
  • Loading branch information
markstory authored Nov 15, 2023
1 parent 79c6ee3 commit 0365345
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/sentry/web/frontend/setup_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sentry.services.hybrid_cloud.project.service import project_service
from sentry.services.hybrid_cloud.project_key.model import ProjectKeyRole
from sentry.services.hybrid_cloud.project_key.service import project_key_service
from sentry.services.hybrid_cloud.user.model import RpcUser
from sentry.utils.http import absolute_uri
from sentry.utils.security.orgauthtoken_token import (
SystemUrlPrefixMissingException,
Expand Down Expand Up @@ -75,58 +76,57 @@ def get(self, request: HttpRequest, wizard_hash) -> HttpResponse:
# responses, but project names/slugs aren't unique across regions which could confuse some users.
# Wizard should display region beside project/orgs or have a step to ask which region.

region_data_map = defaultdict(lambda: defaultdict(list))
# {'us': {'org_ids': [...], 'projects': [...], 'keys': [...]}}
region_data_map = defaultdict(lambda: defaultdict(list))

org_mappings_map = {}
for mapping in org_mappings:
region_data_map[mapping.region_name]["org_ids"].append(mapping.organization_id)
status = OrganizationStatus(mapping.status)
serialized_mapping = {
"id": mapping.organization_id,
"name": mapping.name,
"slug": mapping.slug,
"region": mapping.region_name,
"status": {"id": status.name.lower(), "name": status.label},
}
org_mappings_map[mapping.organization_id] = serialized_mapping

for region_name, region_data in region_data_map.items():
org_ids = region_data["org_ids"]
region_data["projects"] = project_service.get_many_by_organizations(
projects = project_service.get_many_by_organizations(
region_name=region_name, organization_ids=org_ids
)
region_data["projects"] = projects

keys_map = defaultdict(list)
for region_name, region_data in region_data_map.items():
project_ids = [rpc_project.id for rpc_project in region_data["projects"]]
region_data["keys"] = project_key_service.get_project_keys_by_region(
keys = project_key_service.get_project_keys_by_region(
region_name=region_name,
project_ids=project_ids,
role=ProjectKeyRole.store,
)

org_mappings_map = {}
for mapping in org_mappings:
status = OrganizationStatus(mapping.status)
serialized_mapping = {
"id": mapping.organization_id,
"name": mapping.name,
"slug": mapping.slug,
"region": mapping.region_name,
"status": {"id": status.name.lower(), "name": status.label},
}
org_mappings_map[mapping.organization_id] = serialized_mapping

keys_map = defaultdict(list)
for key in region_data["keys"]:
serialized_key = {
"dsn": {"public": key.dsn_public},
"isActive": key.is_active,
}
keys_map[key.project_id].append(serialized_key)
region_data["keys"] = keys
for key in region_data["keys"]:
serialized_key = {
"dsn": {"public": key.dsn_public},
"isActive": key.is_active,
}
keys_map[key.project_id].append(serialized_key)

filled_projects = []

for project in region_data["projects"]:
enriched_project = {
"slug": project.slug,
"id": project.id,
"status": STATUS_LABELS.get(project.status, "unknown"),
}
# The wizard only reads the a few fields so serializing the mapping should work fine
enriched_project["organization"] = org_mappings_map[project.organization_id]
enriched_project["keys"] = keys_map[project.id]
filled_projects.append(enriched_project)
for region_name, region_data in region_data_map.items():
for project in region_data["projects"]:
enriched_project = {
"slug": project.slug,
"id": project.id,
"status": STATUS_LABELS.get(project.status, "unknown"),
}
# The wizard only reads the a few fields so serializing the mapping should work fine
enriched_project["organization"] = org_mappings_map[project.organization_id]
enriched_project["keys"] = keys_map[project.id]
filled_projects.append(enriched_project)

# Fetching or creating a token
serialized_token = get_token(org_mappings, request.user)
Expand All @@ -140,7 +140,7 @@ def get(self, request: HttpRequest, wizard_hash) -> HttpResponse:
return render_to_response("sentry/setup-wizard.html", context, request)


def get_token(mappings: List[OrganizationMapping], user: User):
def get_token(mappings: List[OrganizationMapping], user: RpcUser):
can_use_org_tokens = len(mappings) == 1

# If only one org, try to generate an org auth token
Expand All @@ -164,7 +164,7 @@ def get_token(mappings: List[OrganizationMapping], user: User):
return serialize(token)


def get_org_token(mapping: OrganizationMapping, user: User):
def get_org_token(mapping: OrganizationMapping, user: User | RpcUser):
try:
token_str = generate_token(
mapping.slug, generate_region_url(region_name=mapping.region_name)
Expand Down

0 comments on commit 0365345

Please sign in to comment.