|
| 1 | +--- |
| 2 | +name: pr-review-canvas |
| 3 | +disable-model-invocation: true |
| 4 | +description: Generate an interactive PR review walkthrough as an HTML page. Fetches PR data via gh API, categorizes files into core vs mechanical changes, adds reviewer annotations, and renders diffs with moved-code detection. Use when the user pastes a GitHub PR URL and asks for a review, walkthrough, or summary, or says "review this PR". |
| 5 | +--- |
| 6 | + |
| 7 | +# PR Review Canvas |
| 8 | + |
| 9 | +Generate an interactive HTML review of a GitHub PR that reads like a peer walking you through what matters. |
| 10 | + |
| 11 | +## Workflow |
| 12 | + |
| 13 | +### 1. Fetch PR data |
| 14 | + |
| 15 | +Run these `gh api` calls in parallel: |
| 16 | + |
| 17 | +```bash |
| 18 | +gh api repos/{owner}/{repo}/pulls/{number} --jq '{title, body, user: .user.login, state, additions, deletions, changed_files, base: .base.ref, head: .head.ref}' |
| 19 | +gh api repos/{owner}/{repo}/pulls/{number}/files --paginate --jq '.[] | {filename, status, additions, deletions, patch}' |
| 20 | +gh api repos/{owner}/{repo}/pulls/{number}/comments --jq '.[] | {user: .user.login, body, path, line}' |
| 21 | +``` |
| 22 | + |
| 23 | +### 2. Analyze the PR and write the body HTML |
| 24 | + |
| 25 | +Read the diffs, understand the PR, and write the `<body>` content directly as HTML. You have full creative freedom -- the goal is to explain the PR clearly to a reviewer. Use whatever structure best fits the PR. |
| 26 | + |
| 27 | +**Typical structure** (adapt as needed): |
| 28 | +- Header with title, PR number, author, stats |
| 29 | +- Summary box explaining what the PR does in plain English |
| 30 | +- Core file sections with annotations and diffs |
| 31 | +- Mechanical/boilerplate files collapsed by default |
| 32 | +- Review checklist at the bottom |
| 33 | + |
| 34 | +**But you can also add:** |
| 35 | +- **Pseudocode summaries** for verbose code -- show the algorithm in plain English or short pseudocode, with the real diff collapsed below (use a `.bp-section` card labeled "Show full implementation"). Great when 150 lines of retry/backoff/error-handling code is really just "fetch with exponential backoff and circuit breaker." |
| 36 | +- Diagrams (inline SVG, mermaid via CDN, ASCII art in `<pre>`) |
| 37 | +- Flowcharts showing before/after control flow |
| 38 | +- Tables comparing old vs new behavior |
| 39 | +- Callout boxes for warnings, questions, or gotchas |
| 40 | +- Interactive widgets if they help |
| 41 | +- Anything else that makes the review clearer |
| 42 | + |
| 43 | +**Pseudocode pattern example:** |
| 44 | +```html |
| 45 | +<div class="file-card"> |
| 46 | + <div class="file-hdr" onclick="toggle(this)"> |
| 47 | + <span class="fname">retryClient.ts</span> |
| 48 | + <div class="fstats"><span class="pill add">+173</span><span class="pill del">−11</span><span class="chev open">▶</span></div> |
| 49 | + </div> |
| 50 | + <div class="file-body open"> |
| 51 | + <div class="file-note"> |
| 52 | + <strong>What this does in plain English:</strong> |
| 53 | + <pre style="margin-top:8px;color:var(--text);font-size:12px;line-height:1.6;"> |
| 54 | +fetch(url): |
| 55 | + if circuit breaker is open → fail fast |
| 56 | + retry up to N times: |
| 57 | + try fetch with timeout |
| 58 | + on success → close circuit breaker, return |
| 59 | + on retryable error → wait (exponential backoff + jitter) |
| 60 | + on non-retryable error → throw |
| 61 | + circuit breaker records failure</pre> |
| 62 | + </div> |
| 63 | + <div class="bp-section" style="margin:0;border:0;border-radius:0;"> |
| 64 | + <div class="bp-hdr" onclick="toggleBP(this)"> |
| 65 | + <span>Show full implementation (+173 lines)</span><span class="chev">▶</span> |
| 66 | + </div> |
| 67 | + <div class="bp-body"><div data-diff="retryClient"></div></div> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | +</div> |
| 71 | +``` |
| 72 | + |
| 73 | +### 3. Available CSS classes and JS utilities |
| 74 | + |
| 75 | +Read [styles.css](styles.css) and [renderer.js](renderer.js) from this skill directory. These give you a prebuilt dark-themed toolkit. Inject them into [template.html](template.html) verbatim. |
| 76 | + |
| 77 | +**CSS classes you can use:** |
| 78 | + |
| 79 | +| Class | Purpose | |
| 80 | +|-------|---------| |
| 81 | +| `.header`, `.header h1`, `.header-meta` | Page header | |
| 82 | +| `.pill.add`, `.pill.del`, `.pill.files` | Stat badges (+N, -N, N files) | |
| 83 | +| `.content` | Centered content wrapper (max 900px) | |
| 84 | +| `.summary` | Summary/TL;DR box | |
| 85 | +| `.section-title` | Section heading with bottom border | |
| 86 | +| `.ic` | Inline code reference (mono, blue, dark bg) | |
| 87 | +| `.file-card`, `.file-hdr`, `.file-body` | Collapsible file card (use `onclick="toggle(this)"` on `.file-hdr`) | |
| 88 | +| `.file-note` | Sticky reviewer annotation inside a file card | |
| 89 | +| `.bp-section`, `.bp-hdr`, `.bp-body` | Collapsed boilerplate card (use `onclick="toggleBP(this)"`) | |
| 90 | +| `.bp-note` | Note inside a boilerplate card | |
| 91 | +| `.verdict` | Review checklist box | |
| 92 | + |
| 93 | +**JS functions available:** |
| 94 | + |
| 95 | +| Function | Usage | |
| 96 | +|----------|-------| |
| 97 | +| `toggle(hdrElement)` | Toggle a `.file-body` open/closed | |
| 98 | +| `toggleBP(hdrElement)` | Toggle a `.bp-body` open/closed | |
| 99 | +| `renderDiff(target, diffInput)` | Render a unified diff. `target` can be a DOM element, string ID, or CSS selector. `diffInput` can be a raw patch string OR an array of lines -- both work. Automatically filters imports, collapses whitespace-only changes, detects moved code (blue/purple tint). | |
| 100 | +| `esc(string)` | HTML-escape a string | |
| 101 | + |
| 102 | +**Rendering diffs -- use `data-diff` attributes with auto-discovery.** |
| 103 | +Put `<div data-diff="KEY"></div>` placeholders in your body HTML wherever you want a diff rendered. The renderer finds them automatically after DOM load and fills them from the `<script id="pr-diffs-json" type="application/json">` element in `template.html`. |
| 104 | + |
| 105 | +**CRITICAL: Patch strings can contain `</script>` in addition to newlines, backslashes, and quotes.** Even `json.dumps(...)` is not enough if you paste raw output into executable `<script>` because HTML parsing can terminate the tag early. Never manually embed patch strings in JS/JSON. Instead, use this safe approach: |
| 106 | + |
| 107 | +1. During the fetch step, save patches to a JSON file using `jq` (which handles escaping correctly): |
| 108 | +```bash |
| 109 | +gh api repos/{owner}/{repo}/pulls/{number}/files --paginate \ |
| 110 | + --jq '[.[] | {key: (.filename | gsub("[^a-zA-Z0-9]"; "_")), value: (.patch // "")}] | from_entries' \ |
| 111 | + > /tmp/pr-patches-{number}.json |
| 112 | +``` |
| 113 | + |
| 114 | +2. During assembly, use Python to safely inject the JSON into `template.html`: |
| 115 | +```bash |
| 116 | +python3 <<'PY' |
| 117 | +import json |
| 118 | +from pathlib import Path |
| 119 | +
|
| 120 | +patches = json.loads(Path('/tmp/pr-patches-{number}.json').read_text()) |
| 121 | +html = Path('/tmp/pr-review-{number}-body.html').read_text() |
| 122 | +css = Path('styles.css').read_text() |
| 123 | +js = Path('renderer.js').read_text() |
| 124 | +tmpl = Path('template.html').read_text() |
| 125 | +
|
| 126 | +# Prevent literal </script> from terminating HTML script tags early. |
| 127 | +safe_json = json.dumps(patches).replace('<', '\\u003c').replace('>', '\\u003e').replace('&', '\\u0026') |
| 128 | +
|
| 129 | +out = ( |
| 130 | + tmpl.replace('/* INJECT_CSS */', css) |
| 131 | + .replace('/* INJECT_JS */', js) |
| 132 | + .replace('<!-- INJECT_BODY -->', html) |
| 133 | + .replace('{"__PR_DIFFS_PLACEHOLDER__":true}', safe_json) |
| 134 | +) |
| 135 | +
|
| 136 | +Path('/tmp/pr-review-{number}.html').write_text(out) |
| 137 | +PY |
| 138 | +``` |
| 139 | + |
| 140 | +This guarantees valid JSON and script-safe HTML embedding. The agent writes body HTML to a temp file, then Python assembles everything safely. |
| 141 | + |
| 142 | +The diff data keys should match the `data-diff` attribute values in the HTML: |
| 143 | +```html |
| 144 | +<div data-diff="path_to_file_ts"></div> |
| 145 | +``` |
| 146 | + |
| 147 | +Since renderer.js loads in `<head>`, you can also call `renderDiff(target, lines)` directly from inline `<script>` tags if needed for custom use cases. The function accepts a DOM element, ID string, or CSS selector as `target`, and a string or array as `lines`. |
| 148 | + |
| 149 | +**You're not limited to these.** Add your own inline `<style>` blocks, `<script>` blocks, SVGs, diagrams, or anything else. The prebuilt pieces save time but don't constrain you. |
| 150 | + |
| 151 | +### 4. Assemble and serve |
| 152 | + |
| 153 | +1. Write your body HTML (everything that goes inside `<body>`) to `/tmp/pr-review-{number}-body.html` |
| 154 | +2. Save patches to `/tmp/pr-patches-{number}.json` using the `jq` command from step 3 above |
| 155 | +3. Run the Python assembly script from step 3 above (reads styles.css, renderer.js, template.html from this skill directory, injects body + patches safely, writes final HTML) |
| 156 | +4. Start a local server on a fixed port: |
| 157 | + ```bash |
| 158 | + cd /tmp && python3 -m http.server 8432 --bind 127.0.0.1 |
| 159 | + ``` |
| 160 | + Run this backgrounded, then navigate the in-app browser to `http://127.0.0.1:8432/pr-review-{number}.html`. |
| 161 | + |
| 162 | + **Why a fixed port and `cd /tmp`:** Background shells have no TTY, so Python buffers its startup message ("Serving HTTP on...") indefinitely — using port 0 means you can never read which port was chosen. And `--directory /tmp` works but `cd /tmp` is more robust across Python versions. If port 8432 is taken, try 8433, 8434, etc. |
| 163 | + |
| 164 | +### Diff features (handled automatically by renderer.js) |
| 165 | + |
| 166 | +- Filters out import-only lines |
| 167 | +- Collapses whitespace-only changes into context lines |
| 168 | +- Detects moved code blocks (3+ consecutive lines deleted in one place and added identically elsewhere) -- renders in blue/purple instead of red/green |
| 169 | +- Near-matches (moved + small edit) get a different purple tint |
| 170 | + |
| 171 | +### Style notes |
| 172 | + |
| 173 | +- Dark theme: `#1a1a1a` background, Inter body font, IBM Plex Mono for code |
| 174 | +- Use `var(--warning)` for orange, `var(--success)` for green, `var(--danger)` for red, `var(--accent)` for blue |
| 175 | +- Sticky file headers (`position: sticky; top: 0`) and notes (`top: 35px`) pin while scrolling |
| 176 | +- Core files expanded by default (`.file-body.open`), mechanical files collapsed |
0 commit comments