Skip to content

Commit d8b7b87

Browse files
committed
fix(source-control-entire): preserve literal text and bound cli errors
1 parent c875280 commit d8b7b87

4 files changed

Lines changed: 88 additions & 12 deletions

File tree

‎packages/source-control-entire/src/server/EntireApi.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,45 @@ import { assert, describe, it } from "@effect/vitest";
22
import { GitCommandError } from "@t3tools/contracts";
33
import * as Effect from "effect/Effect";
44
import * as Schema from "effect/Schema";
5+
import * as FetchHttpClient from "effect/http/FetchHttpClient";
6+
import * as EntireCli from "./EntireCli.ts";
57

68
import * as EntireApi from "./EntireApi.ts";
79
import { API_ORIGIN, fakeEntire, REMOTE_URL } from "./entireTestServer.ts";
810

911
const request = { cwd: "/repo", repository: "gh/acme/web", operation: "test" } as const;
1012

1113
describe("EntireApi", () => {
14+
it.effect.each([
15+
[new EntireCli.EntireCliMissingError(), "missing-tool"],
16+
[new EntireCli.EntireNotSignedInError(), "unauthenticated"],
17+
[
18+
new EntireCli.EntireCliFailedError({
19+
operation: "find repository",
20+
detail: "private command output",
21+
}),
22+
"failed",
23+
],
24+
] as const)("keeps CLI failures in the cause with reason %s", ([cause, reason]) => {
25+
const server = fakeEntire({});
26+
return Effect.gen(function* () {
27+
const cli = yield* EntireCli.EntireCli;
28+
const api = yield* EntireApi.make.pipe(
29+
Effect.provideService(EntireCli.EntireCli, {
30+
...cli,
31+
repository: () => Effect.fail(cause),
32+
}),
33+
Effect.provide(FetchHttpClient.layer),
34+
);
35+
const error = yield* api
36+
.requestJson({ ...request, path: "/trails/7" }, Schema.Unknown)
37+
.pipe(Effect.flip);
38+
assert.strictEqual(error.reason, reason);
39+
assert.strictEqual(error.cause, cause);
40+
assert.notInclude(error.message, "private command output");
41+
assert.deepStrictEqual(server.sent, []);
42+
}).pipe(Effect.provide(server.layer));
43+
});
1244
it.effect("sends the CLI's bearer to the repository's cell and never follows a redirect", () => {
1345
const server = fakeEntire({ "GET /trails/7": { ok: true } });
1446
return Effect.gen(function* () {

‎packages/source-control-entire/src/server/EntireApi.ts‎

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const NO_REDIRECT: RequestInit = { redirect: "manual" };
1717
const TIMEOUT = Duration.seconds(30);
1818
const API_HOST_SUFFIX = ".api.entire.io";
1919

20-
export const EntireApiFailure = Schema.Literals([
20+
const EntireApiFailure = Schema.Literals([
2121
"missing-tool",
2222
"unauthenticated",
2323
"rate-limited",
@@ -167,13 +167,21 @@ export const make = Effect.gen(function* () {
167167
reason: "failed",
168168
detail: "Entire repositories are named gh/<owner>/<repo> or et/<project>/<repo>.",
169169
});
170-
const coordinates = yield* cli
171-
.repository(yield* reference(input))
172-
.pipe(
173-
Effect.mapError((cause) =>
174-
fail({ status: null, reason: cliFailure(cause), detail: cause.message, cause }),
175-
),
176-
);
170+
const coordinates = yield* cli.repository(yield* reference(input)).pipe(
171+
Effect.mapError((cause) =>
172+
fail({
173+
status: null,
174+
reason: cliFailure(cause),
175+
detail:
176+
cause._tag === "EntireCliMissingError"
177+
? "The Entire CLI is not installed on the server."
178+
: cause._tag === "EntireNotSignedInError"
179+
? "The Entire CLI on the server is not signed in. Run entire login."
180+
: "The Entire CLI could not find the repository.",
181+
cause,
182+
}),
183+
),
184+
);
177185
return { repository, coordinates };
178186
});
179187

@@ -184,7 +192,17 @@ export const make = Effect.gen(function* () {
184192
) {
185193
const cells = yield* cli.cells.pipe(
186194
Effect.mapError((cause) =>
187-
fail({ status: null, reason: cliFailure(cause), detail: cause.message, cause }),
195+
fail({
196+
status: null,
197+
reason: cliFailure(cause),
198+
detail:
199+
cause._tag === "EntireCliMissingError"
200+
? "The Entire CLI is not installed on the server."
201+
: cause._tag === "EntireNotSignedInError"
202+
? "The Entire CLI on the server is not signed in. Run entire login."
203+
: "The Entire CLI could not list processing cells.",
204+
cause,
205+
}),
188206
),
189207
);
190208
const match = cells.find(
@@ -230,7 +248,17 @@ export const make = Effect.gen(function* () {
230248
: `/api/v1/repos/${encodeURIComponent(coordinates.repoId)}`;
231249
const token = yield* cli.token.pipe(
232250
Effect.mapError((cause) =>
233-
fail({ status: null, reason: cliFailure(cause), detail: cause.message, cause }),
251+
fail({
252+
status: null,
253+
reason: cliFailure(cause),
254+
detail:
255+
cause._tag === "EntireCliMissingError"
256+
? "The Entire CLI is not installed on the server."
257+
: cause._tag === "EntireNotSignedInError"
258+
? "The Entire CLI on the server is not signed in. Run entire login."
259+
: "The Entire CLI could not read the authentication token.",
260+
cause,
261+
}),
234262
),
235263
);
236264
const base = HttpClientRequest.make(input.method ?? "GET")(

‎packages/source-control-entire/src/server/entireTrailBody.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ describe("trailBodyMarkdown", () => {
9999
content: [
100100
paragraph(text("**Mercury**, snake_case, _x_, ~~y~~, [a](b), `c` and C:\\*")),
101101
paragraph(text("2 * 3 = 6, user_id, [WIP]")),
102+
...["# heading", "> quote", "- item", "1. item"].map((value) => paragraph(text(value))),
102103
],
103104
};
104105
const markdown = trailBodyMarkdown({ text_snapshot: "", content_json });
@@ -107,10 +108,21 @@ describe("trailBodyMarkdown", () => {
107108
"\\*\\*Mercury\\*\\*, snake_case, \\_x\\_, \\~\\~y\\~\\~, \\[a](b), \\`c\\` and C:\\\\\\*",
108109
"",
109110
"2 * 3 = 6, user_id, [WIP]",
111+
"",
112+
"\\# heading",
113+
"",
114+
"\\> quote",
115+
"",
116+
"\\- item",
117+
"",
118+
"1\\. item",
110119
].join("\n"),
111120
);
112121
// Entire's converter reads no escapes, so saving them back would keep the backslashes.
113122
expect(unpreservedMarkdown(markdown)).toBe("escaped markdown characters");
123+
for (const value of ["\\# heading", "\\> quote", "\\- item", "1\\. item"]) {
124+
expect(unpreservedMarkdown(value)).toBe("escaped markdown characters");
125+
}
114126
expect(unpreservedMarkdown("2 * 3 = 6, user_id, [WIP]")).toBeNull();
115127
});
116128

‎packages/source-control-entire/src/server/entireTrailBody.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ function renderList(node: ProseMirrorNode, ordered: boolean): string {
9696
function renderBlock(node: ProseMirrorNode): string {
9797
switch (node.type) {
9898
case "paragraph":
99-
return renderInline(node);
99+
return renderInline(node).replace(
100+
/^( {0,3})(#{1,6}(?=\s|$)|[>+-](?=\s|$)|\d{1,9}[.)](?=\s))/gmu,
101+
(_, indent: string, marker: string) =>
102+
indent + marker.replace(/[#>+.)-]/u, (char) => `\\${char}`),
103+
);
100104
case "heading": {
101105
const level = typeof node.attrs?.level === "number" ? node.attrs.level : 1;
102106
return `${"#".repeat(Math.min(Math.max(level, 1), 6))} ${renderInline(node)}`;
@@ -164,7 +168,7 @@ const BLANK_QUOTE = /^\s{0,3}>\s*$/u;
164168
const LIST_ITEM = /^(\s*)(?:[-*+]|(\d+)[.)])\s+/u;
165169
const UNDERSCORE_EMPHASIS = /(^|[^\w\\])(__?)(?=\S).*?(?<=\S)\2(?!\w)/u;
166170
const STRIKE = /~~(?=\S).*?~~/u;
167-
const ESCAPE = /\\[\\`*_~[]/u;
171+
const ESCAPE = /\\[\\`*_~[\]#>+.)-]/u;
168172
/** `***x***` reads back as bold `*x` and a stray `*`. */
169173
const BOLD_ITALIC = /\*\*\*/u;
170174

0 commit comments

Comments
 (0)