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
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:
parent
93e7843438
commit
0fbf1d97bc
1 changed files with 40 additions and 1 deletions
|
|
@ -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<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(
|
||||
"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",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue