/** * Gitea 上的仓库操作(建仓 / 查仓 / 列出)。 * * 凭据从环境变量读,不写进命令行参数也不落盘 —— 免得密码进 shell 历史或被提交。 * GIT_USER / GIT_PASS / GIT_HOST * * 用法: * node scripts/gitea.mjs whoami * node scripts/gitea.mjs list * node scripts/gitea.mjs ensure 建仓(已存在就复用) */ const HOST = process.env.GIT_HOST || 'https://git.xnhw.cc'; const USER = process.env.GIT_USER || ''; const PASS = process.env.GIT_PASS || ''; if (!USER || !PASS) { console.error('缺少 GIT_USER / GIT_PASS 环境变量'); process.exit(2); } const auth = `Basic ${Buffer.from(`${USER}:${PASS}`).toString('base64')}`; async function api(path, init = {}) { const res = await fetch(`${HOST}/api/v1${path}`, { ...init, headers: { Authorization: auth, 'Content-Type': 'application/json', ...(init.headers || {}), }, }); const text = await res.text(); let body; try { body = text ? JSON.parse(text) : {}; } catch { body = { __raw: text.slice(0, 300) }; } return { status: res.status, body }; } const [cmd, arg] = process.argv.slice(2); if (cmd === 'whoami') { const r = await api('/user'); if (r.status !== 200) { console.log('❌ 认证失败:HTTP', r.status, JSON.stringify(r.body).slice(0, 200)); process.exit(1); } console.log('✅ 认证通过'); console.log(' 用户名 :', r.body.login); console.log(' 昵称 :', r.body.full_name || '(无)'); console.log(' 邮箱 :', r.body.email || '(隐藏)'); console.log(' 管理员 :', r.body.is_admin ? '是' : '否'); } else if (cmd === 'list') { const r = await api(`/users/${encodeURIComponent(USER)}/repos?limit=50`); if (r.status !== 200) { console.log('❌ HTTP', r.status, JSON.stringify(r.body).slice(0, 200)); process.exit(1); } if (!r.body.length) { console.log(` ${USER} 名下还没有仓库`); } else { console.log(` ${USER} 名下有 ${r.body.length} 个仓库:`); for (const repo of r.body) { console.log(` ${repo.full_name.padEnd(28)} ${repo.private ? '私有' : '公开'} ${repo.size ?? 0} KB ${repo.empty ? '(空)' : ''}`); } } } else if (cmd === 'ensure') { const name = arg; if (!name) { console.error('用法: ensure '); process.exit(2); } const exists = await api(`/repos/${encodeURIComponent(USER)}/${encodeURIComponent(name)}`); if (exists.status === 200) { console.log(`✅ 仓库已存在:${exists.body.full_name}(${exists.body.private ? '私有' : '公开'},${exists.body.empty ? '空仓' : '有内容'})`); console.log(` clone: ${exists.body.clone_url}`); } else { const created = await api('/user/repos', { method: 'POST', body: JSON.stringify({ name, description: '豆包复刻 · 原生安卓(Compose + 内嵌 Termux)+ Web/Windows', private: true, auto_init: false, default_branch: 'main', }), }); if (created.status === 201) { console.log(`✅ 已创建:${created.body.full_name}(私有)`); console.log(` clone: ${created.body.clone_url}`); } else { console.log('❌ 创建失败 HTTP', created.status, JSON.stringify(created.body).slice(0, 300)); process.exit(1); } } } else { console.error('用法: whoami | list | ensure '); process.exit(2); }