| 1 | // Wrapper around the local Gitea API. Used by the chat to: |
| 2 | // - check whether a repo exists |
| 3 | // - list repos (so the user can pick an existing one) |
| 4 | // - create a new repo for a brand-new project |
| 5 | const settings = require('../routes/settings'); |
| 6 | |
| 7 | function cfg() { |
| 8 | const s = settings.read().integrations || {}; |
| 9 | return { |
| 10 | url: s.giteaUrl || process.env.GITEA_URL_INTERNAL || 'http://localhost:3200', |
| 11 | token: s.giteaToken || process.env.GITEA_TOKEN || '', |
| 12 | user: s.giteaUser || process.env.GITEA_ADMIN_USER || '', |
| 13 | }; |
| 14 | } |
| 15 | |
| 16 | function headers() { |
| 17 | const c = cfg(); |
| 18 | const h = { 'Content-Type': 'application/json', Accept: 'application/json' }; |
| 19 | if (c.token) h['Authorization'] = `token ${c.token}`; |
| 20 | return h; |
| 21 | } |
| 22 | |
| 23 | async function status() { |
| 24 | const c = cfg(); |
| 25 | if (!c.url) return { available: false, error: 'no Gitea URL configured' }; |
| 26 | try { |
| 27 | const r = await fetch(`${c.url}/api/v1/version`, { signal: AbortSignal.timeout(3000) }); |
| 28 | if (!r.ok) return { available: false, error: `HTTP ${r.status}` }; |
| 29 | const j = await r.json(); |
| 30 | return { available: true, url: c.url, version: j.version, hasToken: !!c.token, user: c.user }; |
| 31 | } catch (e) { return { available: false, error: e.message }; } |
| 32 | } |
| 33 | |
| 34 | async function listRepos() { |
| 35 | const c = cfg(); |
| 36 | if (!c.token) throw new Error('Gitea token not configured (Settings → Integrations)'); |
| 37 | const r = await fetch(`${c.url}/api/v1/repos/search?limit=50`, { headers: headers() }); |
| 38 | if (!r.ok) throw new Error(`gitea ${r.status}: ${await r.text()}`); |
| 39 | const j = await r.json(); |
| 40 | return (j.data || []).map(repo => ({ |
| 41 | full_name: repo.full_name, |
| 42 | name: repo.name, |
| 43 | owner: repo.owner?.login, |
| 44 | description: repo.description, |
| 45 | private: repo.private, |
| 46 | clone_url: repo.clone_url, |
| 47 | ssh_url: repo.ssh_url, |
| 48 | html_url: repo.html_url, |
| 49 | updated_at: repo.updated_at, |
| 50 | })); |
| 51 | } |
| 52 | |
| 53 | async function repoExists(fullName) { |
| 54 | const c = cfg(); |
| 55 | const r = await fetch(`${c.url}/api/v1/repos/${fullName}`, { headers: headers() }); |
| 56 | if (r.status === 404) return false; |
| 57 | if (!r.ok) throw new Error(`gitea ${r.status}`); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | async function createRepo({ name, owner, description = '', private: priv = false, autoInit = true, defaultBranch = 'main' }) { |
| 62 | const c = cfg(); |
| 63 | if (!c.token) throw new Error('Gitea token not configured (Settings → Integrations)'); |
| 64 | const targetOwner = owner || c.user; |
| 65 | // POST /user/repos creates under the authenticated user; /orgs/{org}/repos creates under an org. |
| 66 | const url = targetOwner === c.user |
| 67 | ? `${c.url}/api/v1/user/repos` |
| 68 | : `${c.url}/api/v1/orgs/${targetOwner}/repos`; |
| 69 | const r = await fetch(url, { |
| 70 | method: 'POST', |
| 71 | headers: headers(), |
| 72 | body: JSON.stringify({ name, description, private: priv, auto_init: autoInit, default_branch: defaultBranch }), |
| 73 | }); |
| 74 | if (!r.ok) throw new Error(`gitea ${r.status}: ${await r.text()}`); |
| 75 | const j = await r.json(); |
| 76 | return { |
| 77 | full_name: j.full_name, |
| 78 | clone_url: j.clone_url, |
| 79 | ssh_url: j.ssh_url, |
| 80 | html_url: j.html_url, |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | module.exports = { cfg, status, listRepos, repoExists, createRepo }; |