Files
gitea-axi/src/comment.ts
alexion 16d2f29f19
All checks were successful
CI / test (pull_request) Successful in 36s
CI / test (push) Successful in 37s
feat: add pr view and checks (task 0009)
Add `pr view <n>` and `pr checks <n>`, built on the truncation machinery
and review fetches from earlier slices.

`pr view` uses the three-call fetch pattern (PR + reviews in parallel, then
the head commit's combined status) so `checks`, `comment_count`, and
`review_count` are in the default output; `--comments`, `--reviews`, and
`--full` behave as on `issue view`, with `--reviews` exposing Gitea's
`official`/`stale` fields plus per-review inline comments.

`pr checks <n>` renders the checks summary line and the `{ name, conclusion }`
rows, or the scalar no-CI message when no statuses exist.

The state→conclusion mapping and summary live in a new `src/checks.ts`;
`commentRows` is extracted into `src/comment.ts` and shared with `issue view`.
2026-07-12 20:22:17 -04:00

68 lines
2.2 KiB
TypeScript

import type { Comment } from "gitea-js";
import { COMMENT_TRUNCATE_LIMIT, truncateBody } from "./body.js";
import type { FlagSpec } from "./flags.js";
import { relativeTime } from "./time.js";
/**
* The one comment-posting shape `issue comment` and `pr comment` share. ADR 0008
* requires both to emit the same `comment` block with the same schema, so the
* block is built in exactly one place; what the two commands genuinely differ on
* — how a missing target is reported, and what to suggest next — stays with them.
*/
export const COMMENT_FLAGS: FlagSpec = {
"--body": { takesValue: true },
"--body-file": { takesValue: true },
"--full": { takesValue: false },
};
export interface CommentItemOptions {
/** The issue or pull request commented on — not the comment's own id. */
number: number;
/** Echo the body untruncated, as `--full` asks. */
full: boolean;
host: string;
now: Date;
}
/** The `comment: { number, author, created, body }` block, body truncated at 800 chars. */
export function commentItem(
comment: Comment,
options: CommentItemOptions,
): Record<string, unknown> {
const body = comment.body ?? "";
return {
number: options.number,
author: comment.user?.login ?? "",
created: relativeTime(comment.created_at, options.now),
body: options.full ? body : truncateBody(body, COMMENT_TRUNCATE_LIMIT, options.host),
};
}
export interface CommentRowsOptions {
host: string;
/** Echo bodies untruncated, as `--full` asks. */
full: boolean;
now: Date;
}
/**
* The `{ author, created, body }` rows for a `--comments` block, shared by
* `issue view` and `pr view`. Each body is cleaned and truncated at 800 chars
* unless `full` is set. Ordered author→created→body, matching the block header
* both commands render.
*/
export function commentRows(
comments: Comment[],
options: CommentRowsOptions,
): Record<string, unknown>[] {
return comments.map((comment) => {
const body = comment.body ?? "";
return {
author: comment.user?.login ?? "",
created: relativeTime(comment.created_at, options.now),
body: options.full ? body : truncateBody(body, COMMENT_TRUNCATE_LIMIT, options.host),
};
});
}