Files
wekan/tests/githubOrgImport.test.cjs
T
Lauri Ojansivu 935e68dfdd GitHub organization import: repositories, issues, releases, Actions, wiki.
Import -> GitHub -> Import a whole GitHub organization makes every
repository of an organization or user its own board, opening in Board View
/ Version Control. Issues go through the ordinary importBoard, as the person
who started the job, with their comments, labels and files. Releases are
cards in a Releases list, their files attached only when chosen. Each
.github/workflows file becomes Rules. The wiki is read from its git
repository with isomorphic-git (1.42.4, pinned), every request through the
SSRF guard and only to github.com: a page is a card in the Wiki list, its
[[links]] point at and are attached to the linked pages' cards, and each
commit that changed it is a dated edit in the card's history, so Restore
brings back an older version. The job shows progress, can be cancelled,
pauses on a restart and stops at a rate limit; the token is kept in memory
only.

Version Control gains Releases and Wiki tabs: the wiki as GitHub shows it,
with the page list, _Sidebar and _Footer, links that stay in the tab, and
each page's history with Restore.

GitHub Actions workflows map to Rules: issue events to triggers, label,
close, reopen, comment, assign and create steps to actions, actions/stale to
an aging rule. Code - builds, tests, scripts - is never run: such a workflow
is a disabled rule with its YAML as the description. WeKan's Rules gain what
they lacked for it: a "commented on" trigger, an "add comment" action written
as the person whose change ran the rule and only where they may comment,
cron schedules in UTC, and a rule description shown on the rule. A disabled
rule no longer fires on a schedule or from its button, which it did.

The new strings are translated in every locale.

Thanks to xet7 !
2026-10-14 12:43:36 +03:00

93 lines
5.0 KiB
JavaScript

'use strict';
// Guard: the GitHub organization import - every repository as its own board
// in Board View / Version Control (server/githubOrgImport.js,
// models/lib/githubOrgImport.js).
// Run: node tests/githubOrgImport.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const o = require('../models/lib/githubOrgImport.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');
test('the owner from a name or a GitHub address', () => {
assert.strictEqual(o.ownerFrom('https://github.com/wekan'), 'wekan');
assert.strictEqual(o.ownerFrom('github.com/wekan/'), 'wekan');
assert.strictEqual(o.ownerFrom(' wekan '), 'wekan');
// negative: not a GitHub login, another site, a path
for (const bad of ['', '-wekan', 'we kan', 'https://evil.example/wekan', 'github.com/wekan/wekan', 'x'.repeat(40), '../etc']) {
assert.strictEqual(o.ownerFrom(bad), null, bad);
}
});
test('options: issues always, known parts only, repository names checked', () => {
assert.deepStrictEqual(o.cleanOrgImport({ owner: 'wekan', parts: ['wiki', 'nope'] }).parts, ['issues', 'wiki']);
assert.deepStrictEqual(o.cleanOrgImport({ owner: 'wekan' }).parts, o.DEFAULT_PARTS);
assert.ok(!o.DEFAULT_PARTS.includes('releaseFiles'), 'release files are large: chosen, not default');
assert.deepStrictEqual(o.cleanOrgImport({ owner: 'wekan', repos: ['wekan', '../x', 'a b'] }).repos, ['wekan']);
assert.throws(() => o.cleanOrgImport({ owner: '' }), /owner-invalid/);
assert.throws(() => o.cleanOrgImport({ owner: 'wekan', repos: ['../x'] }), /no-repos/);
});
test('repositories: forks left out unless chosen, other owners never', () => {
const list = [
{ name: 'wekan', owner: { login: 'wekan' }, has_wiki: true },
{ name: 'fork', owner: { login: 'wekan' }, fork: true },
{ name: 'old', owner: { login: 'wekan' }, archived: true },
{ name: 'theirs', owner: { login: 'someone-else' } },
{ name: '../bad', owner: { login: 'wekan' } },
];
assert.deepStrictEqual(o.selectRepos(list, o.cleanOrgImport({ owner: 'wekan' })).map(r => r.name), ['old', 'wekan']);
assert.deepStrictEqual(o.selectRepos(list, o.cleanOrgImport({ owner: 'WeKan', includeForks: true })).map(r => r.name), ['fork', 'old', 'wekan']);
assert.deepStrictEqual(o.selectRepos(list, o.cleanOrgImport({ owner: 'wekan', repos: ['FORK'] })).map(r => r.name), ['fork']);
});
test('releases as cards', () => {
const [r] = o.releaseCards([{ tag_name: 'v1.0', name: 'First', body: 'Notes', published_at: '2026-01-01T00:00:00Z', prerelease: true,
html_url: 'https://github.com/o/r/releases/tag/v1.0', assets: [{ name: 'a.zip', browser_download_url: 'https://github.com/o/r/releases/download/v1.0/a.zip', size: 5 }, { name: 'x', browser_download_url: 'ftp://x' }] }]);
assert.strictEqual(r.title, 'v1.0 First (pre-release)');
assert.match(r.description, /^Notes\n\nSource: https:\/\/github\.com\/o\/r\/releases\/tag\/v1\.0$/);
assert.deepStrictEqual(r.assets.map(a => a.name), ['a.zip']);
});
const server = read('server/githubOrgImport.js');
const wiki = read('server/lib/githubWiki.js');
test('it imports through importBoard, as the person who started it', () => {
assert.match(server, /Meteor\.callAsync\('importBoard', raw, \{\}, 'github', null\)/);
assert.match(server, /DDP\._CurrentMethodInvocation\.withValue\(\{ userId, isSimulation: false/);
assert.match(server, /if \(!board \|\| !board\.hasAdmin\(userId\)\) throw/);
assert.match(server, /await assertImportEnabled\(\);/);
});
test('negative: the token is never stored', () => {
assert.doesNotMatch(server, /insertAsync\(\{[^}]*token/);
assert.match(server, /const tokens = new Map\(\); \/\/ jobId -> token, never persisted/);
assert.match(read('models/githubImportJobs.js'), /deny\(\{ insert: \(\) => true, update: \(\) => true, remove: \(\) => true \}\)/);
});
test('negative: every outbound request goes through the SSRF guard', () => {
assert.doesNotMatch(server + wiki, /\bfetch\(|require\('isomorphic-git\/http/);
assert.match(wiki, /fetchSafe\(url, \{/);
assert.match(wiki, /if \(new URL\(url\)\.origin !== origin\) throw/);
assert.match(server, /fetchImportedAttachment\(asset\.url\)/);
});
test('the boards open in Version Control; wiki history is dated history', () => {
assert.match(server, /defaultBoardViewModifier\(board, VCS_VIEW, 'private'\)/);
assert.match(wiki, /prepareCardFieldHistory\(\{[\s\S]*?createdAt: new Date\(edit\.at\), fields: SYNC_FIELDS/);
assert.match(wiki, /persistSyncFieldHistory\(\{ history: ChangeHistory, plan/);
assert.match(wiki, /fs\.promises\.rm\(dir, \{ recursive: true, force: true \}\)/);
});
test('UI: the GitHub import page has the organization block', () => {
assert.match(read('client/components/import/import.jade'), /if isGithubImport\n\s+hr\n\s+\+importGithubOrg/);
assert.match(read('client/components/import/import.js'), /Meteor\.call\('githubOrgStartImport', input/);
});
console.log(`# ${passed} passed`);