|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import json |
| 5 | +import urllib.request |
| 6 | +import urllib.error |
| 7 | + |
| 8 | + |
| 9 | +def fetch_github_issues(): |
| 10 | + """Fetch GitHub issues from the repository""" |
| 11 | + repo = "lemonade-sdk/halo_website" |
| 12 | + token = os.environ.get("GITHUB_TOKEN", "") |
| 13 | + |
| 14 | + headers = { |
| 15 | + "Accept": "application/vnd.github.v3+json", |
| 16 | + "User-Agent": "Halo-Website-Fetch", |
| 17 | + } |
| 18 | + |
| 19 | + if token: |
| 20 | + headers["Authorization"] = f"Bearer {token}" |
| 21 | + |
| 22 | + issues = [] |
| 23 | + |
| 24 | + # Try to fetch by milestone first |
| 25 | + try: |
| 26 | + milestones_url = f"https://api.github.com/repos/{repo}/milestones" |
| 27 | + req = urllib.request.Request(milestones_url, headers=headers) |
| 28 | + |
| 29 | + with urllib.request.urlopen(req) as response: |
| 30 | + milestones = json.loads(response.read()) |
| 31 | + |
| 32 | + playbooks_milestone = next( |
| 33 | + (m for m in milestones if m["title"].lower() == "playbooks"), None |
| 34 | + ) |
| 35 | + |
| 36 | + if playbooks_milestone: |
| 37 | + issues_url = f"https://api.github.com/repos/{repo}/issues?milestone={playbooks_milestone['number']}&state=all&per_page=100" |
| 38 | + req = urllib.request.Request(issues_url, headers=headers) |
| 39 | + |
| 40 | + with urllib.request.urlopen(req) as response: |
| 41 | + issues = json.loads(response.read()) |
| 42 | + except Exception as e: |
| 43 | + print(f"Milestone fetch failed: {e}", file=sys.stderr) |
| 44 | + |
| 45 | + # Fallback: fetch issues with "playbooks" label |
| 46 | + if not issues: |
| 47 | + try: |
| 48 | + issues_url = f"https://api.github.com/repos/{repo}/issues?labels=playbooks&state=all&per_page=100" |
| 49 | + req = urllib.request.Request(issues_url, headers=headers) |
| 50 | + |
| 51 | + with urllib.request.urlopen(req) as response: |
| 52 | + issues = json.loads(response.read()) |
| 53 | + except Exception as e: |
| 54 | + print(f"Label fetch failed: {e}", file=sys.stderr) |
| 55 | + |
| 56 | + # If still no issues, try fetching all issues and filter |
| 57 | + if not issues: |
| 58 | + try: |
| 59 | + all_issues_url = ( |
| 60 | + f"https://api.github.com/repos/{repo}/issues?state=all&per_page=100" |
| 61 | + ) |
| 62 | + req = urllib.request.Request(all_issues_url, headers=headers) |
| 63 | + |
| 64 | + with urllib.request.urlopen(req) as response: |
| 65 | + all_issues = json.loads(response.read()) |
| 66 | + |
| 67 | + # Filter for issues that have relevant labels |
| 68 | + issues = [ |
| 69 | + issue |
| 70 | + for issue in all_issues |
| 71 | + if any( |
| 72 | + label["name"].startswith(prefix) |
| 73 | + for label in issue.get("labels", []) |
| 74 | + for prefix in ["track::", "os::", "app::", "framework::", "model::"] |
| 75 | + ) |
| 76 | + ] |
| 77 | + except Exception as e: |
| 78 | + print(f"All issues fetch failed: {e}", file=sys.stderr) |
| 79 | + |
| 80 | + return issues |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + try: |
| 85 | + issues = fetch_github_issues() |
| 86 | + result = {"issues": issues} |
| 87 | + |
| 88 | + # Write to file |
| 89 | + output_file = "github_issues_cache.json" |
| 90 | + with open(output_file, "w", encoding="utf-8") as f: |
| 91 | + json.dump(result, f, indent=2) |
| 92 | + |
| 93 | + print(f"Successfully fetched {len(issues)} issues and saved to {output_file}") |
| 94 | + except Exception as e: |
| 95 | + error_result = {"error": str(e), "issues": []} |
| 96 | + with open("github_issues_cache.json", "w", encoding="utf-8") as f: |
| 97 | + json.dump(error_result, f, indent=2) |
| 98 | + print(f"Error: {e}", file=sys.stderr) |
| 99 | + sys.exit(1) |
0 commit comments