From 4b0737a51224f7ba71426be0896f1d61335ed660 Mon Sep 17 00:00:00 2001 From: nocode Date: Wed, 23 Sep 2026 19:55:15 +0200 Subject: [PATCH] feat(gitea): add an organization and repository administration client `gitea-client.js` stays focused on the single repository the slash commands talk to. The dashboard addresses any repository of the organization, so its endpoints live apart instead of bloating the command path. Co-Authored-By: Claude Opus 5 (1M context) --- src/services/gitea/gitea-admin.js | 144 ++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/services/gitea/gitea-admin.js diff --git a/src/services/gitea/gitea-admin.js b/src/services/gitea/gitea-admin.js new file mode 100644 index 0000000..2ff5589 --- /dev/null +++ b/src/services/gitea/gitea-admin.js @@ -0,0 +1,144 @@ +import { env } from '../../config/env.js'; +import { asList, giteaRequest } from './gitea-client.js'; + +/** + * Administration endpoints used by the web dashboard (M16-T04). + * + * `gitea-client.js` stays focused on the single repository the Discord commands + * talk to; this module addresses any repository of the organization, which is + * what the dashboard needs. + */ + +const query = (params = {}) => { + const search = new URLSearchParams(); + for (const [key, value] of Object.entries(params)) { + if (value !== undefined && value !== null && value !== '') search.set(key, String(value)); + } + const rendered = search.toString(); + return rendered ? `?${rendered}` : ''; +}; + +const path = (owner, repo) => `/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}`; + +/** Organization the dashboard manages by default. */ +export const defaultOwner = () => env.gitea.owner; + +export const org = { + get: (owner = defaultOwner()) => giteaRequest(`/orgs/${encodeURIComponent(owner)}`), + update: (payload, owner = defaultOwner()) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}`, { method: 'PATCH', body: payload }), + listRepos: ({ owner = defaultOwner(), page = 1, limit = 50 } = {}) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}/repos${query({ page, limit })}`).then(asList), + listMembers: ({ owner = defaultOwner(), page = 1, limit = 50 } = {}) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}/members${query({ page, limit })}`).then(asList), + listTeams: ({ owner = defaultOwner(), page = 1, limit = 50 } = {}) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}/teams${query({ page, limit })}`).then(asList), + listTeamMembers: (teamId) => giteaRequest(`/teams/${teamId}/members`).then(asList), + listLabels: ({ owner = defaultOwner(), limit = 100 } = {}) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}/labels${query({ limit })}`).then(asList), + listHooks: ({ owner = defaultOwner() } = {}) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}/hooks`).then(asList), + createRepo: (payload, owner = defaultOwner()) => + giteaRequest(`/orgs/${encodeURIComponent(owner)}/repos`, { method: 'POST', body: payload }), + /** Identity behind `GITEA_TOKEN`, used by the settings page to check the credentials. */ + whoami: () => giteaRequest('/user'), +}; + +export const repos = { + get: (owner, repo) => giteaRequest(path(owner, repo)), + update: (owner, repo, payload) => giteaRequest(path(owner, repo), { method: 'PATCH', body: payload }), + remove: (owner, repo) => giteaRequest(path(owner, repo), { method: 'DELETE' }), + + listBranches: (owner, repo, { page = 1, limit = 50 } = {}) => + giteaRequest(`${path(owner, repo)}/branches${query({ page, limit })}`).then(asList), + createBranch: (owner, repo, { newBranchName, oldBranchName }) => + giteaRequest(`${path(owner, repo)}/branches`, { + method: 'POST', + body: { new_branch_name: newBranchName, old_branch_name: oldBranchName }, + }), + removeBranch: (owner, repo, branch) => + giteaRequest(`${path(owner, repo)}/branches/${encodeURIComponent(branch)}`, { method: 'DELETE' }), + listTags: (owner, repo, { limit = 50 } = {}) => + giteaRequest(`${path(owner, repo)}/tags${query({ limit })}`).then(asList), + listCommits: (owner, repo, { sha, page = 1, limit = 20 } = {}) => + giteaRequest(`${path(owner, repo)}/commits${query({ sha, page, limit })}`).then(asList), + languages: (owner, repo) => giteaRequest(`${path(owner, repo)}/languages`), + listTopics: (owner, repo) => giteaRequest(`${path(owner, repo)}/topics`), + replaceTopics: (owner, repo, topics) => + giteaRequest(`${path(owner, repo)}/topics`, { method: 'PUT', body: { topics } }), + + listIssues: (owner, repo, { state = 'open', type = 'issues', page = 1, limit = 20, q } = {}) => + giteaRequest(`${path(owner, repo)}/issues${query({ state, type, page, limit, q })}`).then(asList), + createIssue: (owner, repo, { title, body, labels, assignees }) => + giteaRequest(`${path(owner, repo)}/issues`, { method: 'POST', body: { title, body, labels, assignees } }), + updateIssue: (owner, repo, number, payload) => + giteaRequest(`${path(owner, repo)}/issues/${number}`, { method: 'PATCH', body: payload }), + commentIssue: (owner, repo, number, body) => + giteaRequest(`${path(owner, repo)}/issues/${number}/comments`, { method: 'POST', body: { body } }), + listLabels: (owner, repo, { limit = 100 } = {}) => + giteaRequest(`${path(owner, repo)}/labels${query({ limit })}`).then(asList), + + listPulls: (owner, repo, { state = 'open', page = 1, limit = 20 } = {}) => + giteaRequest(`${path(owner, repo)}/pulls${query({ state, page, limit })}`).then(asList), + createPull: (owner, repo, { title, body, head, base }) => + giteaRequest(`${path(owner, repo)}/pulls`, { method: 'POST', body: { title, body, head, base } }), + mergePull: (owner, repo, number, { style = 'squash', title, message, deleteBranch = false } = {}) => + giteaRequest(`${path(owner, repo)}/pulls/${number}/merge`, { + method: 'POST', + body: { + Do: style, + MergeTitleField: title, + MergeMessageField: message, + delete_branch_after_merge: deleteBranch, + }, + }), + + listReleases: (owner, repo, { page = 1, limit = 20, draft, preRelease } = {}) => + giteaRequest( + `${path(owner, repo)}/releases${query({ page, limit, draft, 'pre-release': preRelease })}`, + ).then(asList), + getRelease: (owner, repo, id) => giteaRequest(`${path(owner, repo)}/releases/${id}`), + latestRelease: (owner, repo) => giteaRequest(`${path(owner, repo)}/releases/latest`), + createRelease: (owner, repo, { tagName, target, name, body, draft = false, prerelease = false }) => + giteaRequest(`${path(owner, repo)}/releases`, { + method: 'POST', + body: { tag_name: tagName, target_commitish: target, name, body, draft, prerelease }, + }), + updateRelease: (owner, repo, id, payload) => + giteaRequest(`${path(owner, repo)}/releases/${id}`, { method: 'PATCH', body: payload }), + removeRelease: (owner, repo, id) => + giteaRequest(`${path(owner, repo)}/releases/${id}`, { method: 'DELETE' }), + + listCollaborators: (owner, repo) => giteaRequest(`${path(owner, repo)}/collaborators`).then(asList), + addCollaborator: (owner, repo, username, permission = 'write') => + giteaRequest(`${path(owner, repo)}/collaborators/${encodeURIComponent(username)}`, { + method: 'PUT', + body: { permission }, + }), + removeCollaborator: (owner, repo, username) => + giteaRequest(`${path(owner, repo)}/collaborators/${encodeURIComponent(username)}`, { method: 'DELETE' }), + + listHooks: (owner, repo) => giteaRequest(`${path(owner, repo)}/hooks`).then(asList), + createHook: (owner, repo, { url, secret, events, type = 'gitea' }) => + giteaRequest(`${path(owner, repo)}/hooks`, { + method: 'POST', + body: { type, active: true, events, config: { url, content_type: 'json', secret } }, + }), + removeHook: (owner, repo, id) => giteaRequest(`${path(owner, repo)}/hooks/${id}`, { method: 'DELETE' }), + + /** Gitea Actions. Absent before Gitea 1.20, so callers must tolerate a 404. */ + listActionTasks: (owner, repo, { page = 1, limit = 20 } = {}) => + giteaRequest(`${path(owner, repo)}/actions/tasks${query({ page, limit })}`), + dispatchWorkflow: (owner, repo, workflow, { ref, inputs = {} }) => + giteaRequest(`${path(owner, repo)}/actions/workflows/${encodeURIComponent(workflow)}/dispatches`, { + method: 'POST', + body: { ref, inputs }, + }), +}; + +/** Splits `FWS/FWS-Arch` into its two parts, rejecting anything else. */ +export function splitRepository(fullName) { + const [owner, repo, ...rest] = String(fullName ?? '').split('/'); + if (!owner || !repo || rest.length > 0) return null; + return { owner, repo }; +}