mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
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 !
114 lines
5.4 KiB
JavaScript
114 lines
5.4 KiB
JavaScript
'use strict';
|
|
|
|
import { adfPlainText } from './externalParsers.js';
|
|
|
|
// What a Jira Cloud REST v3 issue carries beyond the fields JiraCreator has
|
|
// always mapped (summary, ADF description, status, labels, assignee, reporter,
|
|
// dates, time tracking, estimates, issue type and links), decided without a
|
|
// database so it can be tested in plain Node.
|
|
//
|
|
// tags priority, components and fix versions, as board labels
|
|
// comments fields.comment.comments, ADF bodies as plain text
|
|
// parentKey fields.parent.key, linked when that issue is imported too
|
|
// subtasks fields.subtasks that are NOT imported: a checklist instead
|
|
// custom_fields customfield_NNNNN values, named from the search response's
|
|
// `names` map (expand=names) when present
|
|
// attachments { name, url } to download (JiraCreator)
|
|
// unsupported an attachment with no address, and a parent outside the import
|
|
|
|
function jiraName(value) {
|
|
if (value === null || value === undefined) return undefined;
|
|
if (typeof value === 'string') return value || undefined;
|
|
if (typeof value === 'number' || typeof value === 'boolean') return value;
|
|
if (Array.isArray(value)) {
|
|
const parts = value.map(jiraName).filter(v => v !== undefined && v !== '');
|
|
return parts.length ? parts : undefined;
|
|
}
|
|
if (typeof value === 'object') {
|
|
if (value.type === 'doc') return adfPlainText(value) || undefined;
|
|
const named = value.value ?? value.name ?? value.displayName ?? value.key;
|
|
if (named !== undefined && named !== null && typeof named !== 'object') return named;
|
|
if (value.child) return jiraName(value.child);
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
export function jiraIssueExtras(issue, { names = {}, importedKeys = new Set(), skipFields = [] } = {}) {
|
|
const fields = (issue && issue.fields) || {};
|
|
const at = `/issues/${issue && issue.key ? issue.key : '?'}`;
|
|
const tags = [];
|
|
const priority = fields.priority && jiraName(fields.priority);
|
|
if (priority) tags.push(`priority:${priority}`);
|
|
(Array.isArray(fields.components) ? fields.components : []).forEach(c => { const n = jiraName(c); if (n) tags.push(String(n)); });
|
|
(Array.isArray(fields.fixVersions) ? fields.fixVersions : []).forEach(v => { const n = jiraName(v); if (n) tags.push(`version:${n}`); });
|
|
|
|
const comments = ((fields.comment && Array.isArray(fields.comment.comments)) ? fields.comment.comments : [])
|
|
.filter(Boolean)
|
|
.map(c => ({
|
|
text: adfPlainText(c.body),
|
|
author: c.author && (c.author.accountId || c.author.name || c.author.emailAddress),
|
|
authorName: c.author && (c.author.displayName || c.author.name),
|
|
date: c.created,
|
|
}));
|
|
|
|
const unsupported = [];
|
|
const parentKey = fields.parent && fields.parent.key;
|
|
if (parentKey && !importedKeys.has(parentKey)) {
|
|
unsupported.push({ path: `${at}/parent`, reason: `parent ${parentKey} is not part of this import` });
|
|
}
|
|
const subtasks = (Array.isArray(fields.subtasks) ? fields.subtasks : [])
|
|
.filter(sub => sub && !(sub.key && importedKeys.has(sub.key)))
|
|
.map(sub => {
|
|
const f = sub.fields || {};
|
|
const title = [sub.key ? `[${sub.key}]` : null, f.summary].filter(Boolean).join(' ');
|
|
const category = f.status && f.status.statusCategory && f.status.statusCategory.key;
|
|
return { title, done: category === 'done' };
|
|
})
|
|
.filter(sub => sub.title);
|
|
|
|
const custom = {};
|
|
const skip = new Set(skipFields);
|
|
Object.keys(fields).filter(key => /^customfield_\d+$/.test(key) && !skip.has(key)).forEach(key => {
|
|
const value = jiraName(fields[key]);
|
|
if (value === undefined || value === '') return;
|
|
custom[typeof names[key] === 'string' && names[key] ? names[key] : key] = value;
|
|
});
|
|
|
|
// Each attachment's `content` is Jira's documented download address
|
|
// (/rest/api/3/attachment/content/<id>); JiraCreator downloads it with the
|
|
// sign-in the import page asks for (models/lib/importAttachmentAuth.js).
|
|
const attachments = [];
|
|
(Array.isArray(fields.attachment) ? fields.attachment : []).forEach((file, i) => {
|
|
if (file && typeof file.content === 'string' && /^https?:\/\//i.test(file.content)) {
|
|
attachments.push({ name: String(file.filename || 'attachment'), url: file.content });
|
|
} else {
|
|
unsupported.push({ path: `${at}/attachment/${i}`, reason: 'an attachment with no download address' });
|
|
}
|
|
});
|
|
return {
|
|
attachments,
|
|
tags,
|
|
comments,
|
|
parentKey: parentKey && importedKeys.has(parentKey) ? parentKey : undefined,
|
|
checklists: subtasks.length ? [{ title: 'Sub-tasks', items: subtasks }] : [],
|
|
custom_fields: custom,
|
|
unsupported,
|
|
};
|
|
}
|
|
|
|
// A search response is one page. Enhanced search (/search/jql) ends with
|
|
// isLast; the classic one reports startAt/total. Say when pages are missing.
|
|
export function jiraPageWarnings(data) {
|
|
if (!data || Array.isArray(data)) return [];
|
|
const issues = Array.isArray(data.issues) ? data.issues.length : 0;
|
|
if (data.isLast === false || (typeof data.nextPageToken === 'string' && data.nextPageToken && data.isLast !== true)) {
|
|
return [{ path: '/nextPageToken', reason: 'more result pages exist; only the pages in this file were imported' }];
|
|
}
|
|
const total = Number(data.total);
|
|
const startAt = Number(data.startAt) || 0;
|
|
if (Number.isFinite(total) && total > startAt + issues) {
|
|
return [{ path: '/total', reason: `${total - startAt - issues} issue(s) are on other result pages; only this file was imported` }];
|
|
}
|
|
return [];
|
|
}
|