|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | +import re |
| 10 | +import urllib.error |
| 11 | +import urllib.parse |
| 12 | +import urllib.request |
| 13 | +from pathlib import Path |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +MARKER = "<!-- librecode:first-merged-pr-comment -->" |
| 17 | +PLACEHOLDER = re.compile(r"\{([a-z][a-z0-9_]*)(?:\|([a-z][a-z0-9_]*))?\}") |
| 18 | +ALLOWED_FILTERS = {"urlencode"} |
| 19 | + |
| 20 | + |
| 21 | +class ActionError(RuntimeError): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +def render_template(template: str, context: dict[str, str]) -> str: |
| 26 | + if not template.strip(): |
| 27 | + raise ActionError("message template is empty") |
| 28 | + |
| 29 | + def replace(match: re.Match[str]) -> str: |
| 30 | + name, filter_name = match.groups() |
| 31 | + if name not in context: |
| 32 | + raise ActionError(f"unknown placeholder: {name}") |
| 33 | + value = context[name] |
| 34 | + if filter_name is None: |
| 35 | + return value |
| 36 | + if filter_name not in ALLOWED_FILTERS: |
| 37 | + raise ActionError(f"unknown placeholder filter: {filter_name}") |
| 38 | + if filter_name == "urlencode": |
| 39 | + return urllib.parse.quote(value, safe="") |
| 40 | + raise AssertionError(filter_name) |
| 41 | + |
| 42 | + return PLACEHOLDER.sub(replace, template) |
| 43 | + |
| 44 | + |
| 45 | +def build_context( |
| 46 | + *, |
| 47 | + pr: dict[str, Any], |
| 48 | + repository: str, |
| 49 | + server_url: str, |
| 50 | + api_url: str, |
| 51 | +) -> dict[str, str]: |
| 52 | + owner, repository_name = repository.split("/", 1) |
| 53 | + login = str(pr["user"]["login"]) |
| 54 | + number = str(pr["number"]) |
| 55 | + return { |
| 56 | + "server_url": server_url.rstrip("/"), |
| 57 | + "api_url": api_url.rstrip("/"), |
| 58 | + "repository": repository, |
| 59 | + "repository_owner": owner, |
| 60 | + "repository_name": repository_name, |
| 61 | + "repository_url": f"{server_url.rstrip('/')}/{repository}", |
| 62 | + "pull_request_number": number, |
| 63 | + "pull_request_url": str( |
| 64 | + pr.get("html_url") |
| 65 | + or f"{server_url.rstrip('/')}/{repository}/pull/{number}" |
| 66 | + ), |
| 67 | + "contributor_login": login, |
| 68 | + "contributor_mention": f"@{login}", |
| 69 | + "contributor_url": f"{server_url.rstrip('/')}/{login}", |
| 70 | + "merge_commit_sha": str(pr.get("merge_commit_sha") or ""), |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +def api_request( |
| 75 | + method: str, |
| 76 | + url: str, |
| 77 | + token: str, |
| 78 | + payload: dict[str, Any] | None = None, |
| 79 | +) -> Any: |
| 80 | + data = None if payload is None else json.dumps(payload).encode("utf-8") |
| 81 | + request = urllib.request.Request( |
| 82 | + url, |
| 83 | + data=data, |
| 84 | + method=method, |
| 85 | + headers={ |
| 86 | + "Accept": "application/vnd.github+json", |
| 87 | + "Authorization": f"Bearer {token}", |
| 88 | + "Content-Type": "application/json", |
| 89 | + "X-GitHub-Api-Version": "2022-11-28", |
| 90 | + }, |
| 91 | + ) |
| 92 | + try: |
| 93 | + with urllib.request.urlopen(request, timeout=30) as response: |
| 94 | + body = response.read().decode("utf-8") |
| 95 | + except urllib.error.HTTPError as error: |
| 96 | + body = error.read().decode("utf-8", errors="replace") |
| 97 | + raise ActionError(f"GitHub API request failed ({error.code}): {body}") from error |
| 98 | + return json.loads(body) if body else None |
| 99 | + |
| 100 | + |
| 101 | +def pull_request_from_event(event_path: str) -> dict[str, Any] | None: |
| 102 | + if not event_path: |
| 103 | + return None |
| 104 | + payload = json.loads(Path(event_path).read_text(encoding="utf-8")) |
| 105 | + pr = payload.get("pull_request") |
| 106 | + return pr if isinstance(pr, dict) else None |
| 107 | + |
| 108 | + |
| 109 | +def write_output(name: str, value: str) -> None: |
| 110 | + path = os.environ.get("GITHUB_OUTPUT") |
| 111 | + if not path: |
| 112 | + return |
| 113 | + with Path(path).open("a", encoding="utf-8") as handle: |
| 114 | + handle.write(f"{name}={value}\n") |
| 115 | + |
| 116 | + |
| 117 | +def first_merged_query(repository: str, login: str, closed_at: str) -> str: |
| 118 | + return " ".join( |
| 119 | + ( |
| 120 | + f"repo:{repository}", |
| 121 | + "is:pr", |
| 122 | + "is:merged", |
| 123 | + f"author:{login}", |
| 124 | + f"closed:<={closed_at}", |
| 125 | + ) |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def main() -> int: |
| 130 | + token = os.environ.get("FIRST_MERGED_PR_GITHUB_TOKEN", "") |
| 131 | + template = os.environ.get("FIRST_MERGED_PR_MESSAGE_TEMPLATE", "") |
| 132 | + manual_number = os.environ.get("FIRST_MERGED_PR_NUMBER", "").strip() |
| 133 | + repository = os.environ.get("GITHUB_REPOSITORY", "") |
| 134 | + api_url = os.environ.get("GITHUB_API_URL", "https://api.github.com").rstrip("/") |
| 135 | + server_url = os.environ.get("GITHUB_SERVER_URL", "https://github.com").rstrip("/") |
| 136 | + |
| 137 | + if not token: |
| 138 | + raise ActionError("github token is required") |
| 139 | + if "/" not in repository: |
| 140 | + raise ActionError("GITHUB_REPOSITORY must be in owner/name form") |
| 141 | + |
| 142 | + pr = pull_request_from_event(os.environ.get("GITHUB_EVENT_PATH", "")) |
| 143 | + if pr is None: |
| 144 | + if not manual_number.isdigit() or int(manual_number) <= 0: |
| 145 | + raise ActionError("a valid pull-request-number is required for a manual run") |
| 146 | + owner, repo = repository.split("/", 1) |
| 147 | + pr = api_request( |
| 148 | + "GET", |
| 149 | + f"{api_url}/repos/{owner}/{repo}/pulls/{int(manual_number)}", |
| 150 | + token, |
| 151 | + ) |
| 152 | + |
| 153 | + write_output("contributor-login", str(pr["user"]["login"])) |
| 154 | + write_output("pull-request-number", str(pr["number"])) |
| 155 | + |
| 156 | + if not pr.get("merged") or pr.get("user", {}).get("type") == "Bot": |
| 157 | + write_output("is-first-merged", "false") |
| 158 | + write_output("comment-created", "false") |
| 159 | + print("Pull request is not a merged human contribution; skipping.") |
| 160 | + return 0 |
| 161 | + |
| 162 | + login = str(pr["user"]["login"]) |
| 163 | + closed_at = str(pr["closed_at"]) |
| 164 | + query = first_merged_query(repository, login, closed_at) |
| 165 | + encoded_query = urllib.parse.urlencode({"q": query, "per_page": 2}) |
| 166 | + search = api_request("GET", f"{api_url}/search/issues?{encoded_query}", token) |
| 167 | + total_count = int(search["total_count"]) |
| 168 | + |
| 169 | + if total_count != 1: |
| 170 | + write_output("is-first-merged", "false") |
| 171 | + write_output("comment-created", "false") |
| 172 | + print( |
| 173 | + f"PR #{pr['number']} is not the contributor's first merged pull request; " |
| 174 | + f"found {total_count} merged pull requests up to this one." |
| 175 | + ) |
| 176 | + return 0 |
| 177 | + |
| 178 | + write_output("is-first-merged", "true") |
| 179 | + |
| 180 | + owner, repo = repository.split("/", 1) |
| 181 | + comments = api_request( |
| 182 | + "GET", |
| 183 | + f"{api_url}/repos/{owner}/{repo}/issues/{pr['number']}/comments?per_page=100", |
| 184 | + token, |
| 185 | + ) |
| 186 | + if any(MARKER in str(comment.get("body") or "") for comment in comments): |
| 187 | + write_output("comment-created", "false") |
| 188 | + print(f"PR #{pr['number']} already has a first-merged comment; skipping.") |
| 189 | + return 0 |
| 190 | + |
| 191 | + context = build_context( |
| 192 | + pr=pr, |
| 193 | + repository=repository, |
| 194 | + server_url=server_url, |
| 195 | + api_url=api_url, |
| 196 | + ) |
| 197 | + message = render_template(template, context).strip() |
| 198 | + api_request( |
| 199 | + "POST", |
| 200 | + f"{api_url}/repos/{owner}/{repo}/issues/{pr['number']}/comments", |
| 201 | + token, |
| 202 | + {"body": f"{MARKER}\n{message}"}, |
| 203 | + ) |
| 204 | + write_output("comment-created", "true") |
| 205 | + print(f"Created first-merged contribution comment on PR #{pr['number']}.") |
| 206 | + return 0 |
| 207 | + |
| 208 | + |
| 209 | +if __name__ == "__main__": |
| 210 | + try: |
| 211 | + raise SystemExit(main()) |
| 212 | + except (ActionError, KeyError, OSError, ValueError, json.JSONDecodeError) as error: |
| 213 | + print(f"::error::{error}") |
| 214 | + raise SystemExit(1) from error |
0 commit comments