From 6fca3b7ec4545478b39a728d2db604ae60fe48f7 Mon Sep 17 00:00:00 2001 From: nocode Date: Sun, 13 Sep 2026 12:19:10 +0200 Subject: [PATCH] feat(gitea): add gitea api client Refs: #42 --- src/services/gitea/gitea-client.js | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/services/gitea/gitea-client.js diff --git a/src/services/gitea/gitea-client.js b/src/services/gitea/gitea-client.js new file mode 100644 index 0000000..8d9deb6 --- /dev/null +++ b/src/services/gitea/gitea-client.js @@ -0,0 +1,51 @@ +import { env } from '../../config/env.js'; +import { ExternalApiError } from '../../core/errors.js'; +import { withRetry } from '../../utils/retry.js'; + +const isTransient = (error) => error.name === 'TimeoutError' || error.status === 429 || error.status >= 500; + +/** Minimal Gitea REST API client (M5-T01). */ +export async function giteaRequest(endpoint, { method = 'GET', body } = {}) { + if (!env.gitea.url || !env.gitea.token) { + throw new ExternalApiError('gitea', 'Gitea is not configured'); + } + + return withRetry( + async () => { + const response = await fetch(`${env.gitea.url}/api/v1${endpoint}`, { + method, + headers: { + Authorization: `token ${env.gitea.token}`, + Accept: 'application/json', + ...(body && { 'Content-Type': 'application/json' }), + }, + body: body ? JSON.stringify(body) : undefined, + signal: AbortSignal.timeout(env.gitea.timeoutMs), + }); + + if (!response.ok) { + const error = new ExternalApiError('gitea', `${method} ${endpoint} failed with ${response.status}`); + error.status = response.status; + throw error; + } + return response.status === 204 ? null : response.json(); + }, + { retries: env.gitea.maxRetries, shouldRetry: isTransient }, + ); +} + +const repoPath = () => `/repos/${env.gitea.owner}/${env.gitea.repo}`; + +export const gitea = { + listIssues: (params = 'state=open&type=issues&limit=10') => giteaRequest(`${repoPath()}/issues?${params}`), + getIssue: (number) => giteaRequest(`${repoPath()}/issues/${number}`), + createIssue: ({ title, body }) => + giteaRequest(`${repoPath()}/issues`, { method: 'POST', body: { title, body } }), + listPullRequests: (params = 'state=open&limit=10') => giteaRequest(`${repoPath()}/pulls?${params}`), + getPullRequest: (number) => giteaRequest(`${repoPath()}/pulls/${number}`), + createPullRequest: ({ title, body, head, base }) => + giteaRequest(`${repoPath()}/pulls`, { method: 'POST', body: { title, body, head, base } }), + listCommits: (limit = 10) => giteaRequest(`${repoPath()}/commits?limit=${limit}`), + listReleases: (limit = 5) => giteaRequest(`${repoPath()}/releases?limit=${limit}`), + getLatestRelease: () => giteaRequest(`${repoPath()}/releases/latest`), +};