Files
wekan/models/lib/importParts.js
T
Lauri Ojansivu 51238ed8ad Import: attachment files of Jira, Nextcloud Deck, OpenProject, Asana and GitLab.
Their exports name attachments without carrying the files, so the files
were reported as a loss. Each service documents where a file is
downloaded, behind the same sign-in as its API:

- Jira: fields.attachment[].content (/rest/api/3/attachment/content/<id>),
  Basic email:API token; Jira's redirect to its media store is followed
  without the sign-in.
- Deck: /index.php/apps/deck/api/v1.0|v1.1/boards/../stacks/../cards/..
  /attachments/<id>, Basic user:app password, OCS-APIRequest: true.
- OpenProject: _links.downloadLocation, relative to the instance, Basic
  apikey:<key>.
- Asana: GET /attachments/<gid>?opt_fields=download_url with a personal
  access token, then the signed download_url without it; a file the
  export embeds (export_boards.pl) is taken as it is; one kept on Google
  Drive and the like is a link, reported.
- GitLab: a private project's /uploads/ link through
  /api/v4/projects/<id>/uploads/<secret>/<file> (17.4+), Bearer token.

The import page asks for the address, user and token under "Attachment
files" for these five (the REST import takes the same as attachmentAuth).
They go with that one import: held beside the creator in a WeakMap, never
stored or logged, the token field emptied on Import, and sent ONLY to
the origin the member typed - an export names any URL it likes. Every
download is the import's SSRF-guarded fetch, which validates each hop
and drops Authorization on a cross-origin redirect. A file that cannot
be fetched is on the loss report with its reason. Unticking Attachments
leaves them out (Jira's per-issue field is pruned too).

Zenkit is left out: it publishes no way to download a file.

Tests: importAttachmentAuth.test.cjs (each service's request, the
sign-in only to its origin, malformed sign-ins, never stored or
logged), the parser tests pin the attachment lists, a Meteor test
imports an embedded Asana file onto its card and shows a Jira file is
reported without a sign-in and that the sign-in never reaches an address
the guard refuses, and import-attachment-auth.e2e.js the page (not run
here). Not run against the live services.

Thanks to xet7 !
2026-10-15 01:27:26 +03:00

88 lines
3.5 KiB
JavaScript

'use strict';
// WHAT TO IMPORT, applied to the document before a creator sees it (#1173).
//
// The selection popup is one list, and on an export it says what goes out. On
// an IMPORT it says what comes in - and the honest way to make that true for
// every source is to take the parts out of the parsed document rather than to
// teach five creators (WeKan, Trello, Jira, CSV, Kanboard) a selection each.
// A creator that never sees a comment cannot import one.
//
// The keys are the ones a WeKan export writes, plus the names Trello and the
// Kanboard-shaped parsers use for the same things, because the pruning happens
// after parsing and before creation - the one point every source passes through.
//
// A section that is NOT in the selection is emptied, never deleted: the
// creators read `board.cards`, `board.checklists` and so on directly, and an
// undefined array is a crash where an empty one is "there were none".
// selection key -> the arrays it covers, whatever the source calls them
const PART_ARRAYS = {
comments: ['comments', 'cardComments', 'actions'],
checklists: ['checklists', 'checklistItems'],
attachments: ['attachments'],
'custom-fields': ['customFields'],
activities: ['activities'],
subtasks: ['subtaskItems'],
};
function pruneImportDocument(doc, fields) {
// No selection is "everything", the same rule every exporter follows.
if (!doc || typeof doc !== 'object' || !Array.isArray(fields) || fields.length === 0) {
return doc;
}
const wanted = new Set(fields);
if (!wanted.has('scrum') || !wanted.has('custom-fields')) delete doc.wekanScrumMapping;
// Jira time tracking is nested in issues rather than top-level customFields.
// Prune both supported representations so fallback fields cannot restore a
// value the user explicitly excluded in the existing import selector.
for (const issue of Array.isArray(doc.issues) ? doc.issues : []) {
const values = issue?.fields;
if (!values || typeof values !== 'object') continue;
const tracking = values.timetracking;
if (!wanted.has('scrum')) {
delete values.issuetype;
if (values.status && typeof values.status === 'object') delete values.status.statusCategory;
}
if (!wanted.has('custom-fields')) {
delete values.timeoriginalestimate;
delete values.timeestimate;
if (tracking && typeof tracking === 'object') {
delete tracking.originalEstimateSeconds;
delete tracking.remainingEstimateSeconds;
delete tracking.originalEstimate;
delete tracking.remainingEstimate;
}
}
// Jira's attachments are per issue, not a top-level array.
if (!wanted.has('attachments')) delete values.attachment;
if (!wanted.has('dates')) {
delete values.timespent;
if (tracking && typeof tracking === 'object') {
delete tracking.timeSpentSeconds;
delete tracking.timeSpent;
}
}
}
if (!wanted.has('scrum')) {
delete doc.scrumTransfer;
delete doc.scrumTransferLosses;
for (const row of [doc, ...(doc.cards || []), ...(doc.lists || []), ...(doc.swimlanes || [])]) {
delete row.scrum;
delete row.scrumRevision;
delete row.scrumImportLosses;
}
}
// An array is only emptied when its key is a KNOWN part that was not chosen -
// an unknown key is somebody else's data and is left alone.
for (const [key, arrays] of Object.entries(PART_ARRAYS)) {
if (wanted.has(key)) continue;
for (const name of arrays) {
if (Array.isArray(doc[name])) doc[name] = [];
}
}
return doc;
}
export { pruneImportDocument, PART_ARRAYS };