mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
feat(viewers): outlines for Markdown and gists, refined pull request page
Markdown: the "On this page" outline covers h1-h3, draws redaction slots like the body, treats a lone opening h1 as the title, highlights the section past the reading line, and shows its menu from 768 to 1199px too. Gists: one card per file and an outline of the files (name, language, lines) with Markdown headings nested under each file; sticky right rail on wide screens, an "In this gist" menu on narrow ones. Pull requests: header with state, anonymized author, branches and commit count; Conversation, Commits and Files changed tabs; a file list with +/- counts, per-file collapse, inline review threads, and redaction slots in the title, body and diff. The server now stores the author, branch names, commits (no SHAs) and review comments, grouped into threads; older pull requests render without them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0640d741f1
commit
176bc857a1
22 files changed
+1515
-295
No files matched your search
+107
-1
@@ -17,6 +17,29 @@ import { createLogger } from "./logger";
|
||||
|
||||
const logger = createLogger("pull-request");
|
||||
|
||||
type ReviewComment = NonNullable<IAnonymizedPullRequestDocument["pullRequest"]["reviewComments"]>[number];
|
||||
|
||||
/**
|
||||
* Group inline review comments into threads: each reply joins the thread of the
|
||||
* comment it answers. Threads keep the order of their first comment; GitHub
|
||||
* comment ids never leave the server.
|
||||
*/
|
||||
export function reviewThreads(comments: ReviewComment[]) {
|
||||
const threads: { path: string; line?: number; comments: ReviewComment[] }[] = [];
|
||||
const byId = new Map<number, (typeof threads)[number]>();
|
||||
const sorted = [...comments].sort((a, b) => new Date(a.creationDate).getTime() - new Date(b.creationDate).getTime());
|
||||
for (const comment of sorted) {
|
||||
let thread = comment.inReplyTo != null ? byId.get(comment.inReplyTo) : undefined;
|
||||
if (!thread) {
|
||||
thread = { path: comment.path, line: comment.line ?? undefined, comments: [] };
|
||||
threads.push(thread);
|
||||
}
|
||||
thread.comments.push(comment);
|
||||
if (comment.commentId != null) byId.set(comment.commentId, thread);
|
||||
}
|
||||
return threads;
|
||||
}
|
||||
|
||||
export default class PullRequest {
|
||||
private _model: IAnonymizedPullRequestDocument;
|
||||
owner: User;
|
||||
@@ -101,7 +124,40 @@ export default class PullRequest {
|
||||
mediaType: { format: "diff" },
|
||||
});
|
||||
|
||||
const [comments, diff] = await Promise.all([commentsPromise, diffPromise]);
|
||||
// Commits and inline review comments are optional extras: a failure keeps
|
||||
// the pull request readable without them.
|
||||
const optional = <T>(label: string, promise: Promise<T[]>) =>
|
||||
promise.catch((err): T[] => {
|
||||
logger.warn(`PR ${label} unavailable, continuing without them`, {
|
||||
code: `pr_${label.replace(/ /g, "_")}_unavailable`,
|
||||
httpStatus: (err as { status?: number }).status,
|
||||
pr: `${owner}/${repo}#${pull_number}`,
|
||||
});
|
||||
return [];
|
||||
});
|
||||
const commitsPromise = optional<{
|
||||
commit?: { message?: string; author?: { date?: string } | null };
|
||||
author?: { login?: string } | null;
|
||||
}>("commits", oct.paginate("GET /repos/{owner}/{repo}/pulls/{pull_number}/commits", {
|
||||
owner, repo, pull_number, per_page: 100,
|
||||
}));
|
||||
const reviewCommentsPromise = optional<{
|
||||
id: number;
|
||||
in_reply_to_id?: number;
|
||||
path?: string;
|
||||
line?: number | null;
|
||||
original_line?: number | null;
|
||||
body?: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
user?: { login?: string } | null;
|
||||
}>("review comments", oct.paginate("GET /repos/{owner}/{repo}/pulls/{pull_number}/comments", {
|
||||
owner, repo, pull_number, per_page: 100,
|
||||
}));
|
||||
|
||||
const [comments, diff, commits, reviewComments] = await Promise.all([
|
||||
commentsPromise, diffPromise, commitsPromise, reviewCommentsPromise,
|
||||
]);
|
||||
|
||||
this._model.pullRequest = {
|
||||
diff: diff.data as unknown as string,
|
||||
@@ -117,6 +173,24 @@ export default class PullRequest {
|
||||
state: prInfo.data.state,
|
||||
baseRepositoryFullName: prInfo.data.base.repo.full_name,
|
||||
headRepositoryFullName: prInfo.data.head.repo?.full_name,
|
||||
author: prInfo.data.user?.login || "",
|
||||
baseRef: prInfo.data.base.ref,
|
||||
headRef: prInfo.data.head.ref,
|
||||
commits: commits.map((c) => ({
|
||||
message: c.commit?.message || "",
|
||||
author: c.author?.login || "",
|
||||
date: c.commit?.author?.date ? new Date(c.commit.author.date) : undefined,
|
||||
})),
|
||||
reviewComments: reviewComments.map((c) => ({
|
||||
commentId: c.id,
|
||||
inReplyTo: c.in_reply_to_id,
|
||||
path: c.path || "",
|
||||
line: c.line ?? c.original_line ?? undefined,
|
||||
body: c.body || "",
|
||||
author: c.user?.login || "",
|
||||
creationDate: new Date(c.created_at),
|
||||
updatedDate: new Date(c.updated_at),
|
||||
})),
|
||||
comments: comments.map((comment) => ({
|
||||
body: comment.body || "",
|
||||
creationDate: new Date(comment.created_at),
|
||||
@@ -362,8 +436,40 @@ export default class PullRequest {
|
||||
return o;
|
||||
});
|
||||
}
|
||||
if (this.options.username && this._model.pullRequest.author) {
|
||||
output.author = anonymizer.anonymize(this._model.pullRequest.author);
|
||||
}
|
||||
// Branch names are labels like the title: shown through the anonymizer.
|
||||
if (this._model.pullRequest.baseRef) output.baseRef = anonymizer.anonymize(this._model.pullRequest.baseRef);
|
||||
if (this._model.pullRequest.headRef) output.headRef = anonymizer.anonymize(this._model.pullRequest.headRef);
|
||||
if (this.options.diff) {
|
||||
output.diff = anonymizer.anonymize(this._model.pullRequest.diff);
|
||||
// Pull requests downloaded before commits were stored have none.
|
||||
if (this._model.pullRequest.commits?.length) {
|
||||
// No SHAs: a commit hash identifies the source repository.
|
||||
output.commits = this._model.pullRequest.commits.map((commit) => {
|
||||
const o: Record<string, unknown> = { message: anonymizer.anonymize(commit.message || "") };
|
||||
if (this.options.username) o.author = anonymizer.anonymize(commit.author || "");
|
||||
if (this.options.date) o.date = commit.date;
|
||||
return o;
|
||||
});
|
||||
}
|
||||
}
|
||||
if (this.options.comments && this._model.pullRequest.reviewComments?.length) {
|
||||
output.reviewComments = reviewThreads(this._model.pullRequest.reviewComments).map((thread) => ({
|
||||
path: anonymizer.anonymize(thread.path),
|
||||
line: thread.line,
|
||||
comments: thread.comments.map((comment) => {
|
||||
const o: Record<string, unknown> = {};
|
||||
if (this.options.body) o.body = anonymizer.anonymize(comment.body);
|
||||
if (this.options.username) o.author = anonymizer.anonymize(comment.author);
|
||||
if (this.options.date) {
|
||||
o.updatedDate = comment.updatedDate;
|
||||
o.creationDate = comment.creationDate;
|
||||
}
|
||||
return o;
|
||||
}),
|
||||
}));
|
||||
}
|
||||
if (this.options.origin) {
|
||||
output.baseRepositoryFullName =
|
||||
|
||||
@@ -59,6 +59,30 @@ const AnonymizedPullRequestSchema = new Schema({
|
||||
state: String,
|
||||
baseRepositoryFullName: String,
|
||||
headRepositoryFullName: String,
|
||||
author: String,
|
||||
baseRef: String,
|
||||
headRef: String,
|
||||
commits: [
|
||||
{
|
||||
_id: false,
|
||||
message: String,
|
||||
author: String,
|
||||
date: Date,
|
||||
},
|
||||
],
|
||||
reviewComments: [
|
||||
{
|
||||
_id: false,
|
||||
commentId: Number,
|
||||
inReplyTo: Number,
|
||||
path: String,
|
||||
line: Number,
|
||||
body: String,
|
||||
author: String,
|
||||
creationDate: Date,
|
||||
updatedDate: Date,
|
||||
},
|
||||
],
|
||||
comments: [
|
||||
{
|
||||
body: String,
|
||||
|
||||
@@ -45,6 +45,24 @@ export interface IAnonymizedPullRequest {
|
||||
state?: string;
|
||||
baseRepositoryFullName?: string;
|
||||
headRepositoryFullName?: string;
|
||||
author?: string;
|
||||
baseRef?: string;
|
||||
headRef?: string;
|
||||
commits?: {
|
||||
message: string;
|
||||
author: string;
|
||||
date?: Date;
|
||||
}[];
|
||||
reviewComments?: {
|
||||
commentId: number;
|
||||
inReplyTo?: number;
|
||||
path: string;
|
||||
line?: number;
|
||||
body: string;
|
||||
author: string;
|
||||
creationDate: Date;
|
||||
updatedDate: Date;
|
||||
}[];
|
||||
comments?: {
|
||||
body: string;
|
||||
creationDate: Date;
|
||||
|
||||
Reference in new issue
Block a user