Skip to content

Commit 1cbbb2d

Browse files
Timna BrownTimna Brown
authored andcommitted
Fix org discussion category lookup
1 parent 7821d19 commit 1cbbb2d

2 files changed

Lines changed: 31 additions & 45 deletions

File tree

.github/workflows/create_monthly_discussion.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,9 @@
1818

1919
QUERY_ORG = """
2020
query($org: String!) {
21-
organization(login: $org) {
22-
id
23-
discussionCategories(first: 100) {
24-
nodes {
21+
organization(login: $org) {
2522
id
26-
name
27-
slug
28-
}
2923
}
30-
}
3124
}
3225
"""
3326

@@ -39,6 +32,10 @@
3932
id
4033
title
4134
url
35+
category {
36+
id
37+
name
38+
}
4239
}
4340
pageInfo {
4441
hasNextPage
@@ -138,12 +135,12 @@ def normalize_title(value):
138135
return (value or "").strip().lower()
139136

140137

141-
def fetch_org_info(org, token):
138+
def fetch_org_id(org, token):
142139
data = graphql_request(token, QUERY_ORG, {"org": org})
143140
org_data = data.get("organization")
144-
if not org_data:
141+
if not org_data or not org_data.get("id"):
145142
raise RuntimeError(f"Organization not found: {org}")
146-
return org_data
143+
return org_data.get("id")
147144

148145

149146
def fetch_discussions(org, token):
@@ -172,9 +169,10 @@ def fetch_discussions(org, token):
172169
return discussions
173170

174171

175-
def find_category_id(categories, name):
172+
def find_category_id_from_discussions(discussions, name):
176173
name_normalized = normalize_title(name)
177-
for category in categories:
174+
for discussion in discussions:
175+
category = discussion.get("category") or {}
178176
if normalize_title(category.get("name")) == name_normalized:
179177
return category.get("id")
180178
return None
@@ -284,14 +282,10 @@ def main():
284282
prev_title = f"Catalog Index - {prev_month_name} {prev_year}"
285283
last_updated = datetime.now(timezone.utc).strftime("%Y-%m-%d")
286284

287-
org_info = fetch_org_info(org, TOKEN)
288-
category_id = find_category_id(
289-
org_info.get("discussionCategories", {}).get("nodes", []), CATEGORY_NAME
290-
)
285+
discussions = fetch_discussions(org, TOKEN)
286+
category_id = find_category_id_from_discussions(discussions, CATEGORY_NAME)
291287
if not category_id:
292288
raise RuntimeError(f"Category not found: {CATEGORY_NAME}")
293-
294-
discussions = fetch_discussions(org, TOKEN)
295289
if find_discussion_by_title(discussions, title):
296290
print(f"Discussion already exists: {title}")
297291
return
@@ -335,7 +329,7 @@ def main():
335329
TOKEN,
336330
MUTATION_CREATE,
337331
{
338-
"orgId": org_info.get("id"),
332+
"orgId": fetch_org_id(org, TOKEN),
339333
"categoryId": category_id,
340334
"title": title,
341335
"body": body,

.github/workflows/ensure_welcome_discussion.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,25 @@
1212

1313
QUERY_ORG = """
1414
query($org: String!) {
15-
organization(login: $org) {
16-
id
17-
discussionCategories(first: 100) {
18-
nodes {
15+
organization(login: $org) {
1916
id
20-
name
21-
slug
22-
}
2317
}
24-
}
2518
}
2619
"""
2720

2821
QUERY_DISCUSSIONS = """
2922
query($org: String!, $after: String) {
3023
organization(login: $org) {
3124
discussions(first: 50, after: $after) {
32-
nodes {
33-
id
34-
title
35-
url
25+
nodes {
26+
id
27+
title
28+
url
3629
category {
30+
id
3731
name
3832
}
39-
}
33+
}
4034
pageInfo {
4135
hasNextPage
4236
endCursor
@@ -100,12 +94,12 @@ def normalize_title(value):
10094
return (value or "").strip().lower()
10195

10296

103-
def fetch_org_info(org, token):
97+
def fetch_org_id(org, token):
10498
data = graphql_request(token, QUERY_ORG, {"org": org})
10599
org_data = data.get("organization")
106-
if not org_data:
100+
if not org_data or not org_data.get("id"):
107101
raise RuntimeError(f"Organization not found: {org}")
108-
return org_data
102+
return org_data.get("id")
109103

110104

111105
def fetch_discussions(org, token):
@@ -134,9 +128,10 @@ def fetch_discussions(org, token):
134128
return discussions
135129

136130

137-
def find_category_id(categories, name):
131+
def find_category_id_from_discussions(discussions, name):
138132
name_normalized = normalize_title(name)
139-
for category in categories:
133+
for discussion in discussions:
134+
category = discussion.get("category") or {}
140135
if normalize_title(category.get("name")) == name_normalized:
141136
return category.get("id")
142137
return None
@@ -175,26 +170,23 @@ def main():
175170
print("Organization is missing from config")
176171
sys.exit(1)
177172

178-
org_info = fetch_org_info(org, TOKEN)
179-
category_id = find_category_id(
180-
org_info.get("discussionCategories", {}).get("nodes", []), WELCOME_CATEGORY
181-
)
173+
discussions = fetch_discussions(org, TOKEN)
174+
category_id = find_category_id_from_discussions(discussions, WELCOME_CATEGORY)
182175
if not category_id:
183176
raise RuntimeError(f"Category not found: {WELCOME_CATEGORY}")
184-
185-
discussions = fetch_discussions(org, TOKEN)
186177
existing = find_discussion_by_title(discussions, WELCOME_TITLE, WELCOME_CATEGORY)
187178

188179
if existing:
189180
discussion_id = existing.get("id")
190181
discussion_url = existing.get("url")
191182
else:
192183
body = read_text(WELCOME_TEMPLATE)
184+
org_id = fetch_org_id(org, TOKEN)
193185
create_data = graphql_request(
194186
TOKEN,
195187
MUTATION_CREATE,
196188
{
197-
"orgId": org_info.get("id"),
189+
"orgId": org_id,
198190
"categoryId": category_id,
199191
"title": WELCOME_TITLE,
200192
"body": body,

0 commit comments

Comments
 (0)