1 file changed
+51
@@ -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`),
|
||||
};
|
||||
Reference in new issue
Block a user