Merge pull request 'Design 47 H1: the forge answers not-found for a missing directory in a trees listing (H-A-232)' (#724) from fix/47-H1-gitea-trees-not-found into main
This commit was merged in pull request #724.
This commit is contained in:
+10
-1
@@ -182,6 +182,15 @@ export const FORGE_REQUEST_TIMEOUT_MS = 20_000;
|
||||
/** The most a tool's answer carries of one file: the bus's message bound is 1 MiB and a file travels as text (issue 1577). */
|
||||
export const MAX_FILE_BYTES = 512 * 1024;
|
||||
|
||||
/** A directory of a listing's `files` the commit does not hold: the seat git answers it with the kind not-found, the error
|
||||
* naming the directory (seat.ts failure; novox/hq design 47 H H-A-232). */
|
||||
export class DirectoryNotInTree extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "DirectoryNotInTree";
|
||||
}
|
||||
}
|
||||
|
||||
/** One request to the forge, cut at the limit and failing loud naming the request. */
|
||||
async function timedFetch(url: string, init: RequestInit, label: string, timeoutMs: number): Promise<Response> {
|
||||
try {
|
||||
@@ -634,7 +643,7 @@ export class GiteaClient {
|
||||
// is whole (a cut one failed above), so a directory absent from it is absent at the commit.
|
||||
filesOf = {};
|
||||
for (const dir of files) {
|
||||
if (dir !== "" && dirOf.get(dir) === undefined) throw new Error(`the directory ${dir} is not in ${owner}/${repo} at ${sha}`);
|
||||
if (dir !== "" && dirOf.get(dir) === undefined) throw new DirectoryNotInTree(`the directory ${dir} is not in ${owner}/${repo} at ${sha}`);
|
||||
const prefix = dir === "" ? "" : `${dir}/`;
|
||||
const blobs: Record<string, string> = {};
|
||||
for (const e of entries) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import {
|
||||
type TagAnswer, type TagsAnswer, type TreesAnswer, type ViewAnswer,
|
||||
} from "@novox/mesh-delivery-contracts-git";
|
||||
import type { CommitStatus, GiteaClient, GiteaComment, GiteaPull } from "./client.js";
|
||||
import { MAX_FILE_BYTES } from "./client.js";
|
||||
import { DirectoryNotInTree, MAX_FILE_BYTES } from "./client.js";
|
||||
import { containedFrom, newestByContext } from "./checks.js";
|
||||
import { Refused, checkName } from "./guard.js";
|
||||
import { appendNote, viewBody, viewComment, type Runner } from "./delivery.js";
|
||||
@@ -64,6 +64,9 @@ export function failure(err: unknown): { error: string; "error-kind": ErrorKind
|
||||
const text = err instanceof Error ? err.message : String(err);
|
||||
// The forge's 404: what was asked about (a repository, a commit, a pull request, a branch, a tag) does not exist.
|
||||
if (/^Gitea API [^:]*: 404 /.test(text)) return { error: text, "error-kind": "not-found" };
|
||||
// A directory of `files` the commit does not hold (client.ts commitTrees): not-found, the error naming the directory, so the
|
||||
// indexer reads the others alone and leaves the gone one out (novox/hq design 47 H H-A-232).
|
||||
if (err instanceof DirectoryNotInTree) return { error: text, "error-kind": "not-found" };
|
||||
const refused = err instanceof Refused || /^Gitea API [^:]*: 4\d\d /.test(text) || /^Gitea API: /.test(text);
|
||||
return { error: text, "error-kind": refused ? "refused" : "failed" };
|
||||
}
|
||||
|
||||
@@ -50,8 +50,9 @@ async function serve(pages: any[]) {
|
||||
res.end(JSON.stringify(pages[page - 1] ?? { sha: COMMIT, tree: [], truncated: false, page, total_count: 0 }));
|
||||
});
|
||||
await new Promise<void>((r) => server.listen(0, r));
|
||||
const client = new GiteaClient(`http://127.0.0.1:${(server.address() as any).port}`, "t");
|
||||
return { client, server, asked };
|
||||
const url = `http://127.0.0.1:${(server.address() as any).port}`;
|
||||
const client = new GiteaClient(url, "t");
|
||||
return { client, server, asked, url };
|
||||
}
|
||||
|
||||
test("a commit's trees: the root and the tree of each directory holding a module.json", async () => {
|
||||
@@ -238,12 +239,19 @@ test("files: every blob under each asked directory, recursively, as its path rel
|
||||
});
|
||||
|
||||
test("a directory of files the commit lacks, or spelled badly, or a listing the forge cut, fails and answers nothing", async () => {
|
||||
const { client, server } = await serve([{ sha: COMMIT, url: "u", tree: LISTING, truncated: false, page: 1, total_count: LISTING.length }]);
|
||||
const { client, server, url } = await serve([{ sha: COMMIT, url: "u", tree: LISTING, truncated: false, page: 1, total_count: LISTING.length }]);
|
||||
try {
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, ["modules/a", "modules/gone"]), (err: Error) => {
|
||||
assert.match(err.message, /modules\/gone/);
|
||||
// The seat answers it with the kind not-found naming the directory (novox/hq design 47 H H-A-232), never failed: the
|
||||
// holder's own client and failure, as the module runs them.
|
||||
const { failure } = await import("../dist/seat.js");
|
||||
const { GiteaClient } = await import("../dist/client.js");
|
||||
await assert.rejects(new GiteaClient(url, "t").commitTrees("novox", "hq", COMMIT, undefined, ["modules/a", "modules/gone"]), (err: Error) => {
|
||||
const said = failure(err);
|
||||
assert.equal(said["error-kind"], "not-found", JSON.stringify(said));
|
||||
assert.match(said.error, /the directory modules\/gone is not in novox\/hq/);
|
||||
return true;
|
||||
});
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, ["modules/a", "modules/gone"]), /modules\/gone/);
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, ["README.md"]), /README\.md/, "a file is no directory");
|
||||
for (const bad of ["../x", "/abs", "a//b", "modules/a/", "."]) {
|
||||
await assert.rejects(client.commitTrees("novox", "hq", COMMIT, undefined, [bad]), /files/, bad);
|
||||
|
||||
Reference in New Issue
Block a user