Files
wekan/tests/listSyncIssueDetails.test.cjs
T
Lauri Ojansivu 326d9bbf61 Board View / Version Control, issue files and synced issue details.
Version Control shows the board as a GitHub or Gitea issue tracker in the
board's own theme: cards are issues (open, or closed when archived, in a
Closed or Done list, or closed upstream), the board's labels are labels,
labels with a due date and the importers' milestone: labels are
milestones, comments are the conversation and attachments are the files.
The issue list takes GitHub's search words (is:open, label:, milestone:,
author:, assignee:@me, no:label, sort:). New issue, Close and Reopen write
through the ordinary collections, so the History tab - the board's
history table - shows them and Restore undoes them. Import links open the
GitHub, GitLab, Gitea and Forgejo importers, and each synced list has a
Sync now button.

The GitHub, GitLab, Gitea and Forgejo importers bring each issue's files:
user-attachments and user-images links, GitLab uploads, and Gitea and
Forgejo assets, downloaded through the import's SSRF guard and stored as
card attachments in Admin Panel / Attachments / Default Storage.

List sync has a new option, Comments, labels, state and files, off by
default: comments come from one paged repository-wide list (GitHub,
Gitea) or each issue's notes (GitLab), keyed by the tracker's ids, so a
second run writes only what changed, and nothing written in WeKan is
changed. Gitea issues are fetched without pull requests, and a rate
limited answer is waited out once or ends the run with the time to retry.

A public board's Show on / Default on Public Board settings are for the
public: visitors and non-members get them, read-only, and the board's own
members get the private side, so any view - Version Control, Email, Chat
and the rest - can be the read-only public view.

The card menu has a QR code of the card's link, drawn in the browser by
WeKan's own encoder, which the tests check module for module against
qrcode-svg.

The new strings are translated in every locale.

Thanks to xet7 !

Fixes #2015.
2026-10-14 12:09:34 +03:00

117 lines
6.3 KiB
JavaScript

'use strict';
// Guard: list sync's "Comments, labels, state and files" - a list synced from
// GitHub, GitLab, Gitea or Forgejo brings each issue's conversation, labels,
// open/closed state and files, so the board is that repository's issue
// tracker in Board View / Version Control.
// Run: node tests/listSyncIssueDetails.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const d = require('../models/lib/listSyncIssueDetails.js');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(`ok - ${name}`); }
const read = f => fs.readFileSync(path.join(__dirname, '..', f), 'utf8');
const labelIdByName = new Map([['bug', 'L-bug'], ['ui', 'L-ui'], ['milestone:v1', 'L-m1']]);
test('labels: the issue\'s own, milestones included, parser-only tags left out', () => {
assert.deepStrictEqual(d.issueLabelNames({ tags: ['bug', 'assignee:bob', 'state_reason:not_planned', 'type:incident', 'milestone:v1', 'bug', { name: 'ui' }] }),
['bug', 'milestone:v1', 'ui']);
assert.strictEqual(d.labelColor('bug'), d.labelColor('bug'));
});
test('labels: sync replaces only what it added; labels added in WeKan stay', () => {
const card = { labelIds: ['L-local', 'L-ui'], syncLabelIds: ['L-ui'], syncIssueState: 'open' };
const plan = d.planIssueDetails({ card, task: { tags: ['bug'], column_name: 'Open' }, labelIdByName });
assert.deepStrictEqual(plan.set, { labelIds: ['L-local', 'L-bug'], syncLabelIds: ['L-bug'] });
// negative: nothing changed upstream - nothing is written
const same = d.planIssueDetails({ card: { labelIds: ['L-local', 'L-bug'], syncLabelIds: ['L-bug'], syncIssueState: 'open' },
task: { tags: ['bug'], column_name: 'Open' }, labelIdByName });
assert.deepStrictEqual(same.set, {});
});
test('state follows the tracker', () => {
const plan = d.planIssueDetails({ card: { syncIssueState: 'open' }, task: { column_name: 'Closed' }, labelIdByName });
assert.strictEqual(plan.set.syncIssueState, 'closed');
});
test('comments: new ones added, edited ones updated, by the tracker\'s comment id', () => {
const plan = d.planIssueDetails({
card: { syncIssueState: 'open' },
task: { column_name: 'Open', comments: [
{ externalId: '1', author: 'alice', text: 'first' },
{ externalId: '2', author: 'bob', text: 'edited upstream' },
{ externalId: '3', author: 'carol', text: 'same' },
{ author: 'nobody', text: 'no id: an import-only comment, never synced' },
] },
labelIdByName,
comments: [{ _id: 'c2', syncCommentId: '2', text: 'bob: before' }, { _id: 'c3', syncCommentId: '3', text: 'carol: same' }],
});
assert.deepStrictEqual(plan.insertComments.map(c => [c.syncCommentId, c.text]), [['1', 'alice: first']]);
assert.deepStrictEqual(plan.updateComments, [{ _id: 'c2', text: 'bob: edited upstream' }]);
});
test('files: each url once', () => {
const plan = d.planIssueDetails({ card: {}, task: { attachments: [{ name: 'a', url: 'https://x/a' }, { name: 'b', url: 'https://x/b' }] },
labelIdByName, fileUrls: new Set(['https://x/a']) });
assert.deepStrictEqual(plan.files.map(f => f.name), ['b']);
});
test('repository-wide comments go to their issue by issue_url', () => {
const issues = d.attachRepositoryComments([{ number: 1 }, { number: 2 }, { id: 'x' }], [
{ id: 10, issue_url: 'https://api.github.com/repos/o/r/issues/2', body: 'hi' },
{ id: 11, issue_url: 'https://gitea.example/api/v1/repos/o/r/issues/1', body: 'yo' },
{ id: 12, issue_url: 'https://api.github.com/repos/o/r/pulls/3', body: 'not an issue' },
]);
assert.deepStrictEqual(issues.map(i => (i.comments_data || []).map(c => c.id)), [[11], [10], []]);
});
test('rate limits: GitHub reset, Retry-After, 429; negative: a plain 403 is not one', () => {
const now = 1_700_000_000_000;
const h = o => new Headers(o);
assert.strictEqual(d.rateLimitWait(403, h({ 'x-ratelimit-remaining': '0', 'x-ratelimit-reset': String(now / 1000 + 30) }), now), 30000);
assert.strictEqual(d.rateLimitWait(403, h({ 'retry-after': '60' }), now), 60000);
assert.strictEqual(d.rateLimitWait(429, h({ 'ratelimit-reset': '5' }), now), 5000);
assert.strictEqual(d.rateLimitWait(429, h({}), now), 60000);
assert.strictEqual(d.rateLimitWait(403, h({ 'x-ratelimit-remaining': '12' }), now), null);
assert.strictEqual(d.rateLimitWait(500, h({ 'retry-after': '1' }), now), null);
});
const fetch = read('server/lib/listSyncFetch.js');
const sync = read('server/listSync.js');
const apply = read('server/lib/listSyncIssueDetails.js');
const method = read('server/methods/listSync.js');
test('fetch: comments only when asked, Gitea without pull requests, rate limit waited once', () => {
assert.match(fetch, /if \(!syncSource\.issueDetails\) return issues;[\s\S]*?\/issues\/comments\?per_page=100/);
assert.match(fetch, /issues\?state=all&type=issues&limit=100/);
assert.match(fetch, /\/issues\/\$\{Number\(issue\.iid\)\}\/notes\?per_page=100&sort=asc/);
assert.match(fetch, /if \(!retried && wait <= RATE_LIMIT_WAIT_MS\)/);
});
test('apply: after a successful run only, never fatal, through hooked collections', () => {
assert.strictEqual((sync.match(/return withIssueDetails\(\{/g) || []).length, 2, 'both success paths');
assert.match(sync, /if \(!source\.issueDetails\) return result;/);
assert.doesNotMatch(apply, /\.direct\./, 'history and undo see every write');
assert.match(apply, /fetchImportedAttachment\(file\.url\)/);
assert.match(apply, /Attachments\.writeAsync\(downloaded\.buffer, \{[\s\S]*?syncUrl: file\.url[\s\S]*?\}, true\)/);
});
test('opt-in: off unless chosen, and only for issue trackers', () => {
assert.match(method, /issueDetails: Match\.Optional\(Boolean\)/);
assert.match(method, /config\.issueDetails === true && ISSUE_DETAIL_SOURCES\.includes\(source\.type\)/);
assert.deepStrictEqual(d.ISSUE_DETAIL_SOURCES, ['github', 'gitlab', 'gitea', 'forgejo']);
});
test('a synced issue is not closed from WeKan (the sync would see it gone)', () => {
assert.match(read('server/versionControlView.js'), /if \(isSyncedIssue\(card\)\) throw new Meteor\.Error\('vcs-synced-issue'\)/);
const vcs = require('../models/lib/versionControlView.js');
assert.strictEqual(vcs.issueState({ syncIssueState: 'closed' }, 'Open'), 'closed');
assert.strictEqual(vcs.issueState({ syncIssueState: 'open' }, 'Closed'), 'open');
});
console.log(`# ${passed} passed`);