Files
wekan/tests/workflowYaml.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

96 lines
3.5 KiB
JavaScript

'use strict';
// Guard: the GitHub Actions workflow reader of the GitHub organization import
// (models/lib/workflowYaml.js) reads real workflow files exactly as a full YAML
// library does, and refuses what it does not understand.
// Run: node tests/workflowYaml.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const { parseWorkflowYaml, WorkflowYamlError } = require('../models/lib/workflowYaml.js');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(`ok - ${name}`); }
const ROOT = path.join(__dirname, '..');
// js-yaml is a development dependency only (never in the bundle): the
// independent reference for the files below.
let jsYaml = null;
try { jsYaml = require('js-yaml'); } catch (e) { /* not installed */ }
function workflowFiles() {
const dirs = ['.github/workflows', '.tools/gitea/.github/workflows', '.tools/git-bug/.github/workflows',
'.tools/FerretDB/.github/workflows', '.tools/node-patches/.github/workflows'];
return dirs.flatMap((dir) => {
const abs = path.join(ROOT, dir);
return fs.existsSync(abs) ? fs.readdirSync(abs).filter(f => /\.ya?ml$/.test(f)).map(f => path.join(abs, f)) : [];
});
}
if (jsYaml) {
test('every workflow file here reads the same as js-yaml (anchors refused)', () => {
const files = workflowFiles();
assert.ok(files.length >= 10, `only ${files.length} workflow files found`);
let compared = 0;
for (const file of files) {
const text = fs.readFileSync(file, 'utf8');
// js-yaml's default schema reads on: as a key, the same as GitHub.
const expected = jsYaml.load(text);
if (/^\s*[^#\n]*[&*][A-Za-z_]/m.test(text) && /:\s*[&*][A-Za-z]/.test(text)) {
assert.throws(() => parseWorkflowYaml(text), WorkflowYamlError, file);
continue;
}
assert.deepStrictEqual(parseWorkflowYaml(text), expected, path.relative(ROOT, file));
compared += 1;
}
console.log(`# ${compared} of ${files.length} files compared`);
});
} else {
console.log('# skip - js-yaml (dev dependency) not installed');
}
test('the shapes workflows use', () => {
const doc = parseWorkflowYaml(`
name: Triage # a comment
"on":
issues:
types: [opened, labeled]
schedule:
- cron: '0 3 * * 1'
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v5
with:
repo-token: "\${{ secrets.GITHUB_TOKEN }}"
- name: Say hi
run: |
echo "hi # not a comment"
echo done
- run: >-
folded
line
`);
assert.deepStrictEqual(doc, {
name: 'Triage',
on: { issues: { types: ['opened', 'labeled'] }, schedule: [{ cron: '0 3 * * 1' }] },
jobs: { triage: { 'runs-on': 'ubuntu-latest', steps: [
{ uses: 'actions/labeler@v5', with: { 'repo-token': '${{ secrets.GITHUB_TOKEN }}' } },
{ name: 'Say hi', run: 'echo "hi # not a comment"\necho done\n' },
{ run: 'folded line' },
] } },
});
assert.deepStrictEqual(parseWorkflowYaml('on: [push, issues]'), { on: ['push', 'issues'] });
assert.deepStrictEqual(parseWorkflowYaml('a: {b: 1, c: [x, "y z"]}'), { a: { b: 1, c: ['x', 'y z'] } });
});
test('negative: anchors, aliases, tags, tabs, duplicates and bad indentation are refused', () => {
for (const bad of ['a: &x 1\nb: *x', 'a: !!str 1', 'a:\n\tb: 1', 'a: 1\na: 2', 'a: 1\n b: 2', '--- \na: 1\n---\nb: 2', 'a: "open']) {
assert.throws(() => parseWorkflowYaml(bad), WorkflowYamlError, JSON.stringify(bad));
}
});
console.log(`# ${passed} passed`);