|
| 1 | +from os import getenv |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | + |
| 7 | +def get_github_prs(token: str, owner: str, repo: str, label: str = "", state: str = "all") -> list[dict]: |
| 8 | + """ |
| 9 | + Fetches pull requests from a GitHub repository that match a given label and state. |
| 10 | +
|
| 11 | + Args: |
| 12 | + token (str): GitHub token. |
| 13 | + owner (str): The owner of the repository. |
| 14 | + repo (str): The name of the repository. |
| 15 | + label (str): The label name. Filter is not applied when empty string. |
| 16 | + state (str): State of PR, e.g. open, closed, all |
| 17 | +
|
| 18 | + Returns: |
| 19 | + list: A list of dictionaries, where each dictionary represents a pull request. |
| 20 | + Returns an empty list if no PRs are found or an error occurs. |
| 21 | + """ |
| 22 | + headers = { |
| 23 | + "Authorization": f"token {token}", |
| 24 | + "Accept": "application/vnd.github.v3+json", |
| 25 | + } |
| 26 | + |
| 27 | + # This endpoint allows filtering by label(and milestone). A PR in GH's perspective is a type of issue. |
| 28 | + prs_url = f"https://api.github.com/repos/{owner}/{repo}/issues" |
| 29 | + params = { |
| 30 | + "state": state, |
| 31 | + "labels": label, |
| 32 | + "per_page": 100, |
| 33 | + } |
| 34 | + |
| 35 | + all_prs = [] |
| 36 | + page = 1 |
| 37 | + while True: |
| 38 | + try: |
| 39 | + params["page"] = page |
| 40 | + response = requests.get(prs_url, headers=headers, params=params) |
| 41 | + response.raise_for_status() # Raise an exception for HTTP errors |
| 42 | + prs = response.json() |
| 43 | + |
| 44 | + if not prs: |
| 45 | + break # No more PRs to fetch |
| 46 | + |
| 47 | + # Check for pr key since we are using issues endpoint instead. |
| 48 | + all_prs.extend([item for item in prs if "pull_request" in item]) |
| 49 | + page += 1 |
| 50 | + |
| 51 | + except requests.exceptions.RequestException as e: |
| 52 | + print(f"Error fetching pull requests: {e}") |
| 53 | + exit(1) |
| 54 | + |
| 55 | + return all_prs |
| 56 | + |
| 57 | + |
| 58 | +def get_prs( |
| 59 | + pull_request_items: list[dict], label: str = "", state: str = "all", milestone_title: Optional[str] = None |
| 60 | +) -> list[dict]: |
| 61 | + """ |
| 62 | + Returns a list of pull requests after applying the label and state filters. |
| 63 | +
|
| 64 | + Args: |
| 65 | + pull_request_items (list[dict]): List of PR items. |
| 66 | + label (str): The label name. Filter is not applied when empty string. |
| 67 | + state (str): State of PR, e.g. open, closed, all |
| 68 | + milestone_title (Optional[str]): The milestone title to filter by. This is the milestone number you created |
| 69 | + in GitHub, e.g. '1.20.0'. If None, no milestone filtering is applied. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + list: A list of dictionaries, where each dictionary represents a pull request. |
| 73 | + Returns an empty list if no PRs are found. |
| 74 | + """ |
| 75 | + pr_list = [] |
| 76 | + count = 0 |
| 77 | + for pr in pull_request_items: |
| 78 | + if state not in [pr["state"], "all"]: |
| 79 | + continue |
| 80 | + |
| 81 | + if label and not [item for item in pr["labels"] if item["name"] == label]: |
| 82 | + continue |
| 83 | + |
| 84 | + if milestone_title: |
| 85 | + if pr["milestone"] is None or pr["milestone"]["title"] != milestone_title: |
| 86 | + continue |
| 87 | + |
| 88 | + pr_list.append(pr) |
| 89 | + count += 1 |
| 90 | + |
| 91 | + print( |
| 92 | + f"Found {count} PRs with {label if label else 'no filter on'} label, state as {state}, and milestone {milestone_title if milestone_title else "any"}" |
| 93 | + ) |
| 94 | + |
| 95 | + return pr_list |
| 96 | + |
| 97 | + |
| 98 | +def get_prs_assignees(pull_request_items: list[dict]) -> list[str]: |
| 99 | + """ |
| 100 | + Returns a list of pull request assignees, excludes jjw24. |
| 101 | +
|
| 102 | + Args: |
| 103 | + pull_request_items (list[dict]): List of PR items to get the assignees from. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + list: A list of strs, where each string is an assignee name. List is not distinct, so can contain |
| 107 | + duplicate names. |
| 108 | + Returns an empty list if none are found. |
| 109 | + """ |
| 110 | + assignee_list = [] |
| 111 | + for pr in pull_request_items: |
| 112 | + [assignee_list.append(assignee["login"]) for assignee in pr["assignees"] if assignee["login"] != "jjw24"] |
| 113 | + |
| 114 | + print(f"Found {len(assignee_list)} assignees") |
| 115 | + |
| 116 | + return assignee_list |
| 117 | + |
| 118 | + |
| 119 | +def get_pr_descriptions(pull_request_items: list[dict]) -> str: |
| 120 | + """ |
| 121 | + Returns the concatenated string of pr title and number in the format of |
| 122 | + '- PR title 1 #3651 |
| 123 | + - PR title 2 #3652 |
| 124 | + - PR title 3 #3653 |
| 125 | + ' |
| 126 | +
|
| 127 | + Args: |
| 128 | + pull_request_items (list[dict]): List of PR items. |
| 129 | +
|
| 130 | + Returns: |
| 131 | + str: a string of PR titles and numbers |
| 132 | + """ |
| 133 | + description_content = "" |
| 134 | + for pr in pull_request_items: |
| 135 | + description_content += f"- {pr['title']} #{pr['number']}\n" |
| 136 | + |
| 137 | + return description_content |
| 138 | + |
| 139 | + |
| 140 | +def update_pull_request_description(token: str, owner: str, repo: str, pr_number: int, new_description: str) -> None: |
| 141 | + """ |
| 142 | + Updates the description (body) of a GitHub Pull Request. |
| 143 | +
|
| 144 | + Args: |
| 145 | + token (str): Token. |
| 146 | + owner (str): The owner of the repository. |
| 147 | + repo (str): The name of the repository. |
| 148 | + pr_number (int): The number of the pull request to update. |
| 149 | + new_description (str): The new content for the PR's description. |
| 150 | +
|
| 151 | + Returns: |
| 152 | + dict or None: The updated PR object (as a dictionary) if successful, |
| 153 | + None otherwise. |
| 154 | + """ |
| 155 | + headers = { |
| 156 | + "Authorization": f"token {token}", |
| 157 | + "Accept": "application/vnd.github.v3+json", |
| 158 | + "Content-Type": "application/json", |
| 159 | + } |
| 160 | + |
| 161 | + url = f"https://api.github.com/repos/{owner}/{repo}/pulls/{pr_number}" |
| 162 | + |
| 163 | + payload = {"body": new_description} |
| 164 | + |
| 165 | + print(f"Attempting to update PR #{pr_number} in {owner}/{repo}...") |
| 166 | + print(f"URL: {url}") |
| 167 | + |
| 168 | + try: |
| 169 | + response = None |
| 170 | + response = requests.patch(url, headers=headers, json=payload) |
| 171 | + response.raise_for_status() |
| 172 | + |
| 173 | + print(f"Successfully updated PR #{pr_number}.") |
| 174 | + |
| 175 | + except requests.exceptions.RequestException as e: |
| 176 | + print(f"Error updating pull request #{pr_number}: {e}") |
| 177 | + if response is not None: |
| 178 | + print(f"Response status code: {response.status_code}") |
| 179 | + print(f"Response text: {response.text}") |
| 180 | + exit(1) |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + github_token = getenv("GITHUB_TOKEN") |
| 185 | + |
| 186 | + if not github_token: |
| 187 | + print("Error: GITHUB_TOKEN environment variable not set.") |
| 188 | + exit(1) |
| 189 | + |
| 190 | + repository_owner = "flow-launcher" |
| 191 | + repository_name = "flow.launcher" |
| 192 | + state = "all" |
| 193 | + |
| 194 | + print(f"Fetching {state} PRs for {repository_owner}/{repository_name} ...") |
| 195 | + |
| 196 | + # First, get all PRs to find the release PR and determine the milestone |
| 197 | + all_pull_requests = get_github_prs(github_token, repository_owner, repository_name) |
| 198 | + |
| 199 | + if not all_pull_requests: |
| 200 | + print("No pull requests found") |
| 201 | + exit(1) |
| 202 | + |
| 203 | + print(f"\nFound total of {len(all_pull_requests)} pull requests") |
| 204 | + |
| 205 | + release_pr = get_prs(all_pull_requests, "release", "open") |
| 206 | + |
| 207 | + if len(release_pr) != 1: |
| 208 | + print(f"Unable to find the exact release PR. Returned result: {release_pr}") |
| 209 | + exit(1) |
| 210 | + |
| 211 | + print(f"Found release PR: {release_pr[0]['title']}") |
| 212 | + |
| 213 | + release_milestone_title = release_pr[0].get("milestone", {}).get("title", None) |
| 214 | + |
| 215 | + if not release_milestone_title: |
| 216 | + print("Release PR does not have a milestone assigned.") |
| 217 | + exit(1) |
| 218 | + |
| 219 | + print(f"Using milestone number: {release_milestone_title}") |
| 220 | + |
| 221 | + enhancement_prs = get_prs(all_pull_requests, "enhancement", "closed", release_milestone_title) |
| 222 | + bug_fix_prs = get_prs(all_pull_requests, "bug", "closed", release_milestone_title) |
| 223 | + |
| 224 | + if len(enhancement_prs) == 0 and len(bug_fix_prs) == 0: |
| 225 | + print(f"No PRs with {release_milestone_title} milestone were found") |
| 226 | + |
| 227 | + description_content = "# Release notes\n" |
| 228 | + description_content += f"## Features\n{get_pr_descriptions(enhancement_prs)}" if enhancement_prs else "" |
| 229 | + description_content += f"## Bug fixes\n{get_pr_descriptions(bug_fix_prs)}" if bug_fix_prs else "" |
| 230 | + |
| 231 | + assignees = list(set(get_prs_assignees(enhancement_prs) + get_prs_assignees(bug_fix_prs))) |
| 232 | + assignees.sort(key=str.lower) |
| 233 | + |
| 234 | + description_content += f"### Authors:\n{', '.join(assignees)}" |
| 235 | + |
| 236 | + update_pull_request_description( |
| 237 | + github_token, repository_owner, repository_name, release_pr[0]["number"], description_content |
| 238 | + ) |
| 239 | + |
| 240 | + print(f"PR content updated to:\n{description_content}") |
0 commit comments