Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions apps/server/src/fork/reviewRules/findings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
htmlUnescape,
isBlank,
pyI,
pyUnicodeCheck,
splitlines,
strip,
stripChars,
Expand Down Expand Up @@ -60,6 +61,7 @@ const blocking = (kind: Kind): kind is FindingKind => kind !== "minor";

/** The text as the findings rules read it. */
export function fold(text: string | null | undefined): string {
pyUnicodeCheck(text ?? "");
const t = htmlUnescape((text ?? "").normalize("NFKC"))
.replace(/\\([!-/:-@[-`{-~])/gu, "$1")
.replace(/\p{Cf}/gu, "");
Expand Down Expand Up @@ -186,6 +188,7 @@ function read(lines: ReadonlyArray<string>, narrow: boolean): Read {
* or the title under it) or a `verdict:` line that says NOT SAFE is a blocker whatever the labels say.
*/
export function readFindings(body: string): [Array<[FindingKind, string]>, boolean] {
pyUnicodeCheck(body);
const lines = splitlines(body);
const found = new Map<number, [FindingKind, string]>();
const seen = new Set<Kind>();
Expand Down
96 changes: 84 additions & 12 deletions apps/server/src/fork/reviewRules/lensComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@
* vectors and the gate's lens corpus. A comment that only looks like a report, or holds a second header, is a
* NO_HEADER entry that reads nothing, as in the gate. The caller picks the posts that count, in the gate's order.
*/
import * as Schema from "effect/Schema";

import {
ALNUM,
B,
S,
WS,
byCodePoint,
cut,
isBlank,
PyTextError,
pyI,
pyUnicodeCheck,
repr,
split,
splitlines,
strip,
stripChars,
} from "./pyText.ts";

const isPyTextError = Schema.is(PyTextError);
const TAG_OPEN = /<\/?[A-Za-z]/gu;
const TAG_NARROW = /<\/?[A-Za-z][^<>]*>/gu;
const LINK_NARROW = /!?\[([^[\]]*)\]\([^()]*\)/gu;
Expand Down Expand Up @@ -54,6 +60,8 @@ const TOP_LINES = 5;
export const NO_HEADER = "comment without a first-line lens header";
export const EDITED = "edited after posting: post a new comment instead";
export const HIDDEN = "hidden: post a new comment instead";
const FLAGS_UNREAD =
"a review whose edit and hidden flags were not read from GitHub: re-run, or post a new comment";
export const EMBEDDED = "holds a lens header line";
const OPT_OUT_READ =
"says on its first line that it is no lens report, but holds a hex read-at sha outside a `>` quote";
Expand Down Expand Up @@ -110,6 +118,7 @@ const opensPlain = (line: string) => !/^(?: {4}|\t| {0,3}(?:>|`{3}|~{3}))/u.test

/** One key per lens across rounds: the name lower-case, without a trailing round marker or emphasis. */
export function lensKey(name: string): string {
pyUnicodeCheck(name);
const words = split(name.toLowerCase());
let lo = 0;
let hi = words.length;
Expand Down Expand Up @@ -140,6 +149,7 @@ export function namesALens(name: string): boolean {

/** [name, sha or null] of a comment's first-line lens header, or null. */
export function lensHeader(body: string, narrow = false): [string, string | null] | null {
pyUnicodeCheck(body);
const first = splitlines(body).find((line) => !isBlank(line)) ?? "";
if (!opensPlain(first)) return null;
const line = normLine(first, narrow);
Expand Down Expand Up @@ -241,6 +251,7 @@ function readProblem(body: string, narrow: boolean): string | null {
* never counts.
*/
export function lensProblem(body: string, overridePrefix?: string): string | null {
pyUnicodeCheck(body);
if (
overridePrefix !== undefined &&
body.startsWith(`${overridePrefix} `) &&
Expand All @@ -260,15 +271,44 @@ export function lensProblem(body: string, overridePrefix?: string): string | nul
return problem;
}

/** A pull request comment or review whose author counts (a lens author association, no bot), in posting order. */
/**
* A pull request comment, review or inline review comment whose author counts (a lens author association, no bot),
* in the gate's list order (lens.Review.lens_posts): the comments, then the reviews, then the inline comments.
*/
export interface LensPost {
readonly body: string;
/** GitHub's includesCreatedEdit: anything but a recorded false is an edit. */
readonly edited: boolean;
/** GitHub's isMinimized. */
readonly hidden: boolean;
/** How the gate names the post in a NO_HEADER key: its url, else `by <login> at <createdAt>`. Default `post <n>`. */
readonly where?: string;
/**
* How the gate names the post in a NO_HEADER key: its url, else `by <login> at <when or ?>`. A key holds the post's
* text, so a caller never matches on a key's text: it reads `NO_HEADER` entries by their prefix only.
*/
readonly where: string;
/** createdAt of a comment or inline comment, submittedAt of a review, or "": with the list index, the post's age. */
readonly when: string;
/** A review whose edit and hidden flags were not read (lens.UNREAD): it reads nothing (FLAGS_UNREAD). */
readonly unread?: boolean;
}

/** A post's age as the gate orders it: [when, list index], `when` by code point first. */
export type PostAt = readonly [when: string, index: number];

/** a is older than b (Python's tuple `<`). */
export const olderThan = (a: PostAt, b: PostAt): boolean => {
const by = byCodePoint(a[0], b[0]);
return by < 0 || (by === 0 && a[1] < b[1]);
};

export interface LensEntry {
readonly at: PostAt;
readonly sha: string | null;
readonly why: string | null;
/** The lens name of its newest header; null for a NO_HEADER entry. */
readonly name: string | null;
/** The post's body: review_judge reads the newest report's findings from it. */
readonly body: string;
}

export interface LensScan {
Expand All @@ -278,22 +318,54 @@ export interface LensScan {
readonly entries: Record<string, [string | null, string | null]>;
}

/** lens.Review.lens_scan on posts that count, with the repository's override prefix. */
export function lensScan(posts: ReadonlyArray<LensPost>, overridePrefix?: string): LensScan {
/**
* lens.Review.lens_scan with each entry's age: the newest post per lens key by [when, index], `index` the post's place
* in `posts`. `skip` leaves a post out (an override) but keeps its index. History stays in list order. A post the
* rules cannot read as Python does throws PyTextError naming that post (`where`).
*/
export function lensEntries(
posts: ReadonlyArray<LensPost>,
overridePrefix?: string,
skip: (post: LensPost) => boolean = () => false,
): { history: Array<[string, string | null]>; entries: Map<string, LensEntry> } {
const history: Array<[string, string | null]> = [];
const entries: Record<string, [string | null, string | null]> = {};
const entries = new Map<string, LensEntry>();
posts.forEach((post, i) => {
const problem = lensProblem(post.body, overridePrefix);
if (skip(post)) return;
const at: PostAt = [post.when, i];
const { body } = post;
const hidden = !post.unread && post.hidden;
let problem: string | null;
let header: [string, string | null] | null;
let key: string;
try {
problem = lensProblem(body, overridePrefix);
header = problem ? null : lensHeader(body);
key = header ? lensKey(header[0]) : "";
} catch (error) {
if (isPyTextError(error) && error.where === undefined)
throw new PyTextError({ codePoints: error.codePoints, where: post.where });
throw error;
}
if (problem) {
const where = post.where ?? `post ${i + 1}`;
entries[`${NO_HEADER} (${where}, ${problem}${post.hidden ? ", hidden" : ""})`] = [null, null];
key = `${NO_HEADER} (${post.where}, ${problem}${hidden ? ", hidden" : ""})`;
entries.set(key, { at, sha: null, why: null, name: null, body });
return;
}
const header = lensHeader(post.body);
if (!header) return;
history.push(header);
const why = post.hidden ? HIDDEN : post.edited ? EDITED : null;
entries[lensKey(header[0])] = why ? [null, why] : [header[1], null];
const was = entries.get(key);
if (was && olderThan(at, was.at)) return;
const why = post.unread ? FLAGS_UNREAD : hidden ? HIDDEN : post.edited ? EDITED : null;
entries.set(key, { at, sha: why ? null : header[1], why, name: header[0], body });
});
return { history, entries };
}

/** lens.Review.lens_scan on posts that count, with the repository's override prefix. */
export function lensScan(posts: ReadonlyArray<LensPost>, overridePrefix?: string): LensScan {
const { history, entries } = lensEntries(posts, overridePrefix);
const out: Record<string, [string | null, string | null]> = {};
for (const [key, { sha, why }] of entries) out[key] = [sha, why];
return { history, entries: out };
}
Loading
Loading