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
5 changes: 5 additions & 0 deletions .changeset/tolerate-gh-warnings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kitlangton/stack": patch
---

Tolerate non-JSON warnings (e.g. auth expiry notices) emitted before `gh pr view` JSON output. The decoder now extracts the JSON payload from stdout instead of requiring the entire output to be valid JSON.
16 changes: 13 additions & 3 deletions src/services/code-host/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,31 @@ class PullListData extends Schema.Class<PullListData>("PullListData")({

const PullListJson = Schema.Array(Schema.Array(PullListData));

const extractJson = (out: string): string => {
const start = out.search(/[[{]/);
if (start === -1) return out;
const opener = out[start];
const closer = opener === "{" ? "}" : "]";
const end = out.lastIndexOf(closer);
if (end === -1 || end < start) return out;
return out.slice(start, end + 1);
};

const decodePullList = (args: ReadonlyArray<string>, out: string) =>
Effect.try({
try: () => Schema.decodeUnknownSync(PullListJson)(JSON.parse(out)),
try: () => Schema.decodeUnknownSync(PullListJson)(JSON.parse(extractJson(out))),
catch: (err) => new CodeHostDecodeError("gh", args, out, String(err)),
});

const decodePullView = (args: ReadonlyArray<string>, out: string) =>
Effect.try({
try: () => Schema.decodeUnknownSync(PullView)(JSON.parse(out)),
try: () => Schema.decodeUnknownSync(PullView)(JSON.parse(extractJson(out))),
catch: (err) => new CodeHostDecodeError("gh", args, out, String(err)),
});

const decodePullWatch = (args: ReadonlyArray<string>, out: string) =>
Effect.try({
try: () => Schema.decodeUnknownSync(PullWatch)(JSON.parse(out)),
try: () => Schema.decodeUnknownSync(PullWatch)(JSON.parse(extractJson(out))),
catch: (err) => new CodeHostDecodeError("gh", args, out, String(err)),
});

Expand Down
34 changes: 34 additions & 0 deletions tests/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,40 @@ describe("GitHub", () => {
);
});

it.effect("tolerates non-JSON warnings emitted before gh pr view output", () => {
const proc = Layer.succeed(
Proc.Service,
Proc.Service.of({
exec: () =>
Effect.sync(() =>
[
"warning: authentication token expires soon, run gh auth refresh",
JSON.stringify({
number: 1,
title: "one",
body: "body",
headRefName: "one",
headRepository: { nameWithOwner: "owner/project" },
baseRefName: "main",
url: "u1",
isDraft: false,
labels: [],
}),
].join("\n"),
),
}),
);

return Effect.gen(function* () {
const github = yield* CodeHost.Service;
const meta = yield* github.change(1);
expect(Number(meta.number)).toBe(1);
expect(meta.title).toBe("one");
}).pipe(
Effect.provide(CodeHostGitHub.layer.pipe(Layer.provideMerge(cfg), Layer.provideMerge(proc))),
);
});

it.effect("resolves a created GitHub fork PR by its returned URL", () => {
const calls: Array<ReadonlyArray<string>> = [];
const proc = Layer.succeed(
Expand Down