-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeReviewReport.jsx
More file actions
370 lines (355 loc) · 13.3 KB
/
Copy pathCodeReviewReport.jsx
File metadata and controls
370 lines (355 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import { useState } from "react";
import {
Alert,
Card,
Col,
Collapse,
Empty,
Input,
Row,
Select,
Statistic,
Tag,
Typography,
} from "antd";
import Prism from "prismjs";
import "prismjs/components/prism-diff";
import icon from "./favicon.svg";
export const RTIFACT = {
title: "Code Review Report",
icon,
};
const findings = [
{
id: "CR-01",
severity: "Blocking",
file: "src/orders.ts",
line: 84,
title: "Idempotency record is written after the charge",
impact:
"Two concurrent requests can both charge the customer before either request records the shared key.",
evidence:
"The payment call completes before the transaction that inserts the idempotency record.",
recommendation:
"Claim the idempotency key transactionally before calling the payment provider, then return the stored result to duplicates.",
patch: `- const charge = await payments.capture(input);
- await idempotency.save(input.key, charge);
+ const claim = await idempotency.claim(input.key);
+ if (claim.completed) return claim.result;
+ const charge = await payments.capture(input);
+ await idempotency.complete(input.key, charge);`,
},
{
id: "CR-02",
severity: "Blocking",
file: "src/webhook.ts",
line: 41,
title: "Webhook signature is checked after JSON parsing",
impact:
"Parsing changes the signed byte sequence, so valid signatures can fail and untrusted input is processed too early.",
evidence:
"The handler calls request.json() before passing a re-serialized body to signature verification.",
recommendation:
"Read the raw body once, verify it, and only then parse the verified bytes as JSON.",
patch: `- const event = await request.json();
- verifySignature(JSON.stringify(event), signature);
+ const body = await request.text();
+ verifySignature(body, signature);
+ const event = JSON.parse(body);`,
},
{
id: "CR-03",
severity: "Warning",
file: "src/orders.ts",
line: 119,
title: "Provider errors lose their retryability",
impact:
"Every provider failure becomes a generic 500, preventing clients from distinguishing a retryable outage.",
evidence:
"The catch block replaces typed provider errors with Error('Payment failed').",
recommendation:
"Preserve the known timeout code and map it to the existing retryable service-unavailable response.",
patch: `- throw new Error("Payment failed");
+ if (error.code === "PAYMENT_TIMEOUT") throw error;
+ throw new OrderError("PAYMENT_FAILED", { cause: error });`,
},
{
id: "CR-04",
severity: "Warning",
file: "test/orders.test.ts",
line: 132,
title: "The duplicate-request test is sequential",
impact:
"The test proves replay behavior but does not exercise the concurrency window that caused the incident.",
evidence:
"The second request begins only after the first promise has resolved.",
recommendation:
"Start both requests before awaiting either result and assert that the provider receives one capture.",
patch: `- await createOrder(input);
- await createOrder(input);
+ await Promise.all([
+ createOrder(input),
+ createOrder(input),
+ ]);`,
},
{
id: "CR-05",
severity: "Suggestion",
file: "src/orders.ts",
line: 18,
title: "Name the idempotency retention period",
impact:
"The unexplained numeric duration makes the cleanup policy harder to review.",
evidence: "The module passes 86400 directly to the store.",
recommendation:
"Replace the literal with a module-level constant named for the one-day retention policy.",
patch: `+ const IDEMPOTENCY_RETENTION_SECONDS = 24 * 60 * 60;
- await idempotency.expire(key, 86400);
+ await idempotency.expire(key, IDEMPOTENCY_RETENTION_SECONDS);`,
},
];
const severityColor = {
Blocking: "red",
Warning: "gold",
Suggestion: "blue",
};
const files = ["All files", ...new Set(findings.map(({ file }) => file))];
function DiffBlock({ value }) {
const html = Prism.highlight(value, Prism.languages.diff, "diff");
return (
<div>
<div className="mb-2 flex items-center justify-between gap-3">
<Typography.Text strong>Illustrative change</Typography.Text>
<Typography.Text copyable={{ text: value }}>Copy diff</Typography.Text>
</div>
<pre className="language-diff overflow-x-auto" tabIndex={0}>
<code
className="language-diff"
dangerouslySetInnerHTML={{ __html: html }}
/>
</pre>
</div>
);
}
export default function CodeReviewReport() {
const [severity, setSeverity] = useState("All severities");
const [file, setFile] = useState("All files");
const [query, setQuery] = useState("");
const visibleFindings = findings.filter((finding) => {
const matchesSeverity =
severity === "All severities" || finding.severity === severity;
const matchesFile = file === "All files" || finding.file === file;
const searchable =
`${finding.id} ${finding.file} ${finding.title} ${finding.impact}`.toLowerCase();
return (
matchesSeverity &&
matchesFile &&
searchable.includes(query.trim().toLowerCase())
);
});
const counts = Object.fromEntries(
["Blocking", "Warning", "Suggestion"].map((name) => [
name,
findings.filter(({ severity: value }) => value === name).length,
]),
);
return (
<main className="min-h-screen">
<header className="border-b border-border bg-card px-5 py-4 sm:px-8">
<div className="mx-auto flex max-w-7xl flex-wrap items-center justify-between gap-3">
<div>
<Typography.Text
type="secondary"
className="font-mono text-xs uppercase tracking-[0.16em]"
>
Sample review · checkout/idempotency
</Typography.Text>
<div className="mt-1 font-semibold">Pull request review</div>
</div>
<Tag color="red">Changes requested</Tag>
</div>
</header>
<div className="mx-auto grid max-w-7xl gap-8 px-5 py-8 lg:grid-cols-[210px_minmax(0,1fr)] lg:px-8">
<aside className="lg:sticky lg:top-6 lg:self-start">
<nav aria-label="Review sections">
<Typography.Text strong>On this page</Typography.Text>
<ol className="mt-3 grid gap-1 text-sm text-muted-foreground">
{[
["verdict", "Verdict"],
["findings", "Findings"],
["strengths", "What is working"],
["method", "Review scope"],
].map(([id, label]) => (
<li key={id}>
<a
href={`#${id}`}
className="block rounded-md px-3 py-2 hover:bg-card hover:text-foreground"
>
{label}
</a>
</li>
))}
</ol>
</nav>
<Card size="small" className="mt-6">
<Typography.Text strong>Next action</Typography.Text>
<Typography.Paragraph
type="secondary"
className="mb-0 mt-2 text-sm leading-6"
>
Resolve CR-01 and CR-02, add the concurrent regression test, then
request another review.
</Typography.Paragraph>
</Card>
</aside>
<article className="min-w-0">
<section id="verdict" aria-labelledby="verdict-heading">
<Typography.Title id="verdict-heading">
Do not merge until two blockers are resolved
</Typography.Title>
<Typography.Paragraph className="max-w-3xl text-lg leading-8">
The change improves replay handling, but it still permits
duplicate charges under concurrency and verifies webhooks too
late.
</Typography.Paragraph>
<Alert
type="warning"
showIcon
title="Changes requested"
description="Fix both blocking findings and protect the concurrency path with a regression test. Warnings may follow in the same patch."
/>
<Row gutter={[16, 16]} className="mt-5">
<Col xs={12} md={6}>
<Card>
<Statistic title="Findings" value={findings.length} />
</Card>
</Col>
{["Blocking", "Warning", "Suggestion"].map((name) => (
<Col key={name} xs={12} md={6}>
<Card>
<Statistic
title={name}
value={counts[name]}
styles={{
content: {
color:
name === "Blocking"
? "var(--color-danger)"
: undefined,
},
}}
/>
</Card>
</Col>
))}
</Row>
</section>
<section
id="findings"
aria-labelledby="findings-heading"
className="mt-10"
>
<Typography.Title id="findings-heading" level={2}>
Findings and suggested fixes
</Typography.Title>
<Typography.Paragraph type="secondary" className="max-w-3xl">
Filter by severity or file, then expand a finding for its evidence
and smallest suggested correction.
</Typography.Paragraph>
<div className="mb-5 grid gap-3 md:grid-cols-[170px_220px_minmax(220px,1fr)]">
<Select
aria-label="Filter findings by severity"
value={severity}
onChange={setSeverity}
options={["All severities", ...Object.keys(severityColor)].map(
(value) => ({ value, label: value }),
)}
/>
<Select
aria-label="Filter findings by file"
value={file}
onChange={setFile}
options={files.map((value) => ({ value, label: value }))}
/>
<Input
aria-label="Search review findings"
allowClear
placeholder="Search finding, file, or impact"
value={query}
onChange={(event) => setQuery(event.target.value)}
/>
</div>
{visibleFindings.length ? (
<Collapse
defaultActiveKey={["CR-01", "CR-02"]}
items={visibleFindings.map((finding) => ({
key: finding.id,
label: (
<span className="flex flex-wrap items-center gap-2">
<Tag color={severityColor[finding.severity]}>
{finding.severity}
</Tag>
<strong>{finding.title}</strong>
<Typography.Text type="secondary" className="font-mono">
{finding.file}:{finding.line}
</Typography.Text>
</span>
),
children: (
<div>
<dl className="mb-5 grid gap-3 sm:grid-cols-[7rem_1fr]">
<dt className="font-semibold">Impact</dt>
<dd className="mb-0">{finding.impact}</dd>
<dt className="font-semibold">Evidence</dt>
<dd className="mb-0">{finding.evidence}</dd>
<dt className="font-semibold">Fix</dt>
<dd className="mb-0">{finding.recommendation}</dd>
</dl>
<DiffBlock value={finding.patch} />
</div>
),
}))}
/>
) : (
<Empty description="No findings match these filters" />
)}
</section>
<section
id="strengths"
aria-labelledby="strengths-heading"
className="mt-10"
>
<Typography.Title id="strengths-heading" level={2}>
What is working
</Typography.Title>
<ul className="max-w-3xl space-y-2 leading-7">
<li>The public handler contract remains unchanged.</li>
<li>
Known replay responses preserve their original status code.
</li>
<li>
New test names describe customer-visible behavior instead of
implementation details.
</li>
</ul>
</section>
<section
id="method"
aria-labelledby="method-heading"
className="mt-10"
>
<Typography.Title id="method-heading" level={2}>
Review scope and limitations
</Typography.Title>
<Typography.Paragraph className="max-w-3xl leading-7">
This is an illustrative report with fictional files and findings.
A real review should name its revision, requirements, executed
checks, and unreviewed areas. Suggested diffs require validation
in the actual repository before use.
</Typography.Paragraph>
</section>
</article>
</div>
</main>
);
}