153 lines
5.7 KiB
JavaScript
153 lines
5.7 KiB
JavaScript
import { ActionRowBuilder, MessageFlags, ModalBuilder, TextInputBuilder, TextInputStyle } from 'discord.js';
|
||
import { ExternalApiError, UserFacingError } from '../../core/errors.js';
|
||
import { createLogger } from '../../core/logger.js';
|
||
import { logEvent } from '../../database/repositories/event-log-repository.js';
|
||
import { linkButtons } from '../../ui/buttons.js';
|
||
import { errorEmbed } from '../../ui/embeds.js';
|
||
import { issueEmbed, pullRequestEmbed } from './gitea-embeds.js';
|
||
import { gitea, isGiteaConfigured } from './gitea-client.js';
|
||
import { consume, remember } from './pending-actions.js';
|
||
|
||
const log = createLogger('gitea');
|
||
|
||
const PR_CONTEXT_TTL_MS = 10 * 60_000;
|
||
|
||
/** Fails fast with an actionable message when Gitea is not configured (M5-T01). */
|
||
export function assertGiteaConfigured() {
|
||
if (!isGiteaConfigured()) {
|
||
throw new UserFacingError(
|
||
'La partie Gitea n’est pas encore configurée : il manque `GITEA_URL` et `GITEA_TOKEN` dans le `.env`. Tout le reste du bot fonctionne sans cela.',
|
||
);
|
||
}
|
||
}
|
||
|
||
function modal(id, title, fields) {
|
||
return new ModalBuilder()
|
||
.setCustomId(id)
|
||
.setTitle(title)
|
||
.addComponents(
|
||
fields.map((field) =>
|
||
new ActionRowBuilder().addComponents(
|
||
new TextInputBuilder()
|
||
.setCustomId(field.id)
|
||
.setLabel(field.label)
|
||
.setStyle(field.paragraph ? TextInputStyle.Paragraph : TextInputStyle.Short)
|
||
.setMaxLength(field.maxLength)
|
||
.setRequired(field.required ?? true),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/** `/issue create` collects the title and description through a modal (M5-T03). */
|
||
export async function openIssueModal(interaction) {
|
||
assertGiteaConfigured();
|
||
await interaction.showModal(
|
||
modal('issue:create', 'Nouvelle issue Gitea', [
|
||
{ id: 'title', label: 'Titre', maxLength: 200 },
|
||
{ id: 'body', label: 'Description', paragraph: true, maxLength: 4000 },
|
||
]),
|
||
);
|
||
}
|
||
|
||
/** `/pr create` keeps the branch options and asks for the title through a modal. */
|
||
export async function openPullRequestModal(interaction) {
|
||
assertGiteaConfigured();
|
||
const head = interaction.options.getString('head', true);
|
||
const base = interaction.options.getString('base', true);
|
||
|
||
remember(`pr:${interaction.user.id}`, { head, base }, { ttlMs: PR_CONTEXT_TTL_MS });
|
||
await interaction.showModal(
|
||
modal('pr:create', 'Nouvelle pull request', [
|
||
{ id: 'title', label: `Titre (${head} → ${base})`, maxLength: 200 },
|
||
{ id: 'body', label: 'Description', paragraph: true, maxLength: 4000, required: false },
|
||
]),
|
||
);
|
||
}
|
||
|
||
/** Turns an API failure into a message the user can act on (M5-T08). */
|
||
export async function replyGiteaFailure(interaction, error) {
|
||
log.error('Gitea request failed', error);
|
||
const detail =
|
||
error instanceof ExternalApiError && /not configured/.test(error.message)
|
||
? 'La partie Gitea n’est pas encore configurée : il manque `GITEA_URL` et `GITEA_TOKEN` dans le `.env`.'
|
||
: 'Gitea ne répond pas pour le moment. Réessaie dans quelques instants — si ça persiste, préviens l’équipe.';
|
||
const payload = { embeds: [errorEmbed(detail)], flags: MessageFlags.Ephemeral };
|
||
|
||
if (interaction.deferred || interaction.replied) {
|
||
await interaction.followUp(payload).catch(() => {});
|
||
} else {
|
||
await interaction.reply(payload).catch(() => {});
|
||
}
|
||
}
|
||
|
||
export async function submitIssueModal(interaction) {
|
||
assertGiteaConfigured();
|
||
await interaction.deferReply();
|
||
|
||
try {
|
||
const issue = await gitea.createIssue({
|
||
title: interaction.fields.getTextInputValue('title'),
|
||
body: `${interaction.fields.getTextInputValue('body')}\n\n_Créée depuis Discord par ${interaction.user.tag}._`,
|
||
});
|
||
logEvent({
|
||
guildId: interaction.guildId,
|
||
type: 'gitea.issue.created',
|
||
actorId: interaction.user.id,
|
||
payload: { number: issue.number },
|
||
});
|
||
await interaction.editReply({
|
||
embeds: [issueEmbed(issue)],
|
||
components: linkButtons([{ label: 'Ouvrir sur Gitea', url: issue.html_url }]),
|
||
});
|
||
} catch (error) {
|
||
await replyGiteaFailure(interaction, error);
|
||
}
|
||
}
|
||
|
||
export async function submitPullRequestModal(interaction) {
|
||
assertGiteaConfigured();
|
||
const context = consume(`pr:${interaction.user.id}`);
|
||
if (!context) {
|
||
throw new UserFacingError(
|
||
'La sélection des branches a expiré : relance `/pr create` pour les choisir à nouveau.',
|
||
);
|
||
}
|
||
|
||
await interaction.deferReply();
|
||
try {
|
||
const pull = await gitea.createPullRequest({
|
||
title: interaction.fields.getTextInputValue('title'),
|
||
body: `${interaction.fields.getTextInputValue('body')}\n\n_Créée depuis Discord par ${interaction.user.tag}._`.trim(),
|
||
head: context.head,
|
||
base: context.base,
|
||
});
|
||
logEvent({
|
||
guildId: interaction.guildId,
|
||
type: 'gitea.pull_request.created',
|
||
actorId: interaction.user.id,
|
||
payload: { number: pull.number, head: context.head, base: context.base },
|
||
});
|
||
await interaction.editReply({
|
||
embeds: [pullRequestEmbed(pull)],
|
||
components: linkButtons([{ label: 'Ouvrir sur Gitea', url: pull.html_url }]),
|
||
});
|
||
} catch (error) {
|
||
await replyGiteaFailure(interaction, error);
|
||
}
|
||
}
|
||
|
||
/** Routes the modal submissions opened by `/issue` and `/pr` (M5-T09). */
|
||
export async function handleGiteaModal(interaction) {
|
||
switch (interaction.customId) {
|
||
case 'issue:create':
|
||
await submitIssueModal(interaction);
|
||
return;
|
||
case 'pr:create':
|
||
await submitPullRequestModal(interaction);
|
||
return;
|
||
default:
|
||
log.debug(`Ignored unknown modal ${interaction.customId}`);
|
||
}
|
||
}
|