feat(create-readme): support Forgejo contents API
All checks were successful
CI / bootstrap (push) Successful in 11s
CI / lint (push) Successful in 26s
CI / typecheck (push) Successful in 33s
CI / test (3.12) (push) Successful in 1m39s
CI / test (3.13) (push) Successful in 2m21s
CI / test (3.14) (push) Successful in 1m26s
CI / complexity (push) Successful in 20s

This commit is contained in:
opencode-agent 2026-08-06 15:11:23 +00:00
parent 93e7843438
commit 0fbf1d97bc

View file

@ -296,6 +296,46 @@ export default tool({
}) })
if (args.repo) { 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<string, string> = {
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<string, string> = {
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( const getRes = spawnSync(
"gh", "gh",
["api", `repos/${args.repo}/contents/README.md`], ["api", `repos/${args.repo}/contents/README.md`],
@ -309,7 +349,6 @@ export default tool({
sha = undefined sha = undefined
} }
} }
const content64 = Buffer.from(content, "utf-8").toString("base64")
const putArgs = [ const putArgs = [
"api", "api",
"-X", "-X",