-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfoc_wg_pr_notifier.py
More file actions
780 lines (676 loc) · 29.5 KB
/
foc_wg_pr_notifier.py
File metadata and controls
780 lines (676 loc) · 29.5 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#!/usr/bin/env python3
"""
FOC-WG PR Notifier
This script queries FilOzone GitHub Project 14 for open PRs matching view 32 filters
and posts a daily summary to #foc-bots in Slack via incoming webhook.
View 32 filters (https://github.com/orgs/FilOzone/projects/14/views/32):
- is:pr
- -status:"🎉 Done"
- -milestone:"MX: Priority and sequencing TBD"
- -milestone:"M4.5: GA Fast Follows"
"""
import os
import sys
import json
import requests
from datetime import datetime
from typing import List, Dict, Any, Optional
from urllib.parse import quote
import argparse
_REPO_ROOT = os.path.dirname(os.path.abspath(__file__))
_FOC_PR_REPORT_DIR = os.path.join(_REPO_ROOT, "foc-pr-report")
if _FOC_PR_REPORT_DIR not in sys.path:
sys.path.insert(0, _FOC_PR_REPORT_DIR)
from foc_pr_report.pr_enrichment import ( # noqa: E402
fetch_project_board_items_rest_filtered,
field_values_by_name,
)
# Milestones to exclude (view 32 filter)
EXCLUDED_MILESTONES = [
"MX: Priority and sequencing TBD",
"M4.5: GA Fast Follows",
]
NOTIFIER_FILTER = (
"is:pr "
'-status:"🎉 Done" '
'-milestone:"MX: Priority and sequencing TBD" '
'-milestone:"M4.5: GA Fast Follows"'
)
# Status constants for categorization
AWAITING_REVIEW_STATUS = "🔎 Awaiting review"
IN_PROGRESS_STATUSES = ["📌 Triage", "🐱 Todo", "⌨️ In Progress"]
DONE_STATUS = "🎉 Done"
# Base URL for personalized GitHub project views
PROJECT_VIEW_BASE_URL = "https://github.com/orgs/FilOzone/projects/14/views/18"
class FOCWGNotifier:
"""Fetches PRs from FilOzone Project 14 and posts to Slack."""
def __init__(
self,
github_token: str,
slack_webhook_url: Optional[str] = None,
user_map_path: Optional[str] = None,
):
self.github_token = github_token
self.slack_webhook_url = slack_webhook_url
self.session = requests.Session()
self.session.headers.update(
{
"Authorization": f"Bearer {github_token}",
"Content-Type": "application/json",
}
)
self.user_map = self._load_user_map(user_map_path)
def _load_user_map(self, user_map_path: Optional[str] = None) -> Dict[str, str]:
"""Load GitHub username to Slack user ID mapping from JSON file."""
if user_map_path is None:
# Default to gh_slack_users.json in the same directory as the script
script_dir = os.path.dirname(os.path.abspath(__file__))
user_map_path = os.path.join(script_dir, "gh_slack_users.json")
try:
with open(user_map_path, "r") as f:
user_map = json.load(f)
print(f"Loaded {len(user_map)} user mappings from {user_map_path}")
return user_map
except FileNotFoundError:
print(f"User map file not found: {user_map_path} (using empty mapping)")
return {}
except json.JSONDecodeError as e:
print(f"Error parsing user map file: {e} (using empty mapping)")
return {}
def fetch_project_items(self) -> List[Dict[str, Any]]:
"""Fetch notifier-relevant project items via REST board filtering."""
return fetch_project_board_items_rest_filtered(
self.session,
filter_query=NOTIFIER_FILTER,
verbose=True,
)
def filter_items(self, items: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Apply filters to the items (open PRs, non-draft, excluding certain milestones and Done status)."""
filtered = []
for item in items:
content = item.get("content")
if not content:
continue
# Filter: Only PRs (not issues)
if content.get("__typename") != "PullRequest":
continue
# Filter: Only open PRs
if content.get("state") != "OPEN":
continue
# Filter: Exclude draft PRs
if content.get("isDraft"):
continue
field_values = field_values_by_name(item)
# Filter: Exclude status "🎉 Done"
status = field_values.get("Status")
if status == DONE_STATUS:
continue
# Filter: Exclude specific milestones
milestone = content.get("milestone", {})
milestone_title = milestone.get("title") if milestone else None
if milestone_title in EXCLUDED_MILESTONES:
continue
# Add field values to content for later use
content["_project_fields"] = field_values
filtered.append(content)
print(f"Filtered to {len(filtered)} PRs (from {len(items)} total items)")
return filtered
def _safe_field_text(self, label: str, value: str, max_length: int = 100) -> str:
"""Safely format a field text, ensuring it doesn't exceed limits."""
# Handle None or empty values
if not value or value.strip() == "" or value == "None":
value = "None"
else:
# Truncate value if needed
if len(value) > max_length:
value = value[: max_length - 3] + "..."
return f"*{label}:*\n{value}"
def build_github_link(self, username: str) -> str:
"""Build a URL-encoded link to View 18 filtered by username."""
filter_query = f'is:pr -status:"🎉 Done" {username}'
return f"{PROJECT_VIEW_BASE_URL}?filterQuery={quote(filter_query)}"
def _get_slack_mention(self, github_username: str) -> str:
"""Get Slack mention for a GitHub user, or fallback to plain text."""
slack_id = self.user_map.get(github_username)
if slack_id:
return f"<@{slack_id}>"
return f"@{github_username}"
def group_prs_by_person(self, prs: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Group PRs by person based on their status.
Returns dict with three keys:
- awaiting_review: {reviewer_username: [(repo, number, url), ...]}
- in_progress: {assignee_or_author: [(repo, number, url), ...]}
- no_reviewer: [prs with "Awaiting Review" status but no reviewer]
"""
awaiting_review: Dict[str, List[tuple]] = {}
in_progress: Dict[str, List[tuple]] = {}
no_reviewer: List[Dict[str, Any]] = []
for pr in prs:
status = pr.get("_project_fields", {}).get("Status", "")
url = pr.get("url", "")
number = pr.get("number")
repo = pr.get("repository", {}).get("nameWithOwner", "Unknown")
# Get short repo name (without org)
short_repo = repo.split("/")[-1] if "/" in repo else repo
pr_info = (short_repo, number, url)
if status == AWAITING_REVIEW_STATUS:
# Get explicitly requested reviewers
review_requests = pr.get("reviewRequests", {}).get("nodes", [])
reviewers = set()
for rr in review_requests:
reviewer = rr.get("requestedReviewer", {})
if reviewer:
# Could be User or Team
reviewer_name = reviewer.get("login") or reviewer.get("name")
if reviewer_name:
reviewers.add(reviewer_name)
# Remove the PR author from reviewers (they aren't reviewing their own PR)
pr_author = pr.get("author", {}).get("login")
reviewers.discard(pr_author)
if reviewers:
# Add to each reviewer's list
for reviewer in reviewers:
if reviewer not in awaiting_review:
awaiting_review[reviewer] = []
awaiting_review[reviewer].append(pr_info)
else:
# No reviewer assigned - skip dependabot PRs
if pr_author and pr_author.lower() in (
"dependabot",
"dependabot[bot]",
):
continue
no_reviewer.append(pr)
elif status in IN_PROGRESS_STATUSES:
# Get assignee, or fall back to author
assignees = pr.get("assignees", {}).get("nodes", [])
assignee_logins = [
a.get("login") for a in assignees if a and a.get("login")
]
if assignee_logins:
# Add to first assignee's list (primary assignee)
person = assignee_logins[0]
else:
# Fall back to author
person = pr.get("author", {}).get("login", "unknown")
if person not in in_progress:
in_progress[person] = []
in_progress[person].append(pr_info)
return {
"awaiting_review": awaiting_review,
"in_progress": in_progress,
"no_reviewer": no_reviewer,
}
def format_person_centric_message(
self, prs: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
"""Format PRs into a person-centric Slack message with @mentions and personalized links."""
if not prs:
return [
{
"text": "FOC-WG PR Summary: No open PRs matching filters",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "FOC-WG PR Summary",
"emoji": True,
},
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "No open PRs matching the current filters.",
},
},
],
}
]
grouped = self.group_prs_by_person(prs)
blocks = []
# Section: PRs Awaiting Your Review (only users in mapping)
mapped_awaiting = {
u: prs
for u, prs in grouped["awaiting_review"].items()
if u in self.user_map
}
if mapped_awaiting:
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*:mag: PRs Awaiting Your Review*",
},
}
)
# Sort by username alphabetically (case-insensitive)
for username in sorted(mapped_awaiting.keys(), key=str.lower):
pr_list = mapped_awaiting[username]
mention = self._get_slack_mention(username)
view_link = self.build_github_link(username)
# Format: <@USER> repo#123 repo#456 (view all)
pr_links = " ".join(
[f"<{url}|{repo}#{num}>" for repo, num, url in pr_list]
)
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f" • {mention}: {pr_links} (<{view_link}|view all>)",
},
}
)
# Section: PRs In Progress (only users in mapping)
mapped_in_progress = {
u: prs for u, prs in grouped["in_progress"].items() if u in self.user_map
}
if mapped_in_progress:
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*:construction: PRs In Progress*",
},
}
)
# Sort by username alphabetically (case-insensitive)
for username in sorted(mapped_in_progress.keys(), key=str.lower):
pr_list = mapped_in_progress[username]
mention = self._get_slack_mention(username)
view_link = self.build_github_link(username)
# Format: <@USER> repo#123 repo#456 (view all)
pr_links = " ".join(
[f"<{url}|{repo}#{num}>" for repo, num, url in pr_list]
)
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f" • {mention}: {pr_links} (<{view_link}|view all>)",
},
}
)
# Section: PRs Awaiting Review (No Reviewer Assigned)
if grouped["no_reviewer"]:
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*:warning: PRs Awaiting Review (No Reviewer Assigned)*",
},
}
)
for pr in grouped["no_reviewer"]:
repo = pr.get("repository", {}).get("nameWithOwner", "Unknown")
number = pr.get("number")
title = pr.get("title", "Untitled")
url = pr.get("url", "")
author = pr.get("author", {}).get("login", "unknown")
author_mention = self._get_slack_mention(author)
# Truncate title if too long
if len(title) > 60:
title = title[:57] + "..."
blocks.append(
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f" • <{url}|{repo}#{number}> - {title} (authored by {author_mention})",
},
}
)
# Footer with timestamp
now = datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
blocks.append(
{
"type": "context",
"elements": [{"type": "mrkdwn", "text": f"Generated {now}"}],
}
)
return [{"text": f"FOC-WG PR Summary: {len(prs)} open PRs", "blocks": blocks}]
def format_slack_messages(self, prs: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Format PRs into one or more Slack messages with blocks (splits if >50 blocks)."""
SLACK_MAX_BLOCKS = 50
if not prs:
return [
{
"text": "FOC-WG Daily PR Summary: No open PRs matching filters",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "📋 FOC-WG Daily PR Summary",
"emoji": True,
},
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "No open PRs matching the current filters.",
},
},
],
}
]
# Sort by updated date (most recent first)
prs_sorted = sorted(prs, key=lambda x: x.get("updatedAt", ""), reverse=True)
# Group PRs by repository
prs_by_repo: Dict[str, List[Dict]] = {}
for pr in prs_sorted:
repo = pr.get("repository", {}).get("nameWithOwner", "Unknown")
if repo not in prs_by_repo:
prs_by_repo[repo] = []
prs_by_repo[repo].append(pr)
messages = []
current_blocks = []
message_num = 1
# Compact header for first message
header_blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*📋 FOC-WG Daily PR Summary* · {len(prs_sorted)} open PRs · <https://github.com/orgs/FilOzone/projects/14/views/32|View 32>",
},
}
]
# Group PRs by repository first
prs_by_repo: Dict[str, List[Dict]] = {}
for pr in prs_sorted:
repo = pr.get("repository", {}).get("nameWithOwner", "Unknown")
if repo not in prs_by_repo:
prs_by_repo[repo] = []
prs_by_repo[repo].append(pr)
# Estimate if we need to split (now: 1 block per PR + 1 per repo header + header + footer)
estimated_blocks = (
len(header_blocks) + len(prs_sorted) + len(prs_by_repo) + 1
) # +1 for footer
if estimated_blocks > SLACK_MAX_BLOCKS:
# Calculate how many messages we'll need
blocks_per_message = (
SLACK_MAX_BLOCKS - len(header_blocks) - 2
) # Reserve for footer
prs_per_message = blocks_per_message - len(prs_by_repo) # Rough estimate
if prs_per_message < 10:
prs_per_message = 10 # Minimum
current_blocks.extend(header_blocks)
repo_list = list(prs_by_repo.items())
for repo_idx, (repo, repo_prs) in enumerate(repo_list):
# Check if we need to start a new message
# Each PR takes ~1 block (section with fields), repo header takes 1, divider takes 1
estimated_new_blocks = 1 + len(repo_prs) + 1 # repo header + PRs + divider
if (
len(current_blocks) + estimated_new_blocks > SLACK_MAX_BLOCKS - 3
): # Reserve for footer
# Finish current message
now = datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
current_blocks.append(
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": f"Part {message_num} · Generated {now}",
}
],
}
)
messages.append(
{
"text": f"FOC-WG Daily PR Summary (Part {message_num})",
"blocks": current_blocks,
}
)
# Start new message
message_num += 1
current_blocks = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*📋 FOC-WG Daily PR Summary (Part {message_num})* · <https://github.com/orgs/FilOzone/projects/14/views/32|View 32>",
},
}
]
# Compact repo header
current_blocks.append(
{
"type": "section",
"text": {"type": "mrkdwn", "text": f"*{repo}* ({len(repo_prs)})"},
}
)
# PR list for this repo - compact format
for pr_idx, pr in enumerate(repo_prs):
number = pr.get("number")
title = pr.get("title", "Untitled")
url = pr.get("url", "")
author = pr.get("author", {}).get("login", "unknown")
# Get assignees
assignees = pr.get("assignees", {}).get("nodes", [])
assignee_logins = [a.get("login") for a in assignees if a]
assignees_str = (
", ".join(assignee_logins) if assignee_logins else "None"
)
# Truncate if too long (Slack field limit is 2000 chars)
if len(assignees_str) > 100:
assignees_str = assignees_str[:97] + "..."
# Get requested reviewers
review_requests = pr.get("reviewRequests", {}).get("nodes", [])
reviewers = []
for rr in review_requests:
reviewer = rr.get("requestedReviewer", {})
if reviewer:
# Could be User or Team
reviewer_name = reviewer.get("login") or reviewer.get("name")
if reviewer_name:
reviewers.append(reviewer_name)
reviewers_str = ", ".join(reviewers) if reviewers else "None"
# Truncate if too long
if len(reviewers_str) > 100:
reviewers_str = reviewers_str[:97] + "..."
# Dates - parse ISO format dates
created_at = pr.get("createdAt", "")
try:
if created_at:
created_date = datetime.fromisoformat(
created_at.replace("Z", "+00:00")
).strftime("%Y-%m-%d")
else:
created_date = "Unknown"
except (ValueError, AttributeError):
created_date = "Unknown"
# Project fields
project_fields = pr.get("_project_fields", {})
status = project_fields.get("Status", "") or "None"
cycle = (
project_fields.get("Cycle", "")
or project_fields.get("Iteration", "")
or "None"
)
# Truncate if too long
if len(status) > 50:
status = status[:47] + "..."
if len(cycle) > 50:
cycle = cycle[:47] + "..."
# Truncate title if too long
display_title = title
if len(display_title) > 70:
display_title = display_title[:67] + "..."
# Build compact single line: PR link, author, assignee (if different), reviewer, created date, status
pr_line = f"• <{url}|#{number}> {display_title}"
# Add author
pr_line += f" · _{author}_"
# Add assignee only if different from author
if assignees_str != "None" and assignees_str != author:
# Truncate assignees if multiple
if len(assignees_str) > 30:
assignees_str = assignees_str[:27] + "..."
pr_line += f" → _{assignees_str}_"
# Add reviewer if exists
if reviewers_str != "None":
# Truncate reviewers if multiple
if len(reviewers_str) > 30:
reviewers_str = reviewers_str[:27] + "..."
pr_line += f" 👀 _{reviewers_str}_"
# Add created date
pr_line += f" · {created_date}"
# Add status (truncate if too long)
if status and status != "None":
status_short = status[:20] if len(status) > 20 else status
pr_line += f" · {status_short}"
current_blocks.append(
{"type": "section", "text": {"type": "mrkdwn", "text": pr_line}}
)
# No divider between repos - repo headers provide enough separation
# Compact footer with timestamp
now = datetime.utcnow().strftime("%Y-%m-%d %H:%M UTC")
footer_text = f"Generated {now}"
current_blocks.append(
{"type": "context", "elements": [{"type": "mrkdwn", "text": footer_text}]}
)
messages.append(
{
"text": f"FOC-WG Daily PR Summary: {len(prs_sorted)} open PRs",
"blocks": current_blocks,
}
)
# Update footers with part indicators only if we actually have multiple messages
if len(messages) > 1:
for i, msg in enumerate(messages, 1):
# Update footer in each message
for block in reversed(msg["blocks"]):
if block.get("type") == "context":
elements = block.get("elements", [])
if elements and "Generated" in str(elements[0].get("text", "")):
elements[0]["text"] = (
f"Part {i}/{len(messages)} · {elements[0]['text']}"
)
break
# Update header if it's a continuation message
if i > 1:
for block in msg["blocks"]:
if block.get("type") == "section" and "Part" in str(
block.get("text", {}).get("text", "")
):
block["text"]["text"] = block["text"]["text"].replace(
f"Part {i})", f"Part {i}/{len(messages)})"
)
break
return messages
def post_to_slack(self, message: Dict[str, Any]) -> bool:
"""Post message to Slack webhook."""
if not self.slack_webhook_url:
raise ValueError("Slack webhook URL not configured")
response = requests.post(self.slack_webhook_url, json=message, timeout=30)
if response.status_code != 200:
print(f"Slack API error: {response.status_code} - {response.text}")
return False
return True
def run(self, dry_run: bool = False) -> bool:
"""Execute the full workflow."""
try:
# Fetch items from project
print("Fetching items from FilOzone Project 14...")
items = self.fetch_project_items()
# Apply filters
print("Applying view 32 filters...")
filtered_prs = self.filter_items(items)
# Format messages using person-centric format
print("Formatting Slack message(s)...")
messages = self.format_person_centric_message(filtered_prs)
if dry_run:
print(
f"\n=== DRY RUN - {len(messages)} message(s) that would be sent ==="
)
for i, message in enumerate(messages, 1):
print(f"\n--- Message {i} of {len(messages)} ---")
print(json.dumps(message, indent=2))
print("\n=== PR Summary ===")
for pr in filtered_prs:
repo = pr.get("repository", {}).get("nameWithOwner", "Unknown")
number = pr.get("number")
title = pr.get("title", "Untitled")
print(f" {repo}#{number}: {title}")
return True
# Post to Slack (may be multiple messages)
print(f"Posting {len(messages)} message(s) to Slack...")
success = True
for i, message in enumerate(messages, 1):
if len(messages) > 1:
print(f" Posting message {i} of {len(messages)}...")
if not self.post_to_slack(message):
success = False
break
# Small delay between messages to avoid rate limiting
if i < len(messages):
import time
time.sleep(1)
if success:
print("✅ Successfully posted to Slack!")
else:
print("❌ Failed to post to Slack")
return success
except Exception as e:
print(f"❌ Error: {e}")
return False
def main():
parser = argparse.ArgumentParser(
description="Fetch PRs from FilOzone Project 14 and post to Slack",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Dry run (fetch PRs and show what would be posted)
GITHUB_TOKEN=xxx python foc_wg_pr_notifier.py --dry-run
# Post to Slack
GITHUB_TOKEN=xxx SLACK_WEBHOOK_URL=yyy python foc_wg_pr_notifier.py
# Use command-line arguments instead of env vars
python foc_wg_pr_notifier.py --token xxx --webhook yyy
# Use a custom user mapping file
python foc_wg_pr_notifier.py --user-map /path/to/users.json --dry-run
""",
)
parser.add_argument(
"--token", help="GitHub personal access token (or set GITHUB_TOKEN env var)"
)
parser.add_argument(
"--webhook", help="Slack webhook URL (or set SLACK_WEBHOOK_URL env var)"
)
parser.add_argument(
"--user-map",
help="Path to JSON file mapping GitHub usernames to Slack user IDs (default: gh_slack_users.json in script directory)",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Fetch PRs and show message without posting to Slack",
)
args = parser.parse_args()
# Get credentials
github_token = args.token or os.getenv("GITHUB_TOKEN")
slack_webhook = args.webhook or os.getenv("SLACK_WEBHOOK_URL")
if not github_token:
print(
"Error: GitHub token required. Set GITHUB_TOKEN environment variable or use --token flag."
)
sys.exit(1)
if not args.dry_run and not slack_webhook:
print(
"Error: Slack webhook URL required. Set SLACK_WEBHOOK_URL environment variable or use --webhook flag."
)
print("Tip: Use --dry-run to test without posting to Slack.")
sys.exit(1)
notifier = FOCWGNotifier(github_token, slack_webhook, args.user_map)
success = notifier.run(dry_run=args.dry_run)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()