feat(projects): pull Gitea issues onto any board
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>
This commit is contained in:
1 parent
8869c74b7b
commit
1c21f6ff53
4 files changed
+260
-21
No files matched your search
+30
-9
@@ -407,26 +407,40 @@ export function registerProjectRoutes(router) {
|
||||
{ access: ACCESS_LEVELS.STAFF },
|
||||
);
|
||||
|
||||
/** Turns the open issues of a repository into cards, skipping the ones already there. */
|
||||
/**
|
||||
* Pulls issues from Gitea onto the board.
|
||||
*
|
||||
* An issue already on the board is skipped rather than duplicated, so the
|
||||
* button can be pressed again after a sprint without cleaning up after it.
|
||||
*/
|
||||
router.post(
|
||||
'/api/projects/:id/import',
|
||||
async ({ params, body, actor }) => {
|
||||
const found = board(params.id);
|
||||
const repository = String(body.repository ?? found.repository ?? '');
|
||||
const parts = splitRepository(repository);
|
||||
if (!parts) throw badRequest('Dépôt attendu au format `organisation/depot`.');
|
||||
if (!isGiteaConfigured()) throw badRequest('Gitea n’est pas configuré.');
|
||||
|
||||
const repository = String(body.repository ?? found.repository ?? '').trim();
|
||||
const parts = splitRepository(repository);
|
||||
if (!parts) throw badRequest('Dépôt attendu au format `organisation/depot`.');
|
||||
|
||||
const state = body.state === 'all' ? 'all' : 'open';
|
||||
const columns = listColumns(found.id);
|
||||
const target = columns.find((column) => !column.is_done) ?? columns[0];
|
||||
if (!target) throw badRequest('Ce tableau n’a aucune colonne.');
|
||||
if (columns.length === 0) throw badRequest('Ce tableau n’a aucune colonne.');
|
||||
|
||||
// Falls back to the first column that is not the finished one: importing
|
||||
// straight into « Terminé » is never what anyone means.
|
||||
const requested = body.columnId ? getColumn(Number.parseInt(body.columnId, 10)) : null;
|
||||
const target =
|
||||
requested && requested.board_id === found.id
|
||||
? requested
|
||||
: (columns.find((column) => !column.is_done) ?? columns[0]);
|
||||
|
||||
const existing = new Set(
|
||||
listCards(found.id)
|
||||
.map((card) => card.issue_ref)
|
||||
.filter(Boolean),
|
||||
);
|
||||
const issues = await repos.listIssues(parts.owner, parts.repo, { state: 'open', limit: 50 });
|
||||
const issues = await repos.listIssues(parts.owner, parts.repo, { state, limit: 100 });
|
||||
|
||||
let imported = 0;
|
||||
for (const issue of issues) {
|
||||
@@ -440,13 +454,20 @@ export function registerProjectRoutes(router) {
|
||||
body: issue.body ? String(issue.body).slice(0, 2000) : null,
|
||||
issueRef: ref,
|
||||
labels: (issue.labels ?? []).map((label) => label.name),
|
||||
dueAt: issue.due_date ? String(issue.due_date).slice(0, 10) : null,
|
||||
createdBy: actor.userId,
|
||||
});
|
||||
imported += 1;
|
||||
}
|
||||
|
||||
audit(actor, 'web.project.import', { board: found.id, repository, imported });
|
||||
return { imported, skipped: issues.length - imported, column: target.name };
|
||||
audit(actor, 'web.project.import', { board: found.id, repository, state, imported });
|
||||
return {
|
||||
imported,
|
||||
skipped: issues.length - imported,
|
||||
found: issues.length,
|
||||
column: target.name,
|
||||
repository,
|
||||
};
|
||||
},
|
||||
{ access: ACCESS_LEVELS.STAFF },
|
||||
);
|
||||
|
||||
@@ -345,18 +345,7 @@ async function renderBoard(host, state, id) {
|
||||
h(
|
||||
'div',
|
||||
{ class: 'row' },
|
||||
board.repository &&
|
||||
button(
|
||||
'Importer les issues',
|
||||
async () => {
|
||||
const result = await api.post(`/api/projects/${board.id}/import`, {});
|
||||
toast(
|
||||
`${result.imported} carte(s) créée(s) dans « ${result.column} », ${result.skipped} déjà présente(s).`,
|
||||
);
|
||||
reload();
|
||||
},
|
||||
{ tone: 'ghost' },
|
||||
),
|
||||
button('Importer depuis Gitea', () => importIssues(board, reload), { tone: 'ghost' }),
|
||||
button('Colonne', () => addColumn(board, reload), { tone: 'ghost' }),
|
||||
level >= LEVELS.OWNER &&
|
||||
button(
|
||||
@@ -389,6 +378,73 @@ async function renderBoard(host, state, id) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 });
|
||||
|
||||
Reference in new issue
Block a user