One module per domain, each route declaring the minimum access level the server enforces before the handler runs. Irreversible actions ask for the exact name to be typed back, and every write lands in `event_logs` with its author so the history says who did what. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
180 lines
5.7 KiB
JavaScript
180 lines
5.7 KiB
JavaScript
import { ACCESS_LEVELS } from '../../config/constants.js';
|
||
import { env } from '../../config/env.js';
|
||
import { logEvent } from '../../database/repositories/event-log-repository.js';
|
||
import { org } from '../../services/gitea/gitea-admin.js';
|
||
import { isGiteaConfigured } from '../../services/gitea/gitea-client.js';
|
||
import { badRequest } from '../http.js';
|
||
|
||
/** Organization-wide management (M16-T04). */
|
||
|
||
const requireGitea = () => {
|
||
if (!isGiteaConfigured()) throw badRequest('Gitea n’est pas configuré (`GITEA_URL` et `GITEA_TOKEN`).');
|
||
};
|
||
|
||
const toMember = (member) => ({
|
||
id: member.id,
|
||
login: member.login ?? member.username,
|
||
fullName: member.full_name,
|
||
email: member.email,
|
||
avatar: member.avatar_url,
|
||
isAdmin: member.is_admin,
|
||
lastLogin: member.last_login,
|
||
createdAt: member.created,
|
||
});
|
||
|
||
export function registerOrganizationRoutes(router) {
|
||
router.get(
|
||
'/api/organization',
|
||
async () => {
|
||
requireGitea();
|
||
const [details, members, teams, repositories] = await Promise.all([
|
||
org.get(),
|
||
org.listMembers({ limit: 100 }).catch(() => []),
|
||
org.listTeams({ limit: 100 }).catch(() => []),
|
||
org.listRepos({ limit: 100 }).catch(() => []),
|
||
]);
|
||
|
||
return {
|
||
name: details.username ?? details.name,
|
||
fullName: details.full_name,
|
||
description: details.description,
|
||
website: details.website,
|
||
location: details.location,
|
||
email: details.email,
|
||
avatar: details.avatar_url,
|
||
visibility: details.visibility,
|
||
url: `${env.gitea.url}/${details.username ?? details.name}`,
|
||
members: members.map(toMember),
|
||
teams: teams.map((team) => ({
|
||
id: team.id,
|
||
name: team.name,
|
||
description: team.description,
|
||
permission: team.permission,
|
||
includesAllRepositories: team.includes_all_repositories,
|
||
canCreateOrgRepo: team.can_create_org_repo,
|
||
units: team.units,
|
||
})),
|
||
repositories: repositories.map((repository) => ({
|
||
id: repository.id,
|
||
fullName: repository.full_name,
|
||
name: repository.name,
|
||
description: repository.description,
|
||
private: repository.private,
|
||
archived: repository.archived,
|
||
fork: repository.fork,
|
||
template: repository.template,
|
||
defaultBranch: repository.default_branch,
|
||
stars: repository.stars_count,
|
||
forks: repository.forks_count,
|
||
watchers: repository.watchers_count,
|
||
openIssues: repository.open_issues_count,
|
||
openPulls: repository.open_pr_counter,
|
||
releases: repository.release_counter,
|
||
sizeKb: repository.size,
|
||
language: repository.language,
|
||
updatedAt: repository.updated_at,
|
||
createdAt: repository.created_at,
|
||
htmlUrl: repository.html_url,
|
||
hasIssues: repository.has_issues,
|
||
hasPullRequests: repository.has_pull_requests,
|
||
hasActions: repository.has_actions,
|
||
})),
|
||
};
|
||
},
|
||
{ access: ACCESS_LEVELS.STAFF },
|
||
);
|
||
|
||
router.patch(
|
||
'/api/organization',
|
||
async ({ body, actor }) => {
|
||
requireGitea();
|
||
const payload = {};
|
||
for (const field of ['full_name', 'description', 'website', 'location', 'visibility']) {
|
||
if (field in body) payload[field] = String(body[field] ?? '');
|
||
}
|
||
if (Object.keys(payload).length === 0) throw badRequest('Aucun champ modifiable fourni.');
|
||
|
||
await org.update(payload);
|
||
logEvent({
|
||
guildId: env.discord.guildId || null,
|
||
type: 'web.organization.update',
|
||
actorId: actor.userId,
|
||
payload: { fields: Object.keys(payload) },
|
||
});
|
||
return { ok: true };
|
||
},
|
||
{ access: ACCESS_LEVELS.OWNER },
|
||
);
|
||
|
||
router.get(
|
||
'/api/organization/teams/:id/members',
|
||
async ({ params }) => {
|
||
requireGitea();
|
||
const members = await org.listTeamMembers(Number.parseInt(params.id, 10));
|
||
return { members: members.map(toMember) };
|
||
},
|
||
{ access: ACCESS_LEVELS.STAFF },
|
||
);
|
||
|
||
router.get(
|
||
'/api/organization/labels',
|
||
async () => {
|
||
requireGitea();
|
||
return { labels: await org.listLabels() };
|
||
},
|
||
{ access: ACCESS_LEVELS.STAFF },
|
||
);
|
||
|
||
router.get(
|
||
'/api/organization/hooks',
|
||
async () => {
|
||
requireGitea();
|
||
const hooks = await org.listHooks().catch(() => []);
|
||
return {
|
||
hooks: hooks.map((hook) => ({
|
||
id: hook.id,
|
||
type: hook.type,
|
||
active: hook.active,
|
||
events: hook.events,
|
||
url: hook.config?.url,
|
||
createdAt: hook.created_at,
|
||
})),
|
||
};
|
||
},
|
||
{ access: ACCESS_LEVELS.DEV },
|
||
);
|
||
|
||
/** Creates a repository inside the organization (M16-T04). */
|
||
router.post(
|
||
'/api/organization/repositories',
|
||
async ({ body, actor }) => {
|
||
requireGitea();
|
||
const name = String(body.name ?? '').trim();
|
||
if (!/^[\w.-]{1,100}$/.test(name)) {
|
||
throw badRequest('Nom de dépôt invalide : lettres, chiffres, `.`, `-` et `_` uniquement.');
|
||
}
|
||
|
||
const created = await org.createRepo({
|
||
name,
|
||
description: String(body.description ?? ''),
|
||
private: Boolean(body.private),
|
||
auto_init: body.autoInit !== false,
|
||
default_branch: String(body.defaultBranch ?? 'main'),
|
||
gitignores: body.gitignores ?? undefined,
|
||
license: body.license ?? undefined,
|
||
readme: body.readme ?? undefined,
|
||
issue_labels: body.issueLabels ?? undefined,
|
||
});
|
||
|
||
logEvent({
|
||
guildId: env.discord.guildId || null,
|
||
type: 'web.repository.create',
|
||
actorId: actor.userId,
|
||
payload: { repository: created.full_name },
|
||
});
|
||
return { fullName: created.full_name, htmlUrl: created.html_url };
|
||
},
|
||
{ access: ACCESS_LEVELS.DEV },
|
||
);
|
||
}
|