From 0fbf1d97bcfffc261803f1af1b8d87103433acb0 Mon Sep 17 00:00:00 2001 From: opencode-agent Date: Thu, 6 Aug 2026 15:11:23 +0000 Subject: [PATCH] feat(create-readme): support Forgejo contents API --- .opencode/tools/create-readme.ts | 41 +++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/.opencode/tools/create-readme.ts b/.opencode/tools/create-readme.ts index 6724355..89439f0 100644 --- a/.opencode/tools/create-readme.ts +++ b/.opencode/tools/create-readme.ts @@ -296,6 +296,46 @@ export default tool({ }) if (args.repo) { + const content64 = Buffer.from(content, "utf-8").toString("base64") + if (process.env.FORGEJO_URL) { + const base = process.env.FORGEJO_URL + const token = process.env.FORGEJO_TOKEN + const headers: Record = { + Authorization: `token ${token}`, + Accept: "application/json", + } + let sha: string | undefined + try { + const getRes = await fetch(`${base}/api/v1/repos/${args.repo}/contents/README.md`, { headers }) + if (getRes.status === 200) { + sha = (await getRes.json()).sha + } else if (getRes.status !== 404) { + const text = await getRes.text() + return `⚠️ create-readme failed: Forgejo GET README.md → HTTP ${getRes.status}: ${text}` + } + } catch (e) { + return `⚠️ create-readme failed: Forgejo GET failed: ${e instanceof Error ? e.message : String(e)}` + } + const putBody: Record = { + content: content64, + message: "docs: update README", + } + if (sha) putBody.sha = sha + try { + const putRes = await fetch(`${base}/api/v1/repos/${args.repo}/contents/README.md`, { + method: "PUT", + headers: { ...headers, "Content-Type": "application/json" }, + body: JSON.stringify(putBody), + }) + if (!putRes.ok) { + const text = await putRes.text() + return `⚠️ create-readme failed: Forgejo PUT README.md → HTTP ${putRes.status}: ${text}` + } + } catch (e) { + return `⚠️ create-readme failed: Forgejo PUT failed: ${e instanceof Error ? e.message : String(e)}` + } + return `README.md updated in ${args.repo} via Forgejo API` + } const getRes = spawnSync( "gh", ["api", `repos/${args.repo}/contents/README.md`], @@ -309,7 +349,6 @@ export default tool({ sha = undefined } } - const content64 = Buffer.from(content, "utf-8").toString("base64") const putArgs = [ "api", "-X",