|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Compile standup responses into a GitHub Discussion and close the issue.""" |
| 3 | + |
| 4 | +import os |
| 5 | +from datetime import datetime, timezone, timedelta |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +REPO = os.environ["GITHUB_REPOSITORY"] |
| 10 | +TOKEN = os.environ["STANDUP_TOKEN"] |
| 11 | +CATEGORY_NODE_ID = os.environ["DISCUSSION_CATEGORY_NODE_ID"] |
| 12 | +API = "https://api.github.com" |
| 13 | +GRAPHQL = "https://api.github.com/graphql" |
| 14 | +HEADERS = { |
| 15 | + "Authorization": f"token {TOKEN}", |
| 16 | + "Accept": "application/vnd.github+json", |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +def find_standup_issue(): |
| 21 | + """Find the most recent open standup-input issue from the last 7 days.""" |
| 22 | + since = (datetime.now(timezone.utc) - timedelta(days=7)).isoformat() |
| 23 | + resp = requests.get( |
| 24 | + f"{API}/repos/{REPO}/issues", |
| 25 | + headers=HEADERS, |
| 26 | + params={ |
| 27 | + "labels": "standup-input", |
| 28 | + "state": "open", |
| 29 | + "since": since, |
| 30 | + "sort": "created", |
| 31 | + "direction": "desc", |
| 32 | + "per_page": 1, |
| 33 | + }, |
| 34 | + ) |
| 35 | + resp.raise_for_status() |
| 36 | + issues = resp.json() |
| 37 | + if not issues: |
| 38 | + print("No standup-input issue found in the last 7 days.") |
| 39 | + return None |
| 40 | + return issues[0] |
| 41 | + |
| 42 | + |
| 43 | +def fetch_comments(issue_number): |
| 44 | + """Fetch all comments on an issue.""" |
| 45 | + comments = [] |
| 46 | + page = 1 |
| 47 | + while True: |
| 48 | + resp = requests.get( |
| 49 | + f"{API}/repos/{REPO}/issues/{issue_number}/comments", |
| 50 | + headers=HEADERS, |
| 51 | + params={"per_page": 100, "page": page}, |
| 52 | + ) |
| 53 | + resp.raise_for_status() |
| 54 | + batch = resp.json() |
| 55 | + if not batch: |
| 56 | + break |
| 57 | + comments.extend(batch) |
| 58 | + page += 1 |
| 59 | + return comments |
| 60 | + |
| 61 | + |
| 62 | +def get_repo_node_id(): |
| 63 | + """Get the repository node ID for the GraphQL mutation.""" |
| 64 | + resp = requests.get(f"{API}/repos/{REPO}", headers=HEADERS) |
| 65 | + resp.raise_for_status() |
| 66 | + return resp.json()["node_id"] |
| 67 | + |
| 68 | + |
| 69 | +def create_discussion(title, body, repo_node_id): |
| 70 | + """Create a GitHub Discussion via GraphQL.""" |
| 71 | + mutation = """ |
| 72 | + mutation($repoId: ID!, $categoryId: ID!, $title: String!, $body: String!) { |
| 73 | + createDiscussion(input: { |
| 74 | + repositoryId: $repoId, |
| 75 | + categoryId: $categoryId, |
| 76 | + title: $title, |
| 77 | + body: $body |
| 78 | + }) { |
| 79 | + discussion { |
| 80 | + url |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + """ |
| 85 | + resp = requests.post( |
| 86 | + GRAPHQL, |
| 87 | + headers=HEADERS, |
| 88 | + json={ |
| 89 | + "query": mutation, |
| 90 | + "variables": { |
| 91 | + "repoId": repo_node_id, |
| 92 | + "categoryId": CATEGORY_NODE_ID, |
| 93 | + "title": title, |
| 94 | + "body": body, |
| 95 | + }, |
| 96 | + }, |
| 97 | + ) |
| 98 | + resp.raise_for_status() |
| 99 | + data = resp.json() |
| 100 | + if "errors" in data: |
| 101 | + raise RuntimeError(f"GraphQL errors: {data['errors']}") |
| 102 | + return data["data"]["createDiscussion"]["discussion"]["url"] |
| 103 | + |
| 104 | + |
| 105 | +def close_issue(issue_number, discussion_url): |
| 106 | + """Close the standup issue with a link to the compiled discussion.""" |
| 107 | + requests.post( |
| 108 | + f"{API}/repos/{REPO}/issues/{issue_number}/comments", |
| 109 | + headers=HEADERS, |
| 110 | + json={"body": f"Compiled into discussion: {discussion_url}"}, |
| 111 | + ) |
| 112 | + requests.patch( |
| 113 | + f"{API}/repos/{REPO}/issues/{issue_number}", |
| 114 | + headers=HEADERS, |
| 115 | + json={"state": "closed"}, |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +def main(): |
| 120 | + issue = find_standup_issue() |
| 121 | + if not issue: |
| 122 | + return |
| 123 | + |
| 124 | + issue_number = issue["number"] |
| 125 | + # Extract the week label from the issue title |
| 126 | + title_suffix = issue["title"].removeprefix("Standup Input: ") |
| 127 | + week_label = title_suffix or datetime.now(timezone.utc).strftime("Week of %Y-%m-%d") |
| 128 | + |
| 129 | + comments = fetch_comments(issue_number) |
| 130 | + |
| 131 | + # Build sections per contributor |
| 132 | + sections = [] |
| 133 | + for comment in comments: |
| 134 | + user = comment["user"]["login"] |
| 135 | + if comment["user"]["type"] == "Bot": |
| 136 | + continue |
| 137 | + body = comment["body"].strip() |
| 138 | + sections.append(f"### @{user}\n{body}") |
| 139 | + |
| 140 | + updates = "\n\n".join(sections) if sections else "_No responses._" |
| 141 | + |
| 142 | + discussion_title = f"Weekly Check-in: {week_label}" |
| 143 | + discussion_body = updates |
| 144 | + |
| 145 | + repo_node_id = get_repo_node_id() |
| 146 | + discussion_url = create_discussion(discussion_title, discussion_body, repo_node_id) |
| 147 | + print(f"Created discussion: {discussion_url}") |
| 148 | + |
| 149 | + close_issue(issue_number, discussion_url) |
| 150 | + print(f"Closed issue #{issue_number}") |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + main() |
0 commit comments