Skip to content

Commit 0902adc

Browse files
fix(forgejo): tea API failures keep Gitea's reason (pingdotgg#32244)
1 parent dd7918e commit 0902adc

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎packages/source-control-forgejo/src/server/ForgejoCli.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@ export const make = Effect.gen(function* () {
727727
});
728728
// tea reports HTTP failures with exit code zero; use its response status.
729729
const status = Number(/^HTTP\/\S+ (\d{3})/m.exec(result.stderr)?.[1]);
730-
if (!status || status >= 400)
730+
if (!status || status >= 400) {
731+
const body = result.stdout.trim();
731732
return yield* new ForgejoCliError({
732733
command: "tea",
733734
cwd: input.cwd,
@@ -748,8 +749,13 @@ export const make = Effect.gen(function* () {
748749
? "Forgejo repository or pull request was not found."
749750
: status === 429
750751
? "Forgejo API rate limit exceeded."
751-
: `Forgejo API request failed${status ? ` (HTTP ${status})` : " without an HTTP status"}.`,
752+
: !status
753+
? "Forgejo API request failed without an HTTP status."
754+
: body
755+
? `Forgejo API request failed (HTTP ${status}): ${body}`
756+
: `Forgejo API request failed (HTTP ${status}).`,
752757
});
758+
}
753759
return result;
754760
});
755761
return ForgejoCli.of({ execute, listLogins, getAccount, resolveRepository, api });

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,67 @@ it.effect("rejects HTTP failures even when tea exits successfully", () =>
14371437
),
14381438
);
14391439

1440+
it.effect("keeps Gitea's failure reason when tea reports an HTTP error", () =>
1441+
Effect.gen(function* () {
1442+
const cli = yield* ForgejoCli.make;
1443+
const result = yield* cli
1444+
.api({
1445+
cwd: "/repo",
1446+
repository: "http://forgejo.local:3000/maria/project",
1447+
path: "repos/maria/project/pulls/1/merge",
1448+
method: "POST",
1449+
body: { Do: "merge", delete_branch_after_merge: true },
1450+
})
1451+
.pipe(Effect.result);
1452+
assert.strictEqual(result._tag, "Failure");
1453+
if (result._tag === "Failure") {
1454+
assert.include(result.failure.detail, "405");
1455+
assert.include(result.failure.detail, "The head branch is behind the base branch");
1456+
}
1457+
}).pipe(
1458+
Effect.provideService(
1459+
FileSystem.FileSystem,
1460+
FileSystem.makeNoop({
1461+
exists: () => Effect.succeed(false),
1462+
}),
1463+
),
1464+
Effect.provideService(
1465+
HttpClient.HttpClient,
1466+
HttpClient.make(() => {
1467+
throw new Error("tea must handle its own HTTP request");
1468+
}),
1469+
),
1470+
Effect.provide(
1471+
TestSourceControlHost.layer({
1472+
process: {
1473+
run: (input) =>
1474+
Effect.succeed(
1475+
input.args[0] === "login"
1476+
? processOutput(
1477+
encodeJson([
1478+
{
1479+
name: "work",
1480+
url: "http://forgejo.local:3000",
1481+
ssh_host: "forgejo.local",
1482+
user: "maria",
1483+
default: "true",
1484+
},
1485+
]),
1486+
)
1487+
: processOutput(
1488+
'{"message":"The head branch is behind the base branch","url":"http://forgejo.local:3000/api/swagger"}',
1489+
{
1490+
stderr:
1491+
"HTTP/1.1 405 Method Not Allowed\nContent-Type: application/json;charset=utf-8\n",
1492+
},
1493+
),
1494+
),
1495+
},
1496+
}),
1497+
),
1498+
),
1499+
);
1500+
14401501
it.effect("routes mounted Forgejo repositories without repeating the mount in API paths", () =>
14411502
Effect.gen(function* () {
14421503
const cli = yield* ForgejoCli.make;

0 commit comments

Comments
 (0)