The import only existed on a repository board and always landed in the first column. The organization board is exactly where issues from several repositories belong together, so the button now lives on every board and asks which repository, which column, and whether closed issues come too. An issue already on the board is skipped rather than duplicated, so the button survives being pressed twice — which the tests check against a stand-in forge, along with the due date and labels carried over. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
482 lines
15 KiB
JavaScript
482 lines
15 KiB
JavaScript
import { api } from '../core/api.js';
|
||
import { formatRelative, h, mount } from '../core/dom.js';
|
||
import { autoRefresh } from '../core/live.js';
|
||
import {
|
||
badge,
|
||
button,
|
||
card,
|
||
confirmDangerous,
|
||
field,
|
||
input,
|
||
modal,
|
||
notice,
|
||
renderAsync,
|
||
select,
|
||
spinner,
|
||
table,
|
||
textarea,
|
||
toast,
|
||
toggle,
|
||
} from '../core/ui.js';
|
||
|
||
/** Kanban boards: one per repository, one for the organization (M18). */
|
||
|
||
const LEVELS = { STAFF: 1, DEV: 2, OWNER: 3 };
|
||
|
||
const overdue = (dueAt) => dueAt && new Date(`${dueAt}Z`) < new Date();
|
||
|
||
// --- Board list --------------------------------------------------------------
|
||
|
||
function renderList(host, state) {
|
||
const reload = () => renderList(host, state);
|
||
|
||
return renderAsync(
|
||
host,
|
||
async () => {
|
||
const [projects, repositories] = await Promise.all([
|
||
api.get('/api/projects'),
|
||
api.get('/api/repositories').catch(() => ({ repositories: [] })),
|
||
]);
|
||
return { ...projects, repositories: repositories.repositories ?? [] };
|
||
},
|
||
(data) => {
|
||
const taken = new Set(data.boards.map((entry) => entry.repository).filter(Boolean));
|
||
const available = data.repositories.filter((entry) => !taken.has(entry.fullName));
|
||
|
||
return h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
h(
|
||
'div',
|
||
{ class: 'page-head' },
|
||
h(
|
||
'div',
|
||
{},
|
||
h('h1', { text: 'Projets' }),
|
||
h('p', {
|
||
class: 'muted',
|
||
text: 'Un tableau par dépôt, plus un pour l’organisation. Les cartes peuvent pointer vers une issue Gitea.',
|
||
}),
|
||
),
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
!data.orgBoard &&
|
||
button('Tableau de l’organisation', () => createBoard({ scope: 'org' }, reload), {
|
||
tone: 'primary',
|
||
}),
|
||
available.length > 0 &&
|
||
button('Tableau de dépôt', () => createBoard({ scope: 'repo', available }, reload), {
|
||
tone: available.length > 0 && data.orgBoard ? 'primary' : 'ghost',
|
||
}),
|
||
),
|
||
),
|
||
|
||
data.boards.length === 0 &&
|
||
notice(
|
||
'Aucun tableau pour l’instant. Commence par celui de l’organisation, puis ajoute ceux des dépôts.',
|
||
'info',
|
||
),
|
||
|
||
card(
|
||
'Tableaux',
|
||
{ subtitle: `${data.boards.length} tableau(x)` },
|
||
table(
|
||
[
|
||
{
|
||
label: 'Tableau',
|
||
render: (entry) =>
|
||
h(
|
||
'div',
|
||
{},
|
||
h('strong', { text: entry.name }),
|
||
h('div', {
|
||
class: 'muted',
|
||
text: entry.scope === 'org' ? 'Organisation' : entry.repository,
|
||
}),
|
||
),
|
||
},
|
||
{
|
||
label: 'Portée',
|
||
render: (entry) =>
|
||
entry.scope === 'org' ? badge('Organisation', 'info') : badge('Dépôt', 'neutral'),
|
||
},
|
||
{ label: 'Colonnes', className: 'num', key: 'columns' },
|
||
{ label: 'Cartes', className: 'num', key: 'cards' },
|
||
{
|
||
label: 'Modifié',
|
||
className: 'nowrap',
|
||
render: (entry) => formatRelative(entry.updatedAt),
|
||
},
|
||
],
|
||
data.boards,
|
||
{
|
||
empty: 'Aucun tableau.',
|
||
rowHref: (entry) => `#/projects/${entry.id}`,
|
||
},
|
||
),
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
async function createBoard({ scope, available = [] }, reload) {
|
||
const name = input({ placeholder: scope === 'org' ? 'Organisation FWS' : 'Nom du tableau' });
|
||
const repository =
|
||
scope === 'repo'
|
||
? select(
|
||
available.map((entry) => ({ value: entry.fullName, label: entry.fullName })),
|
||
{},
|
||
)
|
||
: null;
|
||
const description = input({ placeholder: 'À quoi sert ce tableau ?' });
|
||
|
||
const confirmed = await modal(
|
||
scope === 'org' ? 'Tableau de l’organisation' : 'Nouveau tableau de dépôt',
|
||
() => ({
|
||
node: h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
repository && field('Dépôt', repository),
|
||
field('Nom', name, { help: 'Laisse vide pour reprendre le nom du dépôt.' }),
|
||
field('Description', description),
|
||
h('p', {
|
||
class: 'muted',
|
||
text: 'Les colonnes À faire, En cours, En revue et Terminé sont créées automatiquement.',
|
||
}),
|
||
),
|
||
submit: () => scope === 'org' || Boolean(repository?.value),
|
||
}),
|
||
{ confirmLabel: 'Créer', tone: 'primary' },
|
||
);
|
||
if (!confirmed) return;
|
||
|
||
await api.post('/api/projects', {
|
||
scope,
|
||
repository: repository?.value,
|
||
name: name.value.trim(),
|
||
description: description.value.trim(),
|
||
});
|
||
toast('Tableau créé.');
|
||
reload();
|
||
}
|
||
|
||
// --- Board -------------------------------------------------------------------
|
||
|
||
function cardNode(entry, issues, board, reload) {
|
||
const issue = entry.issueRef ? issues[entry.issueRef] : null;
|
||
|
||
const node = h(
|
||
'article',
|
||
{ class: 'kcard', draggable: 'true', dataset: { card: String(entry.id) } },
|
||
h('h4', { text: entry.title }),
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
entry.labels.map((label) => badge(label, 'neutral')),
|
||
issue &&
|
||
badge(
|
||
`${entry.issueRef} · ${issue.state === 'open' ? 'ouverte' : 'fermée'}`,
|
||
issue.state === 'open' ? 'success' : 'neutral',
|
||
),
|
||
!issue && entry.issueRef && badge(entry.issueRef, 'neutral'),
|
||
entry.dueAt && badge(formatRelative(entry.dueAt), overdue(entry.dueAt) ? 'danger' : 'warning'),
|
||
),
|
||
entry.assigneeId && h('div', { class: 'muted', text: `Assignée à ${entry.assigneeId}` }),
|
||
);
|
||
|
||
node.addEventListener('dragstart', (event) => {
|
||
event.dataTransfer.setData('text/plain', String(entry.id));
|
||
event.dataTransfer.effectAllowed = 'move';
|
||
node.classList.add('dragging');
|
||
});
|
||
node.addEventListener('dragend', () => node.classList.remove('dragging'));
|
||
node.addEventListener('click', () => editCard(board, entry, reload));
|
||
return node;
|
||
}
|
||
|
||
function columnNode(column, issues, board, reload) {
|
||
const count = column.cards.length;
|
||
const over = column.wipLimit > 0 && count > column.wipLimit;
|
||
|
||
const list = h(
|
||
'div',
|
||
{ class: 'kcolumn-cards' },
|
||
column.cards.map((entry) => cardNode(entry, issues, board, reload)),
|
||
);
|
||
|
||
const node = h(
|
||
'section',
|
||
{ class: over ? 'kcolumn kcolumn-over' : 'kcolumn', dataset: { column: String(column.id) } },
|
||
h(
|
||
'header',
|
||
{},
|
||
h('strong', { text: column.name }),
|
||
h(
|
||
'span',
|
||
{ class: 'muted' },
|
||
h('span', { text: column.wipLimit > 0 ? `${count} / ${column.wipLimit}` : String(count) }),
|
||
),
|
||
),
|
||
list,
|
||
button('+ Carte', () => editCard(board, { columnId: column.id }, reload), { tone: 'ghost' }),
|
||
);
|
||
|
||
// Dropping anywhere on the column appends; the API renumbers the rest.
|
||
node.addEventListener('dragover', (event) => {
|
||
event.preventDefault();
|
||
node.classList.add('kcolumn-target');
|
||
});
|
||
node.addEventListener('dragleave', () => node.classList.remove('kcolumn-target'));
|
||
node.addEventListener('drop', async (event) => {
|
||
event.preventDefault();
|
||
node.classList.remove('kcolumn-target');
|
||
|
||
const cardId = event.dataTransfer.getData('text/plain');
|
||
if (!cardId) return;
|
||
|
||
const result = await api.post(`/api/projects/${board.id}/cards/${cardId}/move`, {
|
||
columnId: column.id,
|
||
position: column.cards.length,
|
||
});
|
||
if (result.overloaded) toast(`« ${column.name} » dépasse sa limite de ${column.wipLimit}.`, 'info');
|
||
reload();
|
||
});
|
||
|
||
return node;
|
||
}
|
||
|
||
async function editCard(board, entry, reload) {
|
||
const existing = Boolean(entry.id);
|
||
const title = input({ value: entry.title ?? '', placeholder: 'Ce qu’il y a à faire' });
|
||
const body = textarea({ value: entry.body ?? '', placeholder: 'Contexte, critères d’acceptation…' });
|
||
const assignee = input({ value: entry.assigneeId ?? '', placeholder: 'Identifiant Discord (facultatif)' });
|
||
const issueRef = input({ value: entry.issueRef ?? '', placeholder: 'FWS/FWS-Arch#12 (facultatif)' });
|
||
const labels = input({ value: (entry.labels ?? []).join(', '), placeholder: 'bug, urgent' });
|
||
const dueAt = input({ type: 'date', value: (entry.dueAt ?? '').slice(0, 10) });
|
||
|
||
const confirmed = await modal(
|
||
existing ? 'Modifier la carte' : 'Nouvelle carte',
|
||
() => ({
|
||
node: h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
field('Titre', title),
|
||
field('Description', body),
|
||
field('Assignée à', assignee, { help: 'Identifiant Discord du membre qui la porte.' }),
|
||
field('Issue liée', issueRef, {
|
||
help: 'Format `organisation/depot#12`. Son état s’affiche sur la carte.',
|
||
}),
|
||
field('Étiquettes', labels, { help: 'Séparées par des virgules.' }),
|
||
field('Échéance', dueAt),
|
||
),
|
||
submit: () => title.value.trim().length >= 2,
|
||
}),
|
||
{ confirmLabel: existing ? 'Enregistrer' : 'Créer', tone: 'primary' },
|
||
);
|
||
if (!confirmed) return;
|
||
|
||
const payload = {
|
||
title: title.value.trim(),
|
||
body: body.value,
|
||
assigneeId: assignee.value.trim(),
|
||
issueRef: issueRef.value.trim(),
|
||
labels: labels.value
|
||
.split(',')
|
||
.map((label) => label.trim())
|
||
.filter(Boolean),
|
||
dueAt: dueAt.value || '',
|
||
};
|
||
|
||
try {
|
||
if (existing) await api.patch(`/api/projects/${board.id}/cards/${entry.id}`, payload);
|
||
else await api.post(`/api/projects/${board.id}/cards`, { ...payload, columnId: entry.columnId });
|
||
toast(existing ? 'Carte enregistrée.' : 'Carte créée.');
|
||
reload();
|
||
} catch (error) {
|
||
toast(error.message, 'error');
|
||
}
|
||
}
|
||
|
||
async function renderBoard(host, state, id) {
|
||
const level = state.me.accessLevel;
|
||
const reload = () => renderBoard(host, state, id);
|
||
autoRefresh(reload, { topics: ['mutation'], minIntervalMs: 3000 });
|
||
|
||
mount(host, spinner());
|
||
let board;
|
||
try {
|
||
board = await api.get(`/api/projects/${id}`);
|
||
} catch (error) {
|
||
mount(host, notice(error.message, 'error'));
|
||
return;
|
||
}
|
||
|
||
const total = board.columns.reduce((sum, column) => sum + column.cards.length, 0);
|
||
const done = board.columns
|
||
.filter((column) => column.isDone)
|
||
.reduce((sum, column) => sum + column.cards.length, 0);
|
||
|
||
mount(
|
||
host,
|
||
h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
h(
|
||
'div',
|
||
{ class: 'page-head' },
|
||
h(
|
||
'div',
|
||
{},
|
||
h('p', { class: 'muted' }, h('a', { href: '#/projects', text: '← Tous les tableaux' })),
|
||
h('h1', { text: board.name }),
|
||
h('p', {
|
||
class: 'muted',
|
||
text: board.description || (board.repository ?? 'Tableau de l’organisation'),
|
||
}),
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
badge(`${total} carte(s)`, 'neutral'),
|
||
badge(`${done} terminée(s)`, done > 0 ? 'success' : 'neutral'),
|
||
),
|
||
),
|
||
h(
|
||
'div',
|
||
{ class: 'row' },
|
||
button('Importer depuis Gitea', () => importIssues(board, reload), { tone: 'ghost' }),
|
||
button('Colonne', () => addColumn(board, reload), { tone: 'ghost' }),
|
||
level >= LEVELS.OWNER &&
|
||
button(
|
||
'Supprimer',
|
||
async () => {
|
||
if (
|
||
!(await confirmDangerous(
|
||
'Supprimer le tableau',
|
||
`« ${board.name} » et toutes ses cartes seront perdus.`,
|
||
board.name,
|
||
))
|
||
) {
|
||
return;
|
||
}
|
||
await api.delete(`/api/projects/${board.id}`, { confirm: board.name });
|
||
toast('Tableau supprimé.');
|
||
window.location.hash = '#/projects';
|
||
},
|
||
{ tone: 'danger' },
|
||
),
|
||
),
|
||
),
|
||
|
||
h(
|
||
'div',
|
||
{ class: 'kboard' },
|
||
board.columns.map((column) => columnNode(column, board.issues ?? {}, board, reload)),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Pulls Gitea issues onto the board.
|
||
*
|
||
* Offered on every board, not only on a repository one: the organization board
|
||
* is exactly where issues from several repositories belong together.
|
||
*/
|
||
async function importIssues(board, reload) {
|
||
const { repositories } = await api.get('/api/repositories').catch(() => ({ repositories: [] }));
|
||
|
||
const repository =
|
||
repositories.length > 0
|
||
? select(
|
||
repositories.map((entry) => ({ value: entry.fullName, label: entry.fullName })),
|
||
{ value: board.repository ?? repositories[0]?.fullName },
|
||
)
|
||
: input({ value: board.repository ?? '', placeholder: 'FWS/FWS-Arch' });
|
||
|
||
const column = select(
|
||
board.columns.map((entry) => ({ value: String(entry.id), label: entry.name })),
|
||
{ value: String((board.columns.find((entry) => !entry.isDone) ?? board.columns[0])?.id) },
|
||
);
|
||
let closedToo = false;
|
||
|
||
const confirmed = await modal(
|
||
'Importer depuis Gitea',
|
||
() => ({
|
||
node: h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
repositories.length === 0 &&
|
||
notice(
|
||
'La liste des dépôts est indisponible — saisis le dépôt à la main, au format `organisation/depot`.',
|
||
'warning',
|
||
),
|
||
field('Dépôt', repository),
|
||
field('Colonne d’arrivée', column),
|
||
toggle(false, (checked) => (closedToo = checked), 'Inclure aussi les issues fermées'),
|
||
h('p', {
|
||
class: 'muted',
|
||
text: 'Une issue déjà présente sur le tableau est ignorée : tu peux relancer l’import sans créer de doublon.',
|
||
}),
|
||
),
|
||
submit: () => repository.value.trim() !== '',
|
||
}),
|
||
{ confirmLabel: 'Importer', tone: 'primary' },
|
||
);
|
||
if (!confirmed) return;
|
||
|
||
try {
|
||
const result = await api.post(`/api/projects/${board.id}/import`, {
|
||
repository: repository.value.trim(),
|
||
columnId: Number(column.value),
|
||
state: closedToo ? 'all' : 'open',
|
||
});
|
||
|
||
toast(
|
||
result.imported === 0
|
||
? `Rien à importer : les ${result.found} issue(s) de ${result.repository} sont déjà sur le tableau.`
|
||
: `${result.imported} carte(s) créée(s) dans « ${result.column} », ${result.skipped} déjà présente(s).`,
|
||
result.imported === 0 ? 'info' : 'success',
|
||
);
|
||
reload();
|
||
} catch (error) {
|
||
toast(error.message, 'error');
|
||
}
|
||
}
|
||
|
||
async function addColumn(board, reload) {
|
||
const name = input({ placeholder: 'Nom de la colonne' });
|
||
const wipLimit = input({ type: 'number', value: 0, min: 0 });
|
||
let isDone = false;
|
||
|
||
const confirmed = await modal(
|
||
'Nouvelle colonne',
|
||
() => ({
|
||
node: h(
|
||
'div',
|
||
{ class: 'stack' },
|
||
field('Nom', name),
|
||
field('Limite de cartes', wipLimit, { help: '0 = pas de limite. Au-delà, la colonne est signalée.' }),
|
||
toggle(false, (checked) => (isDone = checked), 'Les cartes déposées ici sont terminées'),
|
||
),
|
||
submit: () => name.value.trim() !== '',
|
||
}),
|
||
{ confirmLabel: 'Ajouter', tone: 'primary' },
|
||
);
|
||
if (!confirmed) return;
|
||
|
||
await api.post(`/api/projects/${board.id}/columns`, {
|
||
name: name.value.trim(),
|
||
wipLimit: Number(wipLimit.value) || 0,
|
||
isDone,
|
||
});
|
||
toast('Colonne ajoutée.');
|
||
reload();
|
||
}
|
||
|
||
export function render(host, { params, state }) {
|
||
if (params.length >= 1) return renderBoard(host, state, params[0]);
|
||
return renderList(host, state);
|
||
}
|