Skip to content

Commit b620f33

Browse files
committed
fix(wizard) Fix possibly unbound variable in setup wizard
There have been a few UnboundLocalError events captured which wouldn't happen with this approach. Fixes SENTRY-180Q
1 parent 0552afe commit b620f33

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/sentry/web/frontend/setup_wizard.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from sentry.services.hybrid_cloud.project.service import project_service
2323
from sentry.services.hybrid_cloud.project_key.model import ProjectKeyRole
2424
from sentry.services.hybrid_cloud.project_key.service import project_key_service
25+
from sentry.services.hybrid_cloud.user.model import RpcUser
2526
from sentry.utils.http import absolute_uri
2627
from sentry.utils.security.orgauthtoken_token import (
2728
SystemUrlPrefixMissingException,
@@ -75,8 +76,8 @@ def get(self, request: HttpRequest, wizard_hash) -> HttpResponse:
7576
# responses, but project names/slugs aren't unique across regions which could confuse some users.
7677
# Wizard should display region beside project/orgs or have a step to ask which region.
7778

78-
region_data_map = defaultdict(lambda: defaultdict(list))
7979
# {'us': {'org_ids': [...], 'projects': [...], 'keys': [...]}}
80+
region_data_map = defaultdict(lambda: defaultdict(list))
8081

8182
for mapping in org_mappings:
8283
region_data_map[mapping.region_name]["org_ids"].append(mapping.organization_id)
@@ -108,25 +109,24 @@ def get(self, request: HttpRequest, wizard_hash) -> HttpResponse:
108109
org_mappings_map[mapping.organization_id] = serialized_mapping
109110

110111
keys_map = defaultdict(list)
111-
for key in region_data["keys"]:
112-
serialized_key = {
113-
"dsn": {"public": key.dsn_public},
114-
"isActive": key.is_active,
115-
}
116-
keys_map[key.project_id].append(serialized_key)
117-
118112
filled_projects = []
119-
120-
for project in region_data["projects"]:
121-
enriched_project = {
122-
"slug": project.slug,
123-
"id": project.id,
124-
"status": STATUS_LABELS.get(project.status, "unknown"),
125-
}
126-
# The wizard only reads the a few fields so serializing the mapping should work fine
127-
enriched_project["organization"] = org_mappings_map[project.organization_id]
128-
enriched_project["keys"] = keys_map[project.id]
129-
filled_projects.append(enriched_project)
113+
for region_name, region_data in region_data_map.items():
114+
for key in region_data["keys"]:
115+
serialized_key = {
116+
"dsn": {"public": key.dsn_public},
117+
"isActive": key.is_active,
118+
}
119+
keys_map[key.project_id].append(serialized_key)
120+
for project in region_data["projects"]:
121+
enriched_project = {
122+
"slug": project.slug,
123+
"id": project.id,
124+
"status": STATUS_LABELS.get(project.status, "unknown"),
125+
}
126+
# The wizard only reads the a few fields so serializing the mapping should work fine
127+
enriched_project["organization"] = org_mappings_map[project.organization_id]
128+
enriched_project["keys"] = keys_map[project.id]
129+
filled_projects.append(enriched_project)
130130

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

142142

143-
def get_token(mappings: List[OrganizationMapping], user: User):
143+
def get_token(mappings: List[OrganizationMapping], user: User | RpcUser):
144144
can_use_org_tokens = len(mappings) == 1
145145

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

166166

167-
def get_org_token(mapping: OrganizationMapping, user: User):
167+
def get_org_token(mapping: OrganizationMapping, user: User | RpcUser):
168168
try:
169169
token_str = generate_token(
170170
mapping.slug, generate_region_url(region_name=mapping.region_name)

0 commit comments

Comments
 (0)