-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaapi_weekly_update.py
More file actions
515 lines (437 loc) Β· 15.7 KB
/
kaapi_weekly_update.py
File metadata and controls
515 lines (437 loc) Β· 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python3
"""
Fetches the current iteration from a GitHub Projects v2 board and posts a
formatted summary to Discord. Runs via cron every Monday at 9 AM IST, or
manually with --dry-run.
"""
import argparse
import json
import os
import sys
from collections import defaultdict
from datetime import date, datetime, timedelta
import requests
from dotenv import load_dotenv
load_dotenv()
GITHUB_API_URL = "https://api.github.com/graphql"
# ββ GitHub GraphQL ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
PROJECT_META_QUERY = """
query ProjectMeta($org: String!, $number: Int!) {
organization(login: $org) {
projectV2(number: $number) {
id
title
fields(first: 50) {
nodes {
__typename
... on ProjectV2IterationField {
id
name
configuration {
iterations {
id
title
startDate
duration
}
completedIterations {
id
title
startDate
duration
}
}
}
}
}
}
}
}
"""
PROJECT_ITEMS_QUERY = """
query ProjectItems($org: String!, $number: Int!, $after: String) {
organization(login: $org) {
projectV2(number: $number) {
items(first: 50, after: $after) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
content {
__typename
... on Issue {
number
title
url
repository { nameWithOwner }
assignees(first: 10) { nodes { login name } }
}
... on PullRequest {
number
title
url
repository { nameWithOwner }
assignees(first: 10) { nodes { login name } }
}
... on DraftIssue {
title
assignees(first: 10) { nodes { login name } }
}
}
fieldValues(first: 20) {
nodes {
__typename
... on ProjectV2ItemFieldSingleSelectValue {
name
field {
... on ProjectV2SingleSelectField { name }
}
}
... on ProjectV2ItemFieldIterationValue {
title
startDate
duration
iterationId
field {
... on ProjectV2IterationField { name }
}
}
}
}
}
}
}
}
}
"""
def _execute_graphql(query, variables, token):
"""Execute a GraphQL query against the GitHub API."""
response = requests.post(
GITHUB_API_URL,
json={"query": query, "variables": variables},
headers={
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
},
timeout=30,
)
response.raise_for_status()
body = response.json()
if "errors" in body:
raise RuntimeError(f"GitHub GraphQL errors: {body['errors']}")
return body["data"]
def _find_current_iteration(project_fields, iteration_field_name="Weekly Tasks"):
"""Pick today's iteration from the named iteration field."""
today = date.today()
target_name_lower = iteration_field_name.lower()
iteration_field = None
for field in project_fields:
if field.get("__typename") != "ProjectV2IterationField":
continue
if field["name"].lower() == target_name_lower:
iteration_field = field
break
# Fall back to the first iteration field if the named one isn't found
if iteration_field is None:
for field in project_fields:
if field.get("__typename") == "ProjectV2IterationField":
iteration_field = field
break
if iteration_field is None:
return None
config = iteration_field.get("configuration") or {}
for iteration in config.get("iterations", []):
start = datetime.strptime(iteration["startDate"], "%Y-%m-%d").date()
end = start + timedelta(days=iteration["duration"])
if start <= today < end:
return {
"field_name": iteration_field["name"],
"id": iteration["id"],
"title": iteration["title"],
"start": start.isoformat(),
"end": (end - timedelta(days=1)).isoformat(),
"total_days": iteration["duration"],
"days_elapsed": (today - start).days + 1,
}
return None
def _paginate_items(org, number, token):
"""Paginate through all items in a project (50 at a time)."""
all_items = []
cursor = None
while True:
variables = {"org": org, "number": number}
if cursor:
variables["after"] = cursor
data = _execute_graphql(PROJECT_ITEMS_QUERY, variables, token)
items_data = data["organization"]["projectV2"]["items"]
all_items.extend(items_data["nodes"])
page_info = items_data["pageInfo"]
if not page_info["hasNextPage"]:
break
cursor = page_info["endCursor"]
return all_items
def _extract_field_values(raw_field_values):
"""Flatten a project item's fieldValues into {status, iteration_id}."""
status = None
iteration_id = None
for value in raw_field_values:
typename = value.get("__typename")
field_info = value.get("field") or {}
field_name = (field_info.get("name") or "").lower()
if typename == "ProjectV2ItemFieldSingleSelectValue" and field_name == "status":
status = value.get("name")
elif typename == "ProjectV2ItemFieldIterationValue":
iteration_id = value.get("iterationId")
return status, iteration_id
def fetch_current_iteration(org, project_number, token):
"""Fetch current iteration metadata and all items belonging to it."""
meta = _execute_graphql(
PROJECT_META_QUERY, {"org": org, "number": project_number}, token
)
project = meta["organization"]["projectV2"]
if project is None:
return None
fields = project["fields"]["nodes"]
current = _find_current_iteration(fields)
if current is None:
return None
raw_items = _paginate_items(org, project_number, token)
items = []
for item in raw_items:
content = item.get("content") or {}
status, iteration_id = _extract_field_values(
item["fieldValues"]["nodes"]
)
if iteration_id != current["id"]:
continue
assignees = [
a.get("name") or a.get("login")
for a in (content.get("assignees", {}) or {}).get("nodes", [])
]
identifier = (
f"{content['repository']['nameWithOwner'].split('/')[-1]}#{content['number']}"
if content.get("__typename") in ("Issue", "PullRequest")
else "Draft"
)
items.append(
{
"identifier": identifier,
"title": content.get("title", "(untitled)"),
"url": content.get("url"),
"status": status or "No status",
"assignees": assignees or ["Unassigned"],
}
)
return {
"project_title": project["title"],
"iteration_title": current["title"],
"starts_at": current["start"],
"ends_at": current["end"],
"progress": min(current["days_elapsed"] / current["total_days"], 1.0),
"items": items,
}
# ββ Discord embed builder ββββββββββββββββββββββββββββββββββββββββββββββββββ
TRACKED_MEMBERS = ["akhilesh", "nishika", "prajna", "prashant", "ayush"]
STATE_DISPLAY_ORDER = ["Closed", "In Review", "In Progress", "To Do"]
STATE_NAME_MAP = {
"closed": "Closed",
"done": "Closed",
"in review": "In Review",
"in progress": "In Progress",
"to do": "To Do",
"todo": "To Do",
}
STATE_ICONS = {
"Closed": "\U0001f7e2",
"In Review": "\U0001f50d",
"In Progress": "\U0001f7e1",
"To Do": "βͺ",
}
STATE_SORT_ORDER = {s: i for i, s in enumerate(STATE_DISPLAY_ORDER)}
def _display_state(state_name):
return STATE_NAME_MAP.get(state_name.lower(), state_name)
def _matched_member(assignees):
"""Return the tracked-member key if any assignee matches, else None."""
for assignee in assignees:
lower = assignee.lower()
for member in TRACKED_MEMBERS:
if member in lower:
return assignee
return None
def _progress_bar(progress, length=10):
filled = round(progress * length)
empty = length - filled
pct = round(progress * 100)
if progress >= 0.70:
filled_char = "π©" # green square
elif progress >= 0.30:
filled_char = "π¨" # yellow square
else:
filled_char = "π₯" # red square
return f"{filled_char * filled}{'β¬' * empty} {pct}%"
def _progress_color(progress):
if progress >= 0.70:
return 0x2ECC71
if progress >= 0.30:
return 0xF1C40F
return 0xE74C3C
def _truncate(text, max_len=40):
return text if len(text) <= max_len else text[: max_len - 1] + "β¦"
def _ordinal(day):
if 10 <= day % 100 <= 20:
suffix = "th"
else:
suffix = {1: "st", 2: "nd", 3: "rd"}.get(day % 10, "th")
return f"{day}{suffix}"
def _format_iteration_range(start_iso, end_iso):
start = datetime.strptime(start_iso, "%Y-%m-%d").date()
end = datetime.strptime(end_iso, "%Y-%m-%d").date()
return f"{_ordinal(start.day)} {start.strftime('%B')} - {_ordinal(end.day)} {end.strftime('%B')}"
def build_messages(iteration_data):
all_items = iteration_data["items"]
by_member = defaultdict(list)
for item in all_items:
match = _matched_member(item["assignees"])
if match:
by_member[match].append(item)
tracked_items = [i for items in by_member.values() for i in items]
state_totals = defaultdict(int)
for i in tracked_items:
state_totals[_display_state(i["status"])] += 1
totals_line = ", ".join(
f"{state_totals[s]} {s} {STATE_ICONS.get(s, '')}".strip()
for s in STATE_DISPLAY_ORDER
)
total_count = len(tracked_items)
closed_count = state_totals.get("Closed", 0)
progress = closed_count / total_count if total_count else 0.0
summary_lines = []
for name in sorted(by_member.keys()):
stats = defaultdict(int)
for i in by_member[name]:
stats[_display_state(i["status"])] += 1
parts = []
for s in STATE_DISPLAY_ORDER:
if stats.get(s):
parts.append(f"{stats[s]} {s.lower()}")
for s, count in stats.items():
if s not in STATE_DISPLAY_ORDER:
parts.append(f"{count} {s.lower()}")
first_name = name.split("@")[0].split()[0].title()
summary_lines.append(f"**{first_name}**: {', '.join(parts)}")
iteration_range = _format_iteration_range(
iteration_data["starts_at"], iteration_data["ends_at"]
)
iteration_label = f"{iteration_data['iteration_title']} ({iteration_range})"
summary_embed = {
"title": f"Weekly Update: {iteration_label} - {iteration_data['project_title']}",
"description": (
f"{_progress_bar(progress)}\n"
f"{totals_line}\n\n"
+ ("\n".join(summary_lines) if summary_lines else "_No tracked-member items in this iteration._")
),
"color": _progress_color(progress),
}
payloads = [{"embeds": [summary_embed]}]
for name in sorted(by_member.keys()):
member_items = by_member[name]
member_items.sort(
key=lambda i: STATE_SORT_ORDER.get(_display_state(i["status"]), 99)
)
first_name = name.split("@")[0].split()[0].title()
stats = defaultdict(int)
for i in member_items:
stats[_display_state(i["status"])] += 1
subtitle_parts = []
for s in STATE_DISPLAY_ORDER:
if stats.get(s):
subtitle_parts.append(f"{stats[s]} {s.lower()}")
lines = [f"## {first_name} ({', '.join(subtitle_parts)})"]
for i in member_items:
display = _display_state(i["status"])
icon = STATE_ICONS.get(display, "βͺ")
lines.append(f"{icon} `{i['identifier']}` {i['title']}")
content = "\n".join(lines)
if len(content) > 1990:
content = content[:1987] + "..."
payloads.append({"content": content})
return payloads
# ββ Discord sender ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def send_discord_message(webhook_url, payload):
response = requests.post(webhook_url, json=payload, timeout=10)
response.raise_for_status()
# ββ CLI βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def main():
parser = argparse.ArgumentParser(
description="Post GitHub Projects v2 iteration progress to Discord."
)
parser.add_argument("--org", type=str, help="GitHub org (overrides ORG)")
parser.add_argument(
"--project-number",
type=int,
help="GitHub project number (overrides PROJECT_NUMBER)",
)
parser.add_argument(
"--webhook-url",
type=str,
help="Discord webhook URL (overrides KAAPI_DISCORD_WEBHOOK)",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Print Discord payload to stdout instead of posting",
)
args = parser.parse_args()
token = os.environ.get("GITHUB_TOKEN")
org = args.org or os.environ.get("ORG")
project_number_raw = (
args.project_number or os.environ.get("PROJECT_NUMBER")
)
webhook_url = args.webhook_url or os.environ.get("KAAPI_DISCORD_WEBHOOK")
if not token:
print("Error: GITHUB_TOKEN is not set.", file=sys.stderr)
sys.exit(1)
if not org:
print("Error: ORG is not set and --org not provided.", file=sys.stderr)
sys.exit(1)
if not project_number_raw:
print(
"Error: PROJECT_NUMBER is not set and --project-number not provided.",
file=sys.stderr,
)
sys.exit(1)
project_number = int(project_number_raw)
if not webhook_url and not args.dry_run:
print(
"Error: KAAPI_DISCORD_WEBHOOK is not set and --webhook-url not provided.",
file=sys.stderr,
)
sys.exit(1)
try:
iteration_data = fetch_current_iteration(org, project_number, token)
except requests.HTTPError as e:
print(f"Error calling GitHub API: {e}", file=sys.stderr)
sys.exit(1)
except RuntimeError as e:
print(f"GitHub API error: {e}", file=sys.stderr)
sys.exit(1)
if iteration_data is None:
print(
f"No current iteration found for {org}/projects/{project_number}. Nothing to report."
)
sys.exit(0)
payloads = build_messages(iteration_data)
if args.dry_run:
print(json.dumps(payloads, indent=2))
else:
try:
for payload in payloads:
send_discord_message(webhook_url, payload)
print("Discord update posted successfully.")
except requests.HTTPError as e:
print(f"Error posting to Discord: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()