Files
wekan/server/lib/listSyncSourceCoverage.js
T
Lauri Ojansivu 5e1ed32f58 Sync Jira original and remaining time estimates with conflict review
Map selected aggregate seconds to the board's imported numeric time fields in hours. Preserve zero, missing and explicit null; validate mapping identities and retain unrelated fields through conditional array writes.

Twenty-five focused Node suites and twenty-two Chromium scenarios pass with local MongoDB and Jira fixtures. Live Jira, FerretDB and other browsers remain unverified.

Thanks to xet7 !
2026-09-28 03:49:38 +03:00

112 lines
6.0 KiB
JavaScript

// Source-side coverage for the parsers in models/lib/externalParsers.js.
// Unknown objects are reported at their first unmapped path, without traversing
// or copying their values. Known containers (for example Jira time tracking)
// are inspected so unused siblings cannot hide beside one mapped field.
const WRITABLE_FIELDS = new Set(['title', 'description', 'spentTime', 'estimate', 'originalEstimate', 'remainingEstimate']);
const mapped = (target, converted = false) => ({ target, converted });
const unusedFallback = { reason: 'fallback' };
const present = value => value !== undefined && value !== null && value !== '' &&
(!Array.isArray(value) || value.length > 0);
function issueRules(type, issue, estimateMapping, timeMappings) {
if (type === 'jira') {
const fields = issue.fields || {};
const timeRules = {}, trackingRules = { timeSpentSeconds: mapped('spentTime', true) };
for (const [field, mapping] of Object.entries(timeMappings)) {
timeRules[mapping.flat] = Object.hasOwn(fields.timetracking || {}, mapping.nested) ? unusedFallback : mapped(field, true);
trackingRules[mapping.nested] = mapped(field, true);
}
return { key: mapped('externalId'), fields: {
...(estimateMapping ? { [estimateMapping.estimateFieldId]: mapped('estimate') } : {}),
summary: mapped('title'),
description: mapped('description', typeof fields.description === 'object'),
status: mapped('column_name'), labels: mapped('tags'), duedate: mapped('date_due'),
assignee: mapped('owner_username'), reporter: mapped('requested_by'),
timespent: fields.timetracking?.timeSpentSeconds == null ? mapped('spentTime', true) : unusedFallback,
...timeRules, timetracking: trackingRules,
} };
}
if (type === 'gitlab') {
return { iid: mapped('externalId'), id: issue.iid == null ? mapped('externalId') : unusedFallback,
title: mapped('title'), description: mapped('description'), state: mapped('column_name'),
due_date: mapped('date_due'), milestone: mapped('date_due'), assignee: mapped('owner_username'),
author: mapped('requested_by'), labels: mapped('tags') };
}
// GitHub, Gitea and Forgejo all use parseIssuesArray.
return { number: mapped('externalId'), id: issue.number == null ? mapped('externalId') : unusedFallback,
title: mapped('title'), body: mapped('description'),
description: issue.body ? unusedFallback : mapped('description'),
html_url: mapped('description', true), url: issue.html_url ? unusedFallback : mapped('description', true),
comments_data: mapped('description', true),
state: mapped('column_name'), state_reason: mapped('tags'), labels: mapped('tags'),
assignee: mapped('owner_username'), assignees: mapped('tags'),
user: mapped('requested_by'), author: mapped('requested_by'),
milestone: mapped('tags'), due_date: mapped('date_due') };
}
function describeSyncSourceCoverage(type, raw, fields, estimateMapping = null, timeMappings = {}) {
if (!['jira', 'github', 'gitlab', 'gitea', 'forgejo'].includes(type)) throw new Error('Unsupported Sync coverage source.');
const selected = new Set(fields === undefined ? ['title', 'description'] : fields);
const rows = new Map();
let occurrences = 0, omittedOccurrences = 0, excludedItems = 0, shortenedPaths = false;
const add = (path, reason, target) => {
occurrences++;
const key = `${reason}:${target || ''}:${path}`;
let row = rows.get(key);
if (!row) {
if (rows.size >= 100) { omittedOccurrences++; return; }
row = { path, reason, ...(target ? { target } : {}), count: 0 };
rows.set(key, row);
}
row.count++;
};
const childPath = (parent, key) => {
// Bound strings before escaping, too. An arbitrary source key must not
// expand the diagnostic payload without limit. JSON Pointer escaping
// distinguishes literal slashes/tildes from container boundaries.
if (key.length > 80) shortenedPaths = true;
return `${parent}/${key.slice(0, 80).replace(/~/g, '~0').replace(/\//g, '~1')}${key.length > 80 ? '…' : ''}`;
};
const inspect = (value, rules, parent) => {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
if (present(value)) add(parent, 'unmapped');
return;
}
for (const key of Object.keys(value)) {
const entry = value[key];
if (!present(entry)) continue;
const path = childPath(parent, key);
const rule = Object.hasOwn(rules, key) ? rules[key] : null;
if (!rule) { add(path, 'unmapped'); continue; }
if (rule.reason) { add(path, rule.reason); continue; }
if (!rule.target) { inspect(entry, rule, path); continue; }
if (rule.target === 'externalId') continue;
if (!WRITABLE_FIELDS.has(rule.target)) add(path, 'unmapped', rule.target);
else if (!selected.has(rule.target)) add(path, 'excluded', rule.target);
else if (rule.converted) add(path, 'converted', rule.target);
}
};
const issues = Array.isArray(raw) ? raw : raw.issues;
const issuePath = Array.isArray(raw) ? '/*' : '/issues/*';
for (const issue of issues) {
if (['github', 'gitea', 'forgejo'].includes(type) && issue.pull_request) {
excludedItems++;
add(issuePath, 'excluded-item');
continue;
}
inspect(issue, issueRules(type, issue, estimateMapping, timeMappings), issuePath);
}
if (!Array.isArray(raw)) {
// Pagination counters/tokens are transport state, not issue data. Other
// envelope extensions remain visible in the report.
const envelope = { issues: mapped('externalId'), startAt: mapped('externalId'),
maxResults: mapped('externalId'), total: mapped('externalId'), isLast: mapped('externalId'),
nextPageToken: mapped('externalId'), board: { name: mapped('board.name') } };
inspect(raw, envelope, '');
}
return { rows: [...rows.values()].sort((a, b) => a.path.localeCompare(b.path) || a.reason.localeCompare(b.reason)),
occurrences, omittedOccurrences, excludedItems, sourceItems: issues.length,
truncated: omittedOccurrences > 0 || shortenedPaths };
}
module.exports = { describeSyncSourceCoverage };