refactor(all): uniformisation
This commit is contained in:
89 files changed
+10215
-10215
No files matched your search
@@ -1,69 +1,69 @@
|
||||
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 },
|
||||
);
|
||||
}
|
||||
|
||||
/** True when both the Gitea URL and token are configured (M5-T01). */
|
||||
export function isGiteaConfigured() {
|
||||
return Boolean(env.gitea.url && env.gitea.token);
|
||||
}
|
||||
|
||||
/** Gitea returns either a bare array or a `{ data: [...] }` wrapper depending on the version. */
|
||||
export function asList(payload) {
|
||||
if (Array.isArray(payload)) return payload;
|
||||
return Array.isArray(payload?.data) ? payload.data : [];
|
||||
}
|
||||
|
||||
/** Human readable URL of the configured repository, used in embeds. */
|
||||
export function repositoryUrl() {
|
||||
return env.gitea.url ? `${env.gitea.url}/${env.gitea.owner}/${env.gitea.repo}` : '';
|
||||
}
|
||||
|
||||
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`),
|
||||
getReleaseByTag: (tag) => giteaRequest(`${repoPath()}/releases/tags/${encodeURIComponent(tag)}`),
|
||||
listBranches: (limit = 20) => giteaRequest(`${repoPath()}/branches?limit=${limit}`),
|
||||
};
|
||||
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 },
|
||||
);
|
||||
}
|
||||
|
||||
/** True when both the Gitea URL and token are configured (M5-T01). */
|
||||
export function isGiteaConfigured() {
|
||||
return Boolean(env.gitea.url && env.gitea.token);
|
||||
}
|
||||
|
||||
/** Gitea returns either a bare array or a `{ data: [...] }` wrapper depending on the version. */
|
||||
export function asList(payload) {
|
||||
if (Array.isArray(payload)) return payload;
|
||||
return Array.isArray(payload?.data) ? payload.data : [];
|
||||
}
|
||||
|
||||
/** Human readable URL of the configured repository, used in embeds. */
|
||||
export function repositoryUrl() {
|
||||
return env.gitea.url ? `${env.gitea.url}/${env.gitea.owner}/${env.gitea.repo}` : '';
|
||||
}
|
||||
|
||||
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`),
|
||||
getReleaseByTag: (tag) => giteaRequest(`${repoPath()}/releases/tags/${encodeURIComponent(tag)}`),
|
||||
listBranches: (limit = 20) => giteaRequest(`${repoPath()}/branches?limit=${limit}`),
|
||||
};
|
||||
Reference in new issue
Block a user