feat(gitea): add embeds, modals and the issue and pull request commands
Creating an issue or a pull request needs a title and a description, which do not fit in a slash command option, so the branches chosen as options are kept in memory for a few minutes until the modal is submitted.
This commit is contained in:
7 files changed
+470
-12
No files matched your search
@@ -0,0 +1,148 @@
|
||||
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('Gitea n’est pas configuré : renseigne `GITEA_URL` et `GITEA_TOKEN`.');
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
? 'Gitea n’est pas configuré.'
|
||||
: 'Gitea est momentanément indisponible. Réessaie dans quelques instants.';
|
||||
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('Session expirée : relance `/pr create` pour choisir à nouveau les branches.');
|
||||
}
|
||||
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user