Commit 4f2c243b96e0
Changed files (4)
dots
config
raffi
scripts
github
dots/config/raffi/scripts/github/pr.py
@@ -0,0 +1,762 @@
+#!/usr/bin/env python3
+"""Raffi script filter for GitHub pull requests.
+
+Lists the open pull requests of a repository, or the open pull requests you
+wrote, were asked to review, are assigned to, or are involved in. When Raffi
+sets RAFFI_PREVIEW=1, it prints a preview of the pull request whose URL is in
+RAFFI_VALUE instead.
+"""
+
+import argparse
+import base64
+import json
+import os
+import re
+import subprocess
+import urllib.request
+from collections import Counter
+from datetime import UTC, datetime
+from pathlib import Path
+
+API_HOST = os.environ.get("API_HOST", "github.com")
+CACHE_PULLS = os.environ.get("CACHE_PULLS", "10m")
+ICON = "github"
+# Raffi gives the preview command 3 seconds in total.
+PREVIEW_TIMEOUT = 2.5
+PREVIEW_REFRESH_SECONDS = 20
+PREVIEW_COMMENTS = 3
+PREVIEW_FILES = 50
+# Accounts whose comments the preview skips, besides GitHub Apps.
+BOT_SUFFIXES = ("[bot]", "-bot", "-robot")
+
+CACHE_HOME = Path(os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache")
+AVATAR_DIR = CACHE_HOME / "raffi" / "github-avatars" / API_HOST
+AVATAR_TIMEOUT = 3
+# Raffi loads PNG and SVG icons only and GitHub avatars can be JPEG, so they
+# are embedded in an SVG, clipped to a circle.
+AVATAR_SVG = (
+ '<svg xmlns="http://www.w3.org/2000/svg" '
+ 'xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 128 128">'
+ '<clipPath id="round"><circle cx="64" cy="64" r="64"/></clipPath>'
+ '<image width="128" height="128" clip-path="url(#round)" '
+ 'xlink:href="data:{mime};base64,{data}"/></svg>'
+)
+
+REPO = re.compile(r"[\w.-]+/[\w.-]+")
+PR_URL = re.compile(r"https?://[^/]+/([\w.-]+)/([\w.-]+)/pull/(\d+)")
+ANSI = re.compile(r"\x1b\[[0-9;]*m")
+HTML_COMMENT = re.compile(r"<!--.*?-->", re.DOTALL)
+BLANK_LINES = re.compile(r"\n{3,}")
+
+# Markdown, rendered with the ANSI codes Raffi supports: colours, bold, underline.
+FENCE = re.compile(r"\s*(```|~~~)")
+HEADING = re.compile(r"#{1,6}\s+(.*?)[\s#]*$")
+RULE = re.compile(r"\s*([-*_])(\s*\1){2,}\s*$")
+LIST_ITEM = re.compile(r"(\s*)[-*+]\s+(?:\[([ xX])\]\s+)?(.*)")
+QUOTE = re.compile(r"\s*>\s?(.*)")
+ALERT = re.compile(r"\[!(\w+)\]$")
+TABLE_RULE = re.compile(r"\s*\|?(\s*:?-+:?\s*\|)+\s*(:?-+:?)?\s*$")
+CODE_SPAN = re.compile(r"(`+)(.+?)\1")
+HTML_TAG = re.compile(r"</?[a-zA-Z][^>]*>")
+IMAGE = re.compile(r"!\[([^\]]*)\]\([^)]*\)")
+LINK = re.compile(r"\[([^\]]+)\]\([^)]*\)")
+STRONG = re.compile(r"(\*\*|__)(.+?)\1")
+EMPHASIS = re.compile(r"(?<![\w*])([*_])(?!\s)(.+?)(?<!\s)\1(?![\w*])")
+
+GREEN = "\033[32m"
+RED = "\033[31m"
+YELLOW = "\033[33m"
+GREY = "\033[90m"
+RESET = "\033[0m"
+
+# In the order checks are listed in the preview.
+CI_STYLES = {
+ "failed": ("\uf057", RED, "Failed"),
+ "pending": ("\uf017", YELLOW, "InProgress"),
+ "passed": ("\uf058", GREEN, "Passing"),
+ "skipped": ("\uf056", GREY, "Skipped"),
+}
+FAILED = {
+ "FAILURE",
+ "ERROR",
+ "TIMED_OUT",
+ "CANCELLED",
+ "ACTION_REQUIRED",
+ "STARTUP_FAILURE",
+}
+PENDING = {"PENDING", "EXPECTED"}
+
+REVIEW = {
+ "APPROVED": "Approved",
+ "CHANGES_REQUESTED": "Changes requested",
+ "REVIEW_REQUIRED": "Review required",
+}
+REVIEW_COLORS = {"APPROVED": GREEN, "CHANGES_REQUESTED": RED, "REVIEW_REQUIRED": YELLOW}
+# Verdicts of individual reviewers, in the order they are listed.
+REVIEWER_MARKS = {
+ "APPROVED": "\u2713",
+ "CHANGES_REQUESTED": "\u2717",
+ "COMMENTED": "\U0001f4ac",
+}
+
+MERGE_STATE = {
+ "BEHIND": "Behind base",
+ "BLOCKED": "Blocked",
+ "CLEAN": "Ready",
+ "DIRTY": "Conflicts",
+ "DRAFT": "Draft",
+ "HAS_HOOKS": "Ready",
+ "UNKNOWN": "Checking",
+ "UNSTABLE": "Ready, with failing checks",
+}
+
+CHANGE_STYLES = {
+ "ADDED": ("A", GREEN),
+ "DELETED": ("D", RED),
+ "RENAMED": ("R", "\033[36m"),
+ "COPIED": ("C", "\033[36m"),
+}
+
+# First query words listing pull requests across every repository: the search
+# query, then the title and GitHub page of the first row.
+MODES = {
+ "myprs": ("author:@me", "Open your pull requests dashboard", "pulls"),
+ "review": (
+ "review-requested:@me",
+ "Open your review requests",
+ "pulls/review-requested",
+ ),
+ "assigned": ("assignee:@me", "Open your assigned pull requests", "pulls/assigned"),
+ "involved": (
+ "involves:@me",
+ "Open the pull requests involving you",
+ "pulls?q=is%3Aopen+is%3Apr+involves%3A%40me",
+ ),
+}
+
+PR_FRAGMENT = """
+fragment pr on PullRequest {
+ number title url isDraft headRefName reviewDecision mergeable
+ additions deletions updatedAt
+ repository { nameWithOwner owner { login avatarUrl(size: 128) } }
+ author { login avatarUrl(size: 128) }
+ reviewThreads(first: 100) { nodes { isResolved } }
+ commits(last: 1) { nodes { commit { statusCheckRollup { state } } } }
+}
+"""
+REPO_QUERY = (
+ """
+query($owner: String!, $name: String!) {
+ repository(owner: $owner, name: $name) {
+ pullRequests(states: OPEN, first: 50, orderBy: {field: CREATED_AT, direction: DESC}) {
+ nodes { ...pr }
+ }
+ }
+}
+"""
+ + PR_FRAGMENT
+)
+SEARCH_QUERY = (
+ """
+query($q: String!) {
+ search(query: $q, type: ISSUE, first: 50) {
+ nodes { ...pr }
+ }
+}
+"""
+ + PR_FRAGMENT
+)
+
+# Everything the preview shows, in one request: each request costs about a
+# second and Raffi gives the preview 3.
+PREVIEW_QUERY = """
+query($owner: String!, $name: String!, $number: Int!, $files: Int!) {
+ repository(owner: $owner, name: $name) {
+ pullRequest(number: $number) {
+ number title url state isDraft body createdAt updatedAt mergedAt
+ headRefName baseRefName reviewDecision mergeable mergeStateStatus
+ additions deletions changedFiles
+ author { login }
+ repository { nameWithOwner }
+ autoMergeRequest { mergeMethod enabledBy { login } }
+ milestone { title }
+ assignees(first: 10) { nodes { login } }
+ labels(first: 20) { nodes { name } }
+ reviewRequests(first: 20) {
+ nodes { requestedReviewer { ... on User { login } ... on Team { name } } }
+ }
+ latestReviews(first: 20) { nodes { author { login } state } }
+ reviewThreads(first: 100) { nodes { isResolved } }
+ comments(last: 10) {
+ totalCount
+ nodes { author { __typename login } createdAt body }
+ }
+ closingIssuesReferences(first: 10) {
+ nodes { number title state repository { nameWithOwner } }
+ }
+ files(first: $files) {
+ totalCount
+ nodes { path additions deletions changeType }
+ }
+ commits(last: 1) {
+ totalCount
+ nodes {
+ commit {
+ messageHeadline
+ statusCheckRollup {
+ contexts(first: 100) {
+ nodes {
+ __typename
+ ... on CheckRun {
+ name status conclusion
+ checkSuite { workflowRun { workflow { name } } }
+ }
+ ... on StatusContext { context state }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+"""
+
+
+class GhError(Exception):
+ pass
+
+
+def run_gh(*args: str, timeout: float = 30) -> dict:
+ try:
+ result = subprocess.run(
+ ["gh", *args], capture_output=True, text=True, timeout=timeout, check=False
+ )
+ except FileNotFoundError:
+ raise GhError("GitHub CLI not found, see https://cli.github.com") from None
+ except subprocess.TimeoutExpired:
+ raise GhError(f"gh did not answer within {timeout} seconds") from None
+ if result.returncode:
+ raise GhError(result.stderr.strip() or f"gh exited with {result.returncode}")
+ try:
+ return json.loads(result.stdout)
+ except json.JSONDecodeError as error:
+ raise GhError(f"gh returned invalid JSON: {error}") from None
+
+
+def graphql(
+ query: str, cache: str | None = CACHE_PULLS, timeout: float = 30, **variables
+) -> dict:
+ args = ["api", "graphql", "--hostname", API_HOST]
+ if cache:
+ args += ["--cache", cache]
+ for name, value in {"query": query, **variables}.items():
+ # -F sends integers as numbers, -f keeps everything else a string.
+ args += ["-F" if isinstance(value, int) else "-f", f"{name}={value}"]
+ response = run_gh(*args, timeout=timeout)
+ if errors := response.get("errors"):
+ raise GhError(errors[0].get("message", "GraphQL error"))
+ return response["data"]
+
+
+def parse_query(query: str, repo: str | None = None) -> tuple[str, str]:
+ """Split the typed text into (repository or a MODES key, filter)."""
+ query = query.strip()
+ if repo:
+ return os.environ.get(f"ALIAS_{repo}", repo), query
+ word, _, rest = query.partition(" ")
+ rest = rest.strip()
+ if word in MODES:
+ return word, rest
+ if alias := os.environ.get(f"ALIAS_{word}"):
+ return alias, rest
+ if "/" in word:
+ return word, rest
+ return "myprs", query
+
+
+def ci_group(state: str) -> str:
+ if state in FAILED:
+ return "failed"
+ if state in PENDING:
+ return "pending"
+ if state == "SUCCESS":
+ return "passed"
+ return "skipped"
+
+
+def colored(text: str, color: str) -> str:
+ return f"{color}{text}{RESET}"
+
+
+def author_login(pr: dict) -> str:
+ return (pr.get("author") or {}).get("login", "ghost")
+
+
+def nodes(pr: dict, field: str) -> list:
+ return [node for node in (pr.get(field) or {}).get("nodes") or [] if node]
+
+
+def unresolved_threads(pr: dict) -> int:
+ return sum(not thread["isResolved"] for thread in nodes(pr, "reviewThreads"))
+
+
+def size(change: dict) -> str:
+ added = colored("+{}".format(change["additions"]), GREEN)
+ deleted = colored("-{}".format(change["deletions"]), RED)
+ return f"{added} {deleted}"
+
+
+def build_item(pr: dict, show_repo: bool) -> dict:
+ url = pr["url"]
+ repo = pr["repository"]["nameWithOwner"]
+ suffix = repo if show_repo else author_login(pr)
+ status = "Draft" if pr["isDraft"] else "Open"
+ commits = pr["commits"]["nodes"]
+ rollup = commits[0]["commit"]["statusCheckRollup"] if commits else None
+ if rollup:
+ icon, color, label = CI_STYLES[ci_group(rollup["state"])]
+ status = f"{icon} {colored(f'CI: {label}', color)} \u00b7 {status}"
+ details = [status]
+ if review := REVIEW.get(pr.get("reviewDecision")):
+ details.append(colored(review, REVIEW_COLORS[pr["reviewDecision"]]))
+ if pr.get("mergeable") == "CONFLICTING":
+ details.append(colored("Conflicts", RED))
+ if threads := unresolved_threads(pr):
+ details.append(f"\U0001f4ac {threads} unresolved")
+ if "additions" in pr:
+ details.append(size(pr))
+ if updated := pr.get("updatedAt"):
+ details.append(colored(f"updated {ago(updated)}", GREY))
+ return {
+ "title": f"PR #{pr['number']}: {pr['title']} - {suffix}",
+ "subtitle": " \u00b7 ".join(details),
+ "arg": url,
+ "variables": {
+ "PR_URL": url,
+ "PR_REPO": repo,
+ "PR_NUMBER": str(pr["number"]),
+ "PR_BRANCH": pr.get("headRefName", ""),
+ },
+ }
+
+
+def avatar_path(owner: str) -> Path:
+ return AVATAR_DIR / f"{owner}.svg"
+
+
+def account_icon(account: dict | None) -> str | None:
+ """Path of a GitHub account's avatar, downloaded on first use."""
+ account = account or {}
+ if not account.get("avatarUrl"):
+ return None
+ path = avatar_path(account["login"])
+ if path.exists():
+ return str(path)
+ try:
+ with urllib.request.urlopen(
+ account["avatarUrl"], timeout=AVATAR_TIMEOUT
+ ) as response:
+ mime = response.headers.get_content_type()
+ data = base64.b64encode(response.read()).decode()
+ except OSError:
+ return None
+ if not mime.startswith("image/"):
+ return None
+ path.parent.mkdir(parents=True, exist_ok=True)
+ partial = path.with_suffix(".part")
+ partial.write_text(AVATAR_SVG.format(mime=mime, data=data))
+ partial.replace(path)
+ return str(path)
+
+
+def link_item(title: str, url: str) -> dict:
+ return {
+ "title": title,
+ "subtitle": url,
+ "arg": url,
+ "preview": {"title": title, "text": f"Opens {url}", "dynamic": False},
+ }
+
+
+def error_item(title: str, message: str) -> dict:
+ return {
+ "title": title,
+ "subtitle": message,
+ "preview": {"title": title, "text": message, "dynamic": False},
+ }
+
+
+def list_items(query: str, repo_arg: str | None = None) -> list[dict]:
+ target, needle = parse_query(query, repo_arg)
+ if target not in MODES and not REPO.fullmatch(target):
+ return [
+ error_item("Invalid repository", f"Expected owner/repo, got {target!r}")
+ ]
+
+ try:
+ if target in MODES:
+ search, title, page = MODES[target]
+ data = graphql(SEARCH_QUERY, q=f"is:pr is:open {search}")
+ pulls = data["search"]["nodes"]
+ items = [link_item(title, f"https://{API_HOST}/{page}")]
+ else:
+ owner, name = target.split("/")
+ data = graphql(REPO_QUERY, owner=owner, name=name)
+ pulls = data["repository"]["pullRequests"]["nodes"]
+ url = f"https://{API_HOST}/{target}/pulls"
+ items = [link_item("Open all pull requests page", url)]
+ except GhError as error:
+ return [error_item("GitHub error", str(error))]
+
+ needle = needle.casefold()
+ matches: list[tuple[dict, dict]] = []
+ for pr in filter(None, pulls):
+ item = build_item(pr, show_repo=target in MODES)
+ text = f"{item['title']} {item['subtitle']} {author_login(pr)}"
+ if needle in ANSI.sub("", text).casefold():
+ matches.append((pr, item))
+
+ # Within one repository every owner avatar is the same, so show who
+ # opened each pull request instead.
+ single_repo = len({pr["repository"]["nameWithOwner"] for pr, _ in matches}) == 1
+ for pr, item in matches:
+ account = pr.get("author") if single_repo else pr["repository"].get("owner")
+ if icon := account_icon(account):
+ item["icon"] = {"path": icon}
+
+ if target not in MODES:
+ items.extend(item for _, item in matches)
+ return items
+
+ groups: dict[str, list[dict]] = {}
+ for pr, item in matches:
+ owner = pr["repository"]["nameWithOwner"].split("/", 1)[0]
+ groups.setdefault(owner, []).append(item)
+
+ if len(groups) < 2:
+ items.extend(item for _, item in matches)
+ return items
+
+ for owner, owner_items in groups.items():
+ items.append({"title": owner, "indent": 0, "valid": False})
+ for item in owner_items:
+ item["indent"] = 1
+ items.append(item)
+ return items
+
+
+def check_name(check: dict) -> str:
+ name = check.get("name") or check.get("context") or "?"
+ run = ((check.get("checkSuite") or {}).get("workflowRun")) or {}
+ if workflow := (run.get("workflow") or {}).get("name"):
+ return f"{workflow} / {name}"
+ return name
+
+
+def check_state(check: dict) -> str:
+ if check.get("__typename") == "StatusContext":
+ return check.get("state", "")
+ if check.get("status") != "COMPLETED":
+ return "PENDING"
+ return check.get("conclusion", "")
+
+
+def checks_section(checks: list[dict]) -> dict:
+ order = list(CI_STYLES)
+ rows = sorted(
+ ((ci_group(check_state(check)), check_name(check)) for check in checks),
+ key=lambda row: (order.index(row[0]), row[1]),
+ )
+ counts = Counter(group for group, _ in rows)
+ summary = ", ".join(f"{counts[group]} {group}" for group in order if counts[group])
+ lines = []
+ for group, name in rows:
+ icon, color, _ = CI_STYLES[group]
+ lines.append(f"{colored(icon, color)} {name}")
+ return {
+ "type": "text",
+ "heading": f"Checks: {summary}",
+ "content": "\n".join(lines),
+ }
+
+
+def ago(timestamp: str) -> str:
+ seconds = (datetime.now(UTC) - datetime.fromisoformat(timestamp)).total_seconds()
+ for unit, length in (
+ ("y", 31536000),
+ ("mo", 2592000),
+ ("d", 86400),
+ ("h", 3600),
+ ("m", 60),
+ ):
+ if seconds >= length:
+ return f"{int(seconds // length)}{unit} ago"
+ return "just now"
+
+
+def pr_state(pr: dict) -> str:
+ if pr["state"] == "OPEN" and pr["isDraft"]:
+ return "Draft"
+ return pr["state"].capitalize()
+
+
+def sgr(text: str, on: str, off: str) -> str:
+ return f"\033[{on}m{text}\033[{off}m"
+
+
+def render_text(text: str) -> str:
+ text = HTML_TAG.sub("", text)
+ text = IMAGE.sub(lambda m: sgr(f"[image: {m[1] or 'image'}]", "90", "39"), text)
+ text = LINK.sub(lambda m: sgr(m[1], "4;34", "24;39"), text)
+ text = STRONG.sub(lambda m: sgr(m[2], "1", "22"), text)
+ # Raffi has no italics, so emphasis only loses its markers.
+ return EMPHASIS.sub(r"\2", text)
+
+
+def render_inline(line: str) -> str:
+ """Render inline markdown, leaving code spans untouched."""
+ parts, pos = [], 0
+ for match in CODE_SPAN.finditer(line):
+ parts.append(render_text(line[pos : match.start()]))
+ parts.append(sgr(match[2], "33", "39"))
+ pos = match.end()
+ parts.append(render_text(line[pos:]))
+ return "".join(parts)
+
+
+def render_markdown(text: str) -> str:
+ lines, in_code = [], False
+ for line in text.splitlines():
+ if FENCE.match(line):
+ in_code = not in_code
+ elif in_code:
+ lines.append(sgr(f" {line}", "33", "39"))
+ elif match := HEADING.match(line):
+ lines.append(sgr(render_inline(match[1]), "1;35", "0"))
+ elif RULE.match(line):
+ lines.append(sgr("\u2500" * 40, "90", "39"))
+ elif TABLE_RULE.match(line):
+ continue
+ elif match := LIST_ITEM.match(line):
+ indent, box, item = match.groups()
+ if box is None:
+ marker = "\u2022"
+ elif box == " ":
+ marker = "\u2610"
+ else:
+ marker = sgr("\u2611", "32", "39")
+ lines.append(f"{indent}{marker} {render_inline(item)}")
+ elif match := QUOTE.match(line):
+ quote = match[1]
+ if alert := ALERT.match(quote):
+ quote = sgr(alert[1].capitalize(), "1;33", "0")
+ else:
+ quote = render_inline(quote)
+ lines.append(sgr("\u2502 ", "90", "39") + quote)
+ else:
+ lines.append(render_inline(line))
+ return "\n".join(line.rstrip() for line in lines)
+
+
+def clean_body(body: str) -> str:
+ body = HTML_COMMENT.sub("", body.replace("\r\n", "\n"))
+ return BLANK_LINES.sub("\n\n", render_markdown(body)).strip()
+
+
+def is_bot(author: dict | None) -> bool:
+ author = author or {}
+ login = author.get("login", "")
+ return author.get("__typename") == "Bot" or login.endswith(BOT_SUFFIXES)
+
+
+def reviewers(pr: dict) -> str:
+ """Each reviewer's latest verdict, then the reviews still requested."""
+ verdicts = []
+ for review in nodes(pr, "latestReviews"):
+ if mark := REVIEWER_MARKS.get(review["state"]):
+ verdicts.append(f"{mark} {author_login(review)}")
+ for request in nodes(pr, "reviewRequests"):
+ reviewer = request.get("requestedReviewer") or {}
+ if who := reviewer.get("login") or reviewer.get("name"):
+ verdicts.append(f"\u23f3 {who}")
+ return ", ".join(verdicts)
+
+
+def merge_state(pr: dict) -> str:
+ if pr["state"] == "MERGED":
+ return f"Merged {ago(pr['mergedAt'])}" if pr.get("mergedAt") else "Merged"
+ if pr["state"] != "OPEN":
+ return ""
+ if pr.get("mergeable") == "CONFLICTING":
+ state = "Conflicts"
+ else:
+ state = MERGE_STATE.get(pr.get("mergeStateStatus"), "")
+ if auto := pr.get("autoMergeRequest"):
+ by = (auto.get("enabledBy") or {}).get("login")
+ method = (auto.get("mergeMethod") or "").lower()
+ state += f", auto-merge ({method})" + (f" by {by}" if by else "")
+ return state.strip(", ")
+
+
+def closes_section(pr: dict) -> dict | None:
+ issues = nodes(pr, "closingIssuesReferences")
+ if not issues:
+ return None
+ repo = pr["repository"]["nameWithOwner"]
+ lines = []
+ for issue in issues:
+ state = issue["state"].lower()
+ color = GREEN if state == "open" else "\033[35m"
+ where = issue["repository"]["nameWithOwner"]
+ ref = f"#{issue['number']}" if where == repo else f"{where}#{issue['number']}"
+ lines.append(
+ f"{sgr(ref, '1', '22')} {colored(f'[{state}]', color)} {issue['title']}"
+ )
+ return {"type": "text", "heading": "Closes", "content": "\n".join(lines)}
+
+
+def comment_sections(pr: dict) -> list[dict]:
+ comments = [c for c in nodes(pr, "comments") if not is_bot(c.get("author"))]
+ return [
+ {
+ "type": "text",
+ "heading": f"\U0001f4ac {author_login(comment)} \u00b7 {ago(comment['createdAt'])}",
+ "content": clean_body(comment["body"]) or "(empty)",
+ }
+ for comment in reversed(comments[-PREVIEW_COMMENTS:])
+ ]
+
+
+def files_section(pr: dict) -> dict | None:
+ files = nodes(pr, "files")
+ if not files:
+ return None
+ lines = []
+ for file in files:
+ letter, color = CHANGE_STYLES.get(file["changeType"], ("M", YELLOW))
+ lines.append(f"{colored(letter, color)} {file['path']} {size(file)}")
+ total = (pr.get("files") or {}).get("totalCount", len(files))
+ if total > len(files):
+ lines.append(colored(f"\u2026 and {total - len(files)} more", GREY))
+ return {"type": "text", "heading": f"Files ({total})", "content": "\n".join(lines)}
+
+
+def build_preview(pr: dict) -> dict:
+ repo = pr["repository"]["nameWithOwner"]
+ author = author_login(pr)
+ metadata = [
+ ["Repository", repo],
+ ["Author", author],
+ ["Branch", f"{pr['headRefName']} \u2192 {pr['baseRefName']}"],
+ ]
+ if review := REVIEW.get(pr["reviewDecision"]):
+ metadata.append(["Review", review])
+ if who := reviewers(pr):
+ metadata.append(["Reviewers", who])
+ if merge := merge_state(pr):
+ metadata.append(["Merge", merge])
+ if threads := unresolved_threads(pr):
+ metadata.append(["Threads", f"{threads} unresolved"])
+ commit_nodes = nodes(pr, "commits")
+ if commit_nodes:
+ total = pr["commits"]["totalCount"]
+ headline = commit_nodes[0]["commit"]["messageHeadline"]
+ label = f"{total} commit{'s' * (total != 1)}"
+ metadata.append(["Commits", f"{label}, last: {headline}"])
+ if assignees := ", ".join(a["login"] for a in nodes(pr, "assignees")):
+ metadata.append(["Assignees", assignees])
+ if milestone := (pr.get("milestone") or {}).get("title"):
+ metadata.append(["Milestone", milestone])
+ if labels := ", ".join(label["name"] for label in nodes(pr, "labels")):
+ metadata.append(["Labels", labels])
+ files = pr["changedFiles"]
+ changes = (
+ f"+{pr['additions']} -{pr['deletions']} in {files} file{'s' * (files != 1)}"
+ )
+ metadata += [
+ ["Changes", changes],
+ ["Created", ago(pr["createdAt"])],
+ ["Updated", ago(pr["updatedAt"])],
+ ]
+
+ rollup = commit_nodes[0]["commit"]["statusCheckRollup"] if commit_nodes else None
+ checks = nodes(rollup or {}, "contexts")
+ sections = [checks_section(checks)] if checks else []
+ if closes := closes_section(pr):
+ sections.append(closes)
+ sections.append(
+ {
+ "type": "text",
+ "heading": "Description",
+ "content": clean_body(pr["body"]) or "(no description)",
+ }
+ )
+ sections += comment_sections(pr)
+ if changed := files_section(pr):
+ sections.append(changed)
+
+ avatar = avatar_path(repo.split("/")[0])
+ preview = {
+ "title": f"#{pr['number']} {pr['title']}",
+ "subtitle": f"{repo} by {author}",
+ "badge": pr_state(pr),
+ "icon": str(avatar) if avatar.exists() else ICON,
+ "sections": sections,
+ "metadata": metadata,
+ }
+ if any(ci_group(check_state(check)) == "pending" for check in checks):
+ preview["refresh"] = PREVIEW_REFRESH_SECONDS
+ return preview
+
+
+def preview(url: str) -> dict:
+ match = PR_URL.match(url)
+ if not match:
+ return {
+ "title": url or "Pull request",
+ "icon": ICON,
+ "text": "Not a pull request URL.",
+ }
+ owner, name, number = match.groups()
+ try:
+ data = graphql(
+ PREVIEW_QUERY,
+ cache=None,
+ timeout=PREVIEW_TIMEOUT,
+ owner=owner,
+ name=name,
+ number=int(number),
+ files=PREVIEW_FILES,
+ )
+ pr = (data.get("repository") or {}).get("pullRequest")
+ if not pr:
+ raise GhError("pull request not found")
+ except GhError as error:
+ return {
+ "title": url,
+ "icon": ICON,
+ "text": f"Cannot load this pull request: {error}",
+ }
+ return build_preview(pr)
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+ parser.add_argument(
+ "--repo", help="owner/repo or alias to list; the query only filters"
+ )
+ parser.add_argument("query", nargs="*", help="text typed after the Raffi keyword")
+ args = parser.parse_args(argv)
+
+ if os.environ.get("RAFFI_PREVIEW") == "1":
+ output = preview(os.environ.get("RAFFI_VALUE", ""))
+ else:
+ output = {"items": list_items(" ".join(args.query), args.repo)}
+ print(json.dumps(output, ensure_ascii=False))
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
dots/config/raffi/scripts/github/README.md
@@ -0,0 +1,151 @@
+# GitHub Pull Requests Script Filter
+
+This script lets Raffi browse open GitHub pull requests: your own, the ones
+waiting for your review, assigned to you or involving you, or the ones of a
+given repository. Each row shows CI status, review decision, merge conflicts,
+unresolved review threads, size and last update. The preview pane shows the
+full picture: reviewers, merge state, checks, linked issues, description,
+latest comments and changed files.
+
+## Requirements
+
+- Raffi native mode
+- Python 3.11 or newer
+- The [GitHub CLI](https://cli.github.com), logged in with `gh auth login`
+
+## Install
+
+```bash
+mkdir -p ~/.config/raffi/scripts/github
+cp scripts/github/pr.py ~/.config/raffi/scripts/github/pr.py
+chmod +x ~/.config/raffi/scripts/github/pr.py
+```
+
+## Configure
+
+Add this entry under `addons.script_filters` in `raffi.yaml`:
+
+```yaml
+addons:
+ script_filters:
+ - name: "Pull Requests"
+ keyword: "pr"
+ command: "~/.config/raffi/scripts/github/pr.py"
+ icon: "github"
+ action: "xdg-open {value}"
+ secondary_action: "copy"
+```
+
+To give a repository its own keyword, pin it with `--repo`. Everything typed
+after the keyword then filters that repository's pull requests:
+
+```yaml
+ - name: "Linus Pull Requests"
+ keyword: "linoux"
+ command: "~/.config/raffi/scripts/github/pr.py"
+ args: ["--repo", "linus/linux"]
+ icon: "github"
+ action: "xdg-open {value}"
+ secondary_action: "copy"
+```
+
+## Usage
+
+```text
+pr your open pull requests
+pr raffi your open pull requests matching "raffi"
+pr review pull requests waiting for your review
+pr assigned pull requests assigned to you
+pr involved pull requests you authored, review, were mentioned in
+ or commented on
+pr owner/repo open pull requests of owner/repo
+pr owner/repo login fix the same, filtered by "login fix"
+pr myprs failed your open pull requests with failing CI
+pr review approved review requests already approved by someone
+```
+
+The filter matches everything shown on the row: number, title, repository or
+author, state, CI status, review decision, "Conflicts", unresolved threads and
+size. The first row opens the matching pull request list on GitHub. Enter
+opens the selected pull request, Ctrl+Enter copies its URL.
+
+Cross-repository modes group results under non-actionable repository-owner
+headings when the filtered results span multiple owners. Pull requests are
+indented beneath each heading; single-owner results and `owner/repo` queries
+stay flat.
+
+Each row also passes these variables to `action` and `secondary_action`:
+`PR_URL`, `PR_REPO` (owner/repo), `PR_NUMBER` and `PR_BRANCH`. For example,
+to copy the branch name with Ctrl+Enter:
+
+```yaml
+ secondary_action: 'echo -n "$PR_BRANCH" | wl-copy'
+```
+
+Each pull request shows the avatar of the repository owner, or of its author
+when every listed pull request comes from the same repository. Avatars are
+downloaded once into `~/.cache/raffi/github-avatars/` (or under
+`$XDG_CACHE_HOME`); delete that directory to refresh them. When an avatar
+cannot be downloaded, the entry's icon is used instead.
+
+## Aliases
+
+Set `ALIAS_<name>` in the entry's `env` to use a short name for a repository,
+as the first word of the query or as the `--repo` value:
+
+```yaml
+ env:
+ ALIAS_linoux: "linus/linux"
+```
+
+`pr linoux` then lists the pull requests of `linus/linux`.
+
+## Settings
+
+These environment variables can also be set in the entry's `env`:
+
+| Variable | Default | Description |
+|----------|---------|-------------|
+| `API_HOST` | `github.com` | GitHub host, for GitHub Enterprise |
+| `CACHE_PULLS` | `10m` | How long `gh` caches the pull request list |
+
+## Live preview
+
+With the preview pane open (`Ctrl+P`), the selected pull request shows:
+
+- its author, branches, review decision and each reviewer's verdict
+ (โ approved, โ changes requested, ๐ฌ commented, โณ still requested)
+- its merge state: ready, blocked, behind, conflicts, auto-merge, or when it
+ was merged
+- unresolved review threads, commit count and last commit, assignees,
+ milestone, labels, size and dates
+- its checks, failed ones first
+- the issues it closes
+- its description, with the markdown rendered in colour
+- the last three comments from people, newest first (bot comments are hidden)
+- the changed files with their size, up to 50
+
+The preview makes a single GraphQL request, so it usually shows within a
+second. While a check is still running, it refreshes every 20 seconds.
+
+Raffi asks for the preview by running the script again with `RAFFI_PREVIEW=1`
+and the pull request URL in `RAFFI_VALUE`. To try it by hand:
+
+```bash
+RAFFI_PREVIEW=1 RAFFI_VALUE=https://github.com/chmouel/raffi/pull/1 \
+ ~/.config/raffi/scripts/github/pr.py
+```
+
+Add `dynamic_preview: false` to the entry to turn this off.
+
+## Coming from alfred-pr-workflow
+
+This script replaces `pr.py` from
+[alfred-pr-workflow](https://gitlab.com/chmouel/alfred-pr-workflow). Colours
+are always on, so remove `--color` from `args`.
+
+## Tests
+
+```bash
+cd scripts/github && python3 -m unittest test_pr
+```
dots/config/raffi/scripts/github/test_pr.py
@@ -0,0 +1,487 @@
+import io
+import json
+import os
+import tempfile
+import unittest
+from contextlib import redirect_stdout
+from pathlib import Path
+from unittest.mock import MagicMock, patch
+
+import pr
+
+
+def node(number, title, ci_state=None, draft=False, repo="owner/repo"):
+ rollup = {"state": ci_state} if ci_state else None
+ return {
+ "number": number,
+ "title": title,
+ "url": f"https://github.com/{repo}/pull/{number}",
+ "isDraft": draft,
+ "repository": {"nameWithOwner": repo},
+ "author": {"login": "alice"},
+ "commits": {"nodes": [{"commit": {"statusCheckRollup": rollup}}]},
+ }
+
+
+PULLS = [node(1, "Fix login", "FAILURE"), node(2, "Add docs", draft=True)]
+
+
+def check_run(name, status, conclusion=None, workflow=None):
+ run = {"workflowRun": {"workflow": {"name": workflow}}} if workflow else None
+ return {
+ "__typename": "CheckRun",
+ "name": name,
+ "status": status,
+ "conclusion": conclusion,
+ "checkSuite": run,
+ }
+
+
+VIEW = {
+ "number": 7,
+ "title": "Add feature",
+ "url": "https://github.com/owner/repo/pull/7",
+ "state": "OPEN",
+ "isDraft": False,
+ "author": {"login": "alice"},
+ "repository": {"nameWithOwner": "owner/repo"},
+ "headRefName": "feature",
+ "baseRefName": "main",
+ "reviewDecision": "APPROVED",
+ "mergeable": "MERGEABLE",
+ "mergeStateStatus": "CLEAN",
+ "autoMergeRequest": {"mergeMethod": "SQUASH", "enabledBy": {"login": "bob"}},
+ "milestone": {"title": "v1.0"},
+ "assignees": {"nodes": [{"login": "alice"}]},
+ "labels": {"nodes": [{"name": "bug"}]},
+ "reviewRequests": {"nodes": [{"requestedReviewer": {"name": "core"}}]},
+ "latestReviews": {
+ "nodes": [
+ {"author": {"login": "bob"}, "state": "APPROVED"},
+ {"author": {"login": "carol"}, "state": "CHANGES_REQUESTED"},
+ ]
+ },
+ "reviewThreads": {"nodes": [{"isResolved": False}, {"isResolved": True}]},
+ "comments": {
+ "nodes": [
+ {
+ "author": {"login": "bob"},
+ "createdAt": "2026-01-01T00:00:00Z",
+ "body": "first",
+ },
+ {
+ "author": {"__typename": "Bot", "login": "codecov"},
+ "createdAt": "2026-01-01T01:00:00Z",
+ "body": "coverage",
+ },
+ {
+ "author": {"login": "ci-robot"},
+ "createdAt": "2026-01-01T01:30:00Z",
+ "body": "/approve",
+ },
+ {
+ "author": {"login": "carol"},
+ "createdAt": "2026-01-01T02:00:00Z",
+ "body": "second",
+ },
+ ]
+ },
+ "closingIssuesReferences": {
+ "nodes": [
+ {
+ "number": 3,
+ "title": "Broken",
+ "state": "OPEN",
+ "repository": {"nameWithOwner": "owner/repo"},
+ },
+ {
+ "number": 9,
+ "title": "Upstream",
+ "state": "CLOSED",
+ "repository": {"nameWithOwner": "other/lib"},
+ },
+ ]
+ },
+ "additions": 10,
+ "deletions": 2,
+ "changedFiles": 3,
+ "files": {
+ "totalCount": 3,
+ "nodes": [
+ {"path": "a.py", "additions": 8, "deletions": 0, "changeType": "ADDED"},
+ {"path": "b.py", "additions": 2, "deletions": 2, "changeType": "MODIFIED"},
+ ],
+ },
+ "createdAt": "2026-01-01T00:00:00Z",
+ "updatedAt": "2026-01-02T00:00:00Z",
+ "body": "Intro\r\n<!-- template hint -->\n\n\n\nDetails",
+ "commits": {
+ "totalCount": 2,
+ "nodes": [
+ {
+ "commit": {
+ "messageHeadline": "Add feature",
+ "statusCheckRollup": {
+ "contexts": {
+ "nodes": [
+ check_run("lint", "COMPLETED", "SUCCESS"),
+ check_run("e2e", "IN_PROGRESS", workflow="CI"),
+ {
+ "__typename": "StatusContext",
+ "context": "ci/legacy",
+ "state": "FAILURE",
+ },
+ ]
+ }
+ },
+ }
+ }
+ ],
+ },
+}
+
+
+class ParseQueryTests(unittest.TestCase):
+ def test_targets(self):
+ cases = {
+ "": ("myprs", ""),
+ "myprs fix": ("myprs", "fix"),
+ "review": ("review", ""),
+ "involved docs": ("involved", "docs"),
+ "raffi docs": ("myprs", "raffi docs"),
+ "owner/repo fix login": ("owner/repo", "fix login"),
+ "linoux fix": ("linus/linux", "fix"),
+ }
+ with patch.dict(os.environ, {"ALIAS_linoux": "linus/linux"}):
+ for query, expected in cases.items():
+ with self.subTest(query=query):
+ self.assertEqual(pr.parse_query(query), expected)
+
+ def test_repo_argument_makes_whole_query_a_filter(self):
+ self.assertEqual(
+ pr.parse_query(" a/b c ", "owner/repo"), ("owner/repo", "a/b c")
+ )
+
+
+class ListItemsTests(unittest.TestCase):
+ def list(self, query, response):
+ with patch.object(pr, "run_gh", return_value={"data": response}) as run_gh:
+ return pr.list_items(query), run_gh
+
+ def test_repository_pulls_are_filtered(self):
+ response = {"repository": {"pullRequests": {"nodes": PULLS}}}
+ items, run_gh = self.list("owner/repo failed", response)
+
+ self.assertIn("owner=owner", run_gh.call_args.args)
+ self.assertEqual(items[0]["arg"], "https://github.com/owner/repo/pulls")
+ self.assertEqual(
+ [item["title"] for item in items[1:]], ["PR #1: Fix login - alice"]
+ )
+ self.assertTrue(all("indent" not in item for item in items[1:]))
+ self.assertIn("\x1b[31mCI: Failed\x1b[0m \u00b7 Open", items[1]["subtitle"])
+ self.assertEqual(
+ items[1]["variables"],
+ {
+ "PR_URL": "https://github.com/owner/repo/pull/1",
+ "PR_REPO": "owner/repo",
+ "PR_NUMBER": "1",
+ "PR_BRANCH": "",
+ },
+ )
+
+ def test_own_pulls_show_repository_and_draft(self):
+ items, _ = self.list("docs", {"search": {"nodes": [*PULLS, {}]}})
+
+ self.assertEqual(items[0]["arg"], "https://github.com/pulls")
+ self.assertEqual(items[1]["title"], "PR #2: Add docs - owner/repo")
+ self.assertTrue(items[1]["subtitle"].startswith("Draft"))
+
+ def test_cross_repository_modes_group_pulls_by_owner(self):
+ pulls = [
+ node(1, "First", repo="alice/one"),
+ node(2, "Second", repo="org-b/two"),
+ node(3, "Third", repo="alice/three"),
+ ]
+ response = {"search": {"nodes": pulls}}
+ expected = [
+ ("alice", 0, False),
+ ("PR #1: First - alice/one", 1, None),
+ ("PR #3: Third - alice/three", 1, None),
+ ("org-b", 0, False),
+ ("PR #2: Second - org-b/two", 1, None),
+ ]
+
+ for mode in ("myprs", "review", "assigned", "involved"):
+ with self.subTest(mode=mode):
+ items, _ = self.list(mode, response)
+ self.assertEqual(
+ [
+ (item["title"], item.get("indent"), item.get("valid"))
+ for item in items[1:]
+ ],
+ expected,
+ )
+
+ def test_single_owner_cross_repository_results_stay_flat(self):
+ pulls = [
+ node(1, "First", repo="org/one"),
+ node(2, "Second", repo="org/two"),
+ ]
+ items, _ = self.list("myprs", {"search": {"nodes": pulls}})
+
+ self.assertEqual(
+ [item["title"] for item in items[1:]],
+ ["PR #1: First - org/one", "PR #2: Second - org/two"],
+ )
+ self.assertTrue(all("indent" not in item for item in items[1:]))
+
+ def test_filtering_precedes_owner_grouping(self):
+ pulls = [
+ node(1, "Needle one", repo="alice/one"),
+ node(2, "Other", repo="alice/two"),
+ node(3, "Needle three", repo="org-b/three"),
+ node(4, "Other", repo="org-c/four"),
+ ]
+ items, _ = self.list("myprs needle", {"search": {"nodes": pulls}})
+
+ self.assertEqual(
+ [
+ (item["title"], item.get("indent"), item.get("valid"))
+ for item in items[1:]
+ ],
+ [
+ ("alice", 0, False),
+ ("PR #1: Needle one - alice/one", 1, None),
+ ("org-b", 0, False),
+ ("PR #3: Needle three - org-b/three", 1, None),
+ ],
+ )
+
+ def test_modes_search_their_own_pull_requests(self):
+ items, run_gh = self.list("review", {"search": {"nodes": PULLS}})
+
+ self.assertIn("q=is:pr is:open review-requested:@me", run_gh.call_args.args)
+ self.assertEqual(items[0]["arg"], "https://github.com/pulls/review-requested")
+
+ def test_subtitle_shows_review_conflicts_threads_and_size(self):
+ pull = dict(
+ PULLS[0],
+ reviewDecision="CHANGES_REQUESTED",
+ mergeable="CONFLICTING",
+ reviewThreads={"nodes": [{"isResolved": False}]},
+ additions=5,
+ deletions=1,
+ )
+ subtitle = pr.ANSI.sub("", pr.build_item(pull, False)["subtitle"])
+
+ self.assertIn(
+ "Changes requested \u00b7 Conflicts \u00b7 \U0001f4ac 1 unresolved \u00b7 +5 -1",
+ subtitle,
+ )
+
+ def test_errors_become_items(self):
+ self.assertEqual(pr.list_items("owner/")[0]["title"], "Invalid repository")
+ with patch.object(pr, "run_gh", side_effect=pr.GhError("boom")):
+ self.assertEqual(pr.list_items("")[0]["subtitle"], "boom")
+
+
+class PreviewTests(unittest.TestCase):
+ def test_build_preview(self):
+ preview = pr.build_preview(VIEW)
+
+ self.assertEqual(preview["title"], "#7 Add feature")
+ self.assertEqual(preview["badge"], "Open")
+ metadata = dict(preview["metadata"])
+ self.assertEqual(metadata["Review"], "Approved")
+ self.assertEqual(metadata["Reviewers"], "\u2713 bob, \u2717 carol, \u23f3 core")
+ self.assertEqual(metadata["Merge"], "Ready, auto-merge (squash) by bob")
+ self.assertEqual(metadata["Threads"], "1 unresolved")
+ self.assertEqual(metadata["Commits"], "2 commits, last: Add feature")
+ self.assertEqual(metadata["Milestone"], "v1.0")
+ self.assertEqual(metadata["Changes"], "+10 -2 in 3 files")
+ checks, closes, description, *comments, files = preview["sections"]
+ self.assertEqual(checks["heading"], "Checks: 1 failed, 1 pending, 1 passed")
+ self.assertEqual(
+ pr.ANSI.sub("", checks["content"]).splitlines(),
+ ["\uf057 ci/legacy", "\uf017 CI / e2e", "\uf058 lint"],
+ )
+ self.assertEqual(
+ pr.ANSI.sub("", closes["content"]).splitlines(),
+ ["#3 [open] Broken", "other/lib#9 [closed] Upstream"],
+ )
+ self.assertEqual(description["content"], "Intro\n\nDetails")
+ self.assertEqual(
+ [c["content"] for c in comments], ["second", "first"], "bots skipped"
+ )
+ self.assertEqual(files["heading"], "Files (3)")
+ self.assertEqual(
+ pr.ANSI.sub("", files["content"]).splitlines(),
+ ["A a.py +8 -0", "M b.py +2 -2", "\u2026 and 1 more"],
+ )
+ self.assertEqual(preview["refresh"], pr.PREVIEW_REFRESH_SECONDS)
+
+ def test_merged_and_conflicting_states(self):
+ merged = dict(VIEW, state="MERGED", mergedAt=None)
+ conflicting = dict(VIEW, mergeable="CONFLICTING", autoMergeRequest=None)
+
+ self.assertEqual(pr.merge_state(merged), "Merged")
+ self.assertEqual(pr.merge_state(conflicting), "Conflicts")
+
+ def test_no_refresh_once_checks_are_done(self):
+ commit = VIEW["commits"]["nodes"][0]["commit"]
+ lint = commit["statusCheckRollup"]["contexts"]["nodes"][:1]
+ rollup = {"contexts": {"nodes": lint}}
+ commits = {
+ "totalCount": 1,
+ "nodes": [{"commit": dict(commit, statusCheckRollup=rollup)}],
+ }
+ done = dict(VIEW, isDraft=True, body="", commits=commits)
+ preview = pr.build_preview(done)
+
+ self.assertEqual(preview["badge"], "Draft")
+ self.assertEqual(preview["sections"][2]["content"], "(no description)")
+ self.assertNotIn("refresh", preview)
+
+ def test_main_prints_preview_when_raffi_asks(self):
+ stdout = io.StringIO()
+ env = {"RAFFI_PREVIEW": "1", "RAFFI_VALUE": VIEW["url"]}
+ with (
+ patch.dict(os.environ, env),
+ patch.object(
+ pr,
+ "run_gh",
+ return_value={"data": {"repository": {"pullRequest": VIEW}}},
+ ) as run_gh,
+ redirect_stdout(stdout),
+ ):
+ self.assertEqual(pr.main(["--repo", "owner/repo", "query"]), 0)
+
+ args = run_gh.call_args.args
+ self.assertEqual(args[:2], ("api", "graphql"))
+ self.assertIn("number=7", args)
+ self.assertNotIn("--cache", args)
+ self.assertEqual(json.loads(stdout.getvalue())["title"], "#7 Add feature")
+
+ def test_preview_error_is_shown_in_the_pane(self):
+ with patch.object(pr, "run_gh", side_effect=pr.GhError("offline")):
+ preview = pr.preview(VIEW["url"])
+
+ self.assertIn("offline", preview["text"])
+ self.assertEqual(pr.preview("nope")["text"], "Not a pull request URL.")
+ missing = {"data": {"repository": {"pullRequest": None}}}
+ with patch.object(pr, "run_gh", return_value=missing):
+ self.assertIn("not found", pr.preview(VIEW["url"])["text"])
+
+
+class MarkdownTests(unittest.TestCase):
+ def test_markdown_is_rendered_with_ansi(self):
+ body = """\
+## Summary
+- [x] done
+ - [ ] todo
+* **bold** `a*b*` _em_ snake_case [link](https://x) 
+> [!NOTE]
+> quoted
+<details><summary>More</summary>
+```
+**raw**
+```
+| a | b |
+|---|:-:|
+***"""
+ rendered = pr.render_markdown(body)
+
+ self.assertIn("\x1b[1;35mSummary", rendered)
+ self.assertIn("\x1b[1mbold\x1b[22m", rendered)
+ self.assertIn("\x1b[4;34mlink\x1b[24;39m", rendered)
+ self.assertEqual(
+ pr.ANSI.sub("", rendered).splitlines(),
+ [
+ "Summary",
+ "\u2611 done",
+ " \u2610 todo",
+ "\u2022 bold a*b* em snake_case link [image: shot]",
+ "\u2502 Note",
+ "\u2502 quoted",
+ "More",
+ " **raw**",
+ "| a | b |",
+ "\u2500" * 40,
+ ],
+ )
+
+
+class AvatarTests(unittest.TestCase):
+ def test_owner_avatar_is_cached_as_round_svg(self):
+ response = MagicMock()
+ response.__enter__.return_value = response
+ response.headers.get_content_type.return_value = "image/jpeg"
+ response.read.return_value = b"JPEG"
+ repository = {"owner": {"login": "octo", "avatarUrl": "https://avatars/u/1"}}
+
+ with (
+ tempfile.TemporaryDirectory() as tmp,
+ patch.object(pr, "AVATAR_DIR", Path(tmp)),
+ patch.object(
+ pr.urllib.request, "urlopen", return_value=response
+ ) as urlopen,
+ ):
+ path = pr.account_icon(repository["owner"])
+ self.assertEqual(pr.account_icon(repository["owner"]), path)
+ self.assertEqual(path, f"{tmp}/octo.svg")
+ svg = Path(path).read_text()
+ preview = pr.build_preview(
+ dict(VIEW, repository={"nameWithOwner": "octo/r"})
+ )
+
+ urlopen.assert_called_once()
+ self.assertIn("data:image/jpeg;base64,SlBFRw==", svg)
+ self.assertEqual(preview["icon"], path)
+
+ def test_no_avatar_falls_back_to_github_icon(self):
+ with patch.object(pr.urllib.request, "urlopen", side_effect=OSError("offline")):
+ self.assertIsNone(pr.account_icon({"login": "x", "avatarUrl": "u"}))
+ self.assertIsNone(pr.account_icon(None))
+
+ def test_single_repository_rows_show_author_avatars(self):
+ def pull(number, repo, author):
+ return dict(
+ node(number, "Fix", repo=repo),
+ author={"login": author, "avatarUrl": f"https://avatars/{author}"},
+ repository={
+ "nameWithOwner": repo,
+ "owner": {
+ "login": repo.split("/")[0],
+ "avatarUrl": "https://avatars/owner",
+ },
+ },
+ )
+
+ def icons(query, nodes, response_key):
+ response = (
+ {"search": {"nodes": nodes}}
+ if response_key == "search"
+ else {"repository": {"pullRequests": {"nodes": nodes}}}
+ )
+ with (
+ patch.object(pr, "run_gh", return_value={"data": response}),
+ patch.object(
+ pr, "account_icon", side_effect=lambda a: f"/{a['login']}.svg"
+ ),
+ ):
+ items = pr.list_items(query)
+ return [item["icon"]["path"] for item in items if "icon" in item]
+
+ same_repo = [pull(1, "org/one", "alice"), pull(2, "org/one", "bob")]
+ self.assertEqual(
+ icons("org/one", same_repo, "repository"), ["/alice.svg", "/bob.svg"]
+ )
+ self.assertEqual(
+ icons("review", same_repo, "search"), ["/alice.svg", "/bob.svg"]
+ )
+ mixed = [pull(1, "org/one", "alice"), pull(2, "org/two", "bob")]
+ self.assertEqual(icons("review", mixed, "search"), ["/org.svg", "/org.svg"])
+
+
+if __name__ == "__main__":
+ unittest.main()
dots/config/raffi/raffi.yaml
@@ -127,9 +127,7 @@ addons:
action: ~/.config/raffi/scripts/yubikey-oath-copy {value}
- name: GitHub Pull Requests
keyword: pr
- command: ~/.config/raffi/scripts/pr-raffi
- args:
- - --color
+ command: ~/.config/raffi/scripts/github/pr.py
icon: github
action: xdg-open {value}
secondary_action: echo -n {value} | wl-copy