Files
wekan/models/lib/externalExportFormatters.js
T
Lauri Ojansivu 958e9e13f3 Import Scrum sprints and releases from GitLab, OpenProject and Asana.
Only the Jira importer created sprints and releases. GitLab iterations and
milestones became nothing but milestone: and iteration: labels, OpenProject
versions only a version: label, and Asana milestones and Trello produced
nothing Scrum-related, so a board moved from those tools lost its planning.

Imports now map what each export really carries, through the same journaled
Scrum import stage as Jira and WeKan JSON, so an interrupted import is
recovered the same way. GitLab iterations become sprints and milestones
releases, with their dates and state. OpenProject versions become releases
and its Sprint resources sprints; position sets the backlog rank and story
points become the board's estimate field. Asana milestone tasks become
releases for themselves and the tasks they wait for. Trello has no sprints or
releases; its Power-Up data is counted in the import loss report.

As for Jira, a started sprint is imported as planned and a finished one not
at all, because the exports have no commitment or close snapshot; both are
reported. Malformed dates, unknown states, duplicate names and cards in an
iteration that is not imported are reported, never invented. The milestone
and iteration labels stay for List Sync and existing boards. The import
page's Scrum settings part now also decides whether external imports bring
their planning in. GitLab and OpenProject exports write each card's sprint
and first release back so a board round-trips.

Thanks to xet7 !
2026-10-08 12:08:29 +03:00

317 lines
17 KiB
JavaScript

// Export to other tools: one formatter per format, each emitting the shape that
// WeKan's own importer for that tool reads (externalParsers.js, jiraCreator.js,
// jiraIssueExtras.js), so a board round-trips. Pure - the collection from the
// database is externalExporters.js - so tests/externalExportRoundTrip.test.cjs
// runs every export back through its parser in plain Node.
//
// Each exported item is what collect() gathered for one card, already gated by
// the export selection (#1173): title, description, listTitle, swimlaneTitle,
// labels, dueAt, and - when selected and the format has a place for them -
// startAt, endAt, createdAt, owner, assignees, creator, requestedBy, comments,
// checklists, parentCardId and customFields. A format drops what it has no
// field for; that is the honest answer, not a loss this module invents.
import { formatMarkdownKanban } from './markdownKanbanFormat.js';
import { formatLeo } from './leoOutlineFormat.js';
import { formatTodoTxt } from './todoTxtFormat.js';
import { formatTaskwarrior } from './taskwarriorFormat.js';
import { formatFocalboard } from './focalboardFormat.js';
import { formatTodoistCsv } from './todoistCsvFormat.js';
import { formatOpml } from './opmlOutlineFormat.js';
import { formatOrgMode } from './orgModeFormat.js';
// A WeKan list maps to a "closed" issue state when its name looks terminal.
function isClosed(listTitle) {
return /done|closed|complete|archiv|finished/i.test(listTitle || '');
}
const has = value => value !== undefined && value !== null && value !== '';
const list = value => (Array.isArray(value) ? value : []);
const checklistItems = item => list(item.checklists).flatMap(c => list(c.items));
const githubLike = ({ items }) =>
items.map(i => ({
title: i.title,
body: i.description,
state: isClosed(i.listTitle) ? 'closed' : 'open',
labels: i.labels.map(name => ({ name })),
due_date: i.dueAt,
}));
// OpenProject reads ids from HAL hrefs ending in a number.
const numericIds = items => new Map(items.map((item, index) => [item.cardId, index + 1]));
// Sprints and releases, for the formats with a place for them (GitLab
// iterations and milestones, OpenProject sprints and versions), numbered as
// those APIs number theirs. What the importer reads back is
// models/lib/externalScrumPlanning.js. A cancelled sprint or release has no
// equivalent in either and is left out, with its cards unassigned.
const day = value => (value ? new Date(value).toISOString().slice(0, 10) : undefined);
function planningIndex({ scrumSprints, scrumReleases }) {
const numbered = rows => new Map(list(rows).filter(row => row && row.state !== 'cancelled')
.map((row, index) => [row._id, { ...row, number: index + 1 }]));
return { sprints: numbered(scrumSprints), releases: numbered(scrumReleases) };
}
const GITLAB_ITERATION_STATE = { planned: 1, active: 2, closed: 3 };
const OPENPROJECT_SPRINT_STATUS = { planned: 'in_planning', active: 'active', closed: 'completed' };
const OPENPROJECT_SPRINT_TITLE = { planned: 'In planning', active: 'Active', closed: 'Completed' };
const sprintOf = (planning, item) => (item.scrum && planning.sprints.get(item.scrum.sprintId)) || null;
const releaseOf = (planning, item) => (item.scrum && planning.releases.get(item.scrum.firstReleaseId)) || null;
// Backlog rank as OpenProject's integer position: 1..n in rank order.
function positions(items) {
const ranked = items.filter(i => i.scrum && typeof i.scrum.backlogRank === 'number')
.sort((a, b) => a.scrum.backlogRank - b.scrum.backlogRank);
return new Map(ranked.map((item, index) => [item.cardId, index + 1]));
}
export const formatters = {
// NextCloud Deck: board with stacks, each stack carrying its cards.
deck: ({ board, lists, items }) => ({
title: board.title,
stacks: lists.map(l => ({
title: l.title,
cards: items
.filter(i => i.listTitle === l.title)
.map(i => ({
title: i.title,
description: i.description,
duedate: i.dueAt,
...(Object.keys(i.timetracking || {}).length ? { timetracking: i.timetracking } : {}),
labels: i.labels.map(name => ({ title: name })),
...(has(i.createdAt) ? { createdAt: i.createdAt } : {}),
...(has(i.endAt) ? { done: i.endAt } : {}),
...(has(i.owner) || list(i.assignees).length
? { assignedUsers: [i.owner, ...list(i.assignees)].filter(has).map(uid => ({ participant: { uid } })) } : {}),
...(has(i.creator) ? { owner: { uid: i.creator } } : {}),
...(list(i.comments).length ? { comments: i.comments.map(c => ({
message: c.text, actorId: c.author, creationDateTime: c.date })) } : {}),
})),
})),
}),
// Kanboard: columns, swimlanes and tasks, the shape the Kanboard importer reads.
kanboard: ({ board, lists, swimlanes, items }) => ({
board: { name: board.title },
columns: lists.map(l => ({ title: l.title })),
swimlanes: swimlanes.map(s => ({ name: s.title })),
tasks: items.map(i => ({
title: i.title,
description: i.description,
column_name: i.listTitle,
swimlane_name: i.swimlaneTitle,
date_due: i.dueAt,
...(has(i.startAt) ? { date_started: i.startAt } : {}),
...(has(i.endAt) ? { date_completed: i.endAt } : {}),
...(has(i.createdAt) ? { date_creation: i.createdAt } : {}),
...(has(i.owner) ? { owner_username: i.owner } : {}),
...(has(i.creator) ? { creator_username: i.creator } : {}),
tags: i.labels,
...(checklistItems(i).length ? { subtasks: checklistItems(i).map(x => ({ title: x.title, status: x.done ? 2 : 0 })) } : {}),
...(list(i.comments).length ? { comments: i.comments.map(c => ({
comment: c.text, username: c.author, date_creation: c.date })) } : {}),
})),
}),
// OpenProject: a work-packages collection with HAL links.
openproject: collected => {
const { items } = collected;
const ids = numericIds(items);
const planning = planningIndex(collected);
const ranks = positions(items);
const versions = [...planning.releases.values()].map(r => ({ _type: 'Version', id: r.number, name: r.name,
...(has(r.notes) ? { description: { raw: r.notes } } : {}),
...(r.plannedStart ? { startDate: day(r.plannedStart) } : {}), ...(r.plannedEnd ? { endDate: day(r.plannedEnd) } : {}),
status: r.state === 'released' ? 'closed' : 'open' }));
const sprints = [...planning.sprints.values()].map(sp => ({ _type: 'Sprint', id: sp.number, name: sp.name,
...(has(sp.goal) ? { description: { raw: sp.goal } } : {}),
...(sp.plannedStart ? { startDate: day(sp.plannedStart) } : {}), ...(sp.plannedEnd ? { finishDate: day(sp.plannedEnd) } : {}),
_links: { self: { href: `/api/v3/sprints/${sp.number}`, title: sp.name },
status: { href: `urn:openproject-org:api:v3:sprints:status:${OPENPROJECT_SPRINT_STATUS[sp.state] || 'in_planning'}`,
title: OPENPROJECT_SPRINT_TITLE[sp.state] || 'In planning' } } }));
const fieldNames = [...new Set(items.flatMap(i => Object.keys(i.customFields || {})))];
const fieldKey = name => `customField${fieldNames.indexOf(name) + 1}`;
return {
_embedded: {
...(fieldNames.length ? { schemas: [Object.fromEntries(fieldNames.map(name => [fieldKey(name), { name }]))] } : {}),
...(versions.length ? { versions } : {}),
...(sprints.length ? { sprints } : {}),
elements: items.map(i => ({
id: ids.get(i.cardId),
subject: i.title,
description: { raw: i.description },
dueDate: i.dueAt,
...(has(i.startAt) ? { startDate: i.startAt } : {}),
...(has(i.createdAt) ? { createdAt: i.createdAt } : {}),
...(ranks.has(i.cardId) ? { position: ranks.get(i.cardId) } : {}),
...Object.fromEntries(Object.entries(i.customFields || {}).map(([name, value]) => [fieldKey(name), value])),
_links: {
status: { title: i.listTitle },
...(releaseOf(planning, i) ? { version: { href: `/api/v3/versions/${releaseOf(planning, i).number}`,
title: releaseOf(planning, i).name } } : {}),
...(sprintOf(planning, i) ? { sprint: { href: `/api/v3/sprints/${sprintOf(planning, i).number}`,
title: sprintOf(planning, i).name } } : {}),
...(has(i.owner) ? { assignee: { title: i.owner } } : {}),
...(list(i.assignees).length ? { responsible: { title: i.assignees[0] } } : {}),
...(has(i.creator) ? { author: { title: i.creator } } : {}),
...(ids.has(i.parentCardId) ? { parent: { href: `/api/v3/work_packages/${ids.get(i.parentCardId)}` } } : {}),
},
...(list(i.comments).length ? { _embedded: { activities: i.comments.map(c => ({
_type: 'Activity::Comment', comment: { raw: c.text }, createdAt: c.date,
_links: { user: { title: c.author } } })) } } : {}),
})),
},
};
},
// GitHub / Gitea / Forgejo: an issues array.
github: githubLike,
gitea: githubLike,
forgejo: githubLike,
// GitLab: an Issues API v4 array, carrying what parseGitlab reads (#2698):
// every assignee, the author, creation and close dates and comments as
// notes. No iid: a re-import would then add a second "Source:" line.
// Sprints and releases go out as each issue's iteration and milestone.
gitlab: collected => {
const planning = planningIndex(collected);
return collected.items.map(i => ({
title: i.title,
description: i.description,
state: isClosed(i.listTitle) ? 'closed' : 'opened',
labels: i.labels,
due_date: i.dueAt,
...(has(i.owner) ? { assignees: [i.owner, ...list(i.assignees)].map(username => ({ username })) } : {}),
...(has(i.creator) ? { author: { username: i.creator } } : {}),
...(has(i.createdAt) ? { created_at: i.createdAt } : {}),
...(isClosed(i.listTitle) && has(i.endAt) ? { closed_at: i.endAt } : {}),
...(list(i.comments).length ? { notes: i.comments.map(c => ({ body: c.text,
author: { username: c.author }, created_at: c.date, system: false })) } : {}),
...(sprintOf(planning, i) ? { iteration: (sp => ({ id: sp.number, iid: sp.number, title: sp.name,
description: sp.goal || '', state: GITLAB_ITERATION_STATE[sp.state] || 1,
start_date: day(sp.plannedStart), due_date: day(sp.plannedEnd) }))(sprintOf(planning, i)) } : {}),
...(releaseOf(planning, i) ? { milestone: (r => ({ id: r.number, iid: r.number, title: r.name,
description: r.notes || '', state: r.state === 'released' ? 'closed' : 'active',
start_date: day(r.plannedStart), due_date: day(r.plannedEnd) }))(releaseOf(planning, i)) } : {}),
}));
},
// Trello board JSON (round-trips with WeKan's Trello import).
trello: ({ board, lists, items }) => ({
name: board.title,
prefs: { background: 'blue', permissionLevel: 'private' },
labels: (board.labels || []).map(l => ({ id: l._id, name: l.name, color: l.color })),
lists: lists.map(l => ({ id: l._id, name: l.title, closed: false })),
cards: items.map(i => ({
id: i.cardId,
name: i.title,
desc: i.description,
idList: i.listId,
due: i.dueAt || null,
closed: false,
idLabels: i.labelIds,
idMembers: [],
idChecklists: [],
})),
checklists: [],
actions: [],
}),
// Jira issues collection (round-trips with WeKan's Jira import).
jira: ({ board, items, jiraEstimateMapping }) => {
const keys = new Map(items.map((item, idx) => [item.cardId, `WEKAN-${idx + 1}`]));
const fieldNames = [...new Set(items.flatMap(i => Object.keys(i.customFields || {})))];
const fieldKey = name => `customfield_${90000 + fieldNames.indexOf(name) + 1}`;
return {
board: { name: board.title },
...(jiraEstimateMapping ? {
wekanScrumMapping: { estimateFieldId: jiraEstimateMapping.estimateFieldId, estimateUnit: jiraEstimateMapping.estimateUnit },
schema: { [jiraEstimateMapping.estimateFieldId]: { type: 'number' } },
} : {}),
...(fieldNames.length ? { names: Object.fromEntries(fieldNames.map(name => [fieldKey(name), name])) } : {}),
issues: items.map(i => ({
key: keys.get(i.cardId),
fields: {
summary: i.title,
description: i.description,
status: { name: i.listTitle, ...(i.jiraScrum?.statusCategory ? { statusCategory: i.jiraScrum.statusCategory } : {}) },
...(i.jiraScrum?.issuetype ? { issuetype: i.jiraScrum.issuetype } : {}),
...(i.jiraScrum?.fixVersions ? { fixVersions: i.jiraScrum.fixVersions } : {}),
...(i.jiraEstimate || {}),
labels: i.labels,
duedate: i.dueAt,
...(Object.keys(i.timetracking || {}).length ? { timetracking: i.timetracking } : {}),
...(has(i.createdAt) ? { created: i.createdAt } : {}),
...(has(i.owner) ? { assignee: { name: i.owner } } : {}),
...(has(i.requestedBy || i.creator) ? { reporter: { displayName: i.requestedBy || i.creator } } : {}),
...(keys.has(i.parentCardId) ? { parent: { key: keys.get(i.parentCardId) } } : {}),
...(checklistItems(i).length ? { subtasks: checklistItems(i).map(x => ({
fields: { summary: x.title, status: { statusCategory: { key: x.done ? 'done' : 'new' } } } })) } : {}),
...(list(i.comments).length ? { comment: { comments: i.comments.map(c => ({
body: c.text, author: { name: c.author }, created: c.date })) } } : {}),
...Object.fromEntries(Object.entries(i.customFields || {}).map(([name, value]) => [fieldKey(name), value])),
},
})),
};
},
// Asana tasks (sections become the board columns).
asana: ({ items }) => ({
data: items.map(i => ({
gid: i.cardId,
name: i.title,
notes: i.description,
completed: isClosed(i.listTitle),
...(isClosed(i.listTitle) && has(i.endAt) ? { completed_at: i.endAt } : {}),
due_on: i.dueAt,
...(has(i.startAt) ? { start_on: i.startAt } : {}),
...(has(i.createdAt) ? { created_at: i.createdAt } : {}),
...(has(i.owner) ? { assignee: { name: i.owner } } : {}),
...(has(i.parentCardId) ? { parent: { gid: i.parentCardId } } : {}),
memberships: [{ section: { name: i.listTitle } }],
tags: i.labels.map(name => ({ name })),
...(Object.keys(i.customFields || {}).length ? { custom_fields: Object.entries(i.customFields).map(([name, value]) =>
(typeof value === 'number' ? { name, number_value: value } : { name, text_value: Array.isArray(value) ? value.join(', ') : String(value) })) } : {}),
...(checklistItems(i).length ? { subtasks: checklistItems(i).map(x => ({ name: x.title, completed: Boolean(x.done) })) } : {}),
...(list(i.comments).length ? { stories: i.comments.map(c => ({
resource_subtype: 'comment_added', text: c.text, created_by: { name: c.author }, created_at: c.date })) } : {}),
})),
}),
// ZenKit-style export (stages + items), the adapter shape the importer reads.
zenkit: ({ board, lists, items }) => ({
title: board.title,
stages: lists.map(l => ({ name: l.title })),
items: items.map(i => ({
id: i.cardId,
title: i.title,
description: i.description,
stage_name: i.listTitle,
due: i.dueAt,
tags: i.labels,
...(has(i.startAt) ? { start: i.startAt } : {}),
...(has(i.createdAt) ? { created_at: i.createdAt } : {}),
...(has(i.parentCardId) ? { parent_id: i.parentCardId } : {}),
...(has(i.owner) || list(i.assignees).length ? { assignees: [i.owner, ...list(i.assignees)].filter(has) } : {}),
...(Object.keys(i.customFields || {}).length ? { fields: i.customFields } : {}),
...(list(i.checklists).length ? { checklists: i.checklists.map(c => ({
name: c.title, items: list(c.items).map(x => ({ text: x.title, checked: Boolean(x.done) })) })) } : {}),
...(list(i.comments).length ? { comments: i.comments.map(c => ({ text: c.text, author: c.author, date: c.date })) } : {}),
})),
}),
// Markdown "task list" kanban - the convention several markdown-kanban tools
// use (Obsidian Kanban and similar): `## List name` headings, `- [ ]`/`- [x]`
// items underneath, indented continuation lines as the item's description.
// Round-trips with parseMarkdownKanban in externalParsers.js. Returns a
// plain string, not an object - the one formatter here that does.
markdown: formatMarkdownKanban,
// The Leo literate editor's .leo outline (XML): lists, cards and checklists
// as nested nodes. Round-trips with parseLeo in leoOutline.js.
leo: formatLeo,
// todo.txt: one task per line; round-trips with parseTodoTxt (todoTxtFormat.js).
todotxt: formatTodoTxt,
// Taskwarrior's `task import` JSON; round-trips with parseTaskwarrior (taskwarriorFormat.js).
taskwarrior: formatTaskwarrior,
// Focalboard's archive text (header line and board.jsonl); round-trips with
// parseFocalboard (focalboardFormat.js).
focalboard: data => formatFocalboard(data),
// A Todoist project template (CSV); round-trips with parseTodoistCsv (todoistCsvFormat.js).
todoist: formatTodoistCsv,
// An OPML outline; round-trips with parseOpml (opmlOutline.js, server-only).
opml: formatOpml,
// An Org mode outline; round-trips with parseOrgMode (orgModeFormat.js).
orgmode: formatOrgMode,
};
export const EXTERNAL_EXPORT_FORMATS = Object.keys(formatters);