mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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 !
234 lines
8.9 KiB
JavaScript
234 lines
8.9 KiB
JavaScript
'use strict';
|
||
|
||
// A YAML reader for GitHub Actions workflow files (.github/workflows/*.yml),
|
||
// for the GitHub organization import's Actions -> WeKan Rules. Workflow files
|
||
// use a small, regular part of YAML, and WeKan has no YAML library in its
|
||
// production bundle, so this reads that part and refuses the rest with an
|
||
// error, instead of adding a dependency:
|
||
//
|
||
// key: value block mappings, keys plain or quoted
|
||
// - item block sequences, also of mappings ("- uses: x")
|
||
// [a, "b"] {a: b} flow sequences and mappings, one line
|
||
// 'x' "x\n" plain scalars; true/false/null/numbers typed
|
||
// | |- > >- block scalars (run: scripts, bodies)
|
||
// # comment, --- comments and one document start
|
||
//
|
||
// Anchors, aliases, tags and multi-document files are refused: a workflow
|
||
// does not need them, and a reader that half-understood them would map a
|
||
// different workflow than the one written. Pure: tests/workflowYaml.test.cjs.
|
||
|
||
class WorkflowYamlError extends Error {}
|
||
|
||
function fail(message, lineNo) {
|
||
throw new WorkflowYamlError(lineNo ? `line ${lineNo}: ${message}` : message);
|
||
}
|
||
|
||
// The line without its comment: a # starts one at the line start or after a
|
||
// space, and not inside quotes.
|
||
function stripComment(text) {
|
||
let quote = null;
|
||
for (let i = 0; i < text.length; i += 1) {
|
||
const ch = text[i];
|
||
if (quote) {
|
||
if (ch === '\\' && quote === '"') { i += 1; continue; }
|
||
if (ch === quote) quote = null;
|
||
} else if (ch === '"' || ch === "'") {
|
||
quote = ch;
|
||
} else if (ch === '#' && (i === 0 || /\s/.test(text[i - 1]))) {
|
||
return text.slice(0, i).replace(/\s+$/, '');
|
||
}
|
||
}
|
||
return text.replace(/\s+$/, '');
|
||
}
|
||
|
||
function unquoteDouble(body, lineNo) {
|
||
return body.replace(/\\(u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2}|.)/g, (m, e) => {
|
||
if (e[0] === 'u' || e[0] === 'x') return String.fromCharCode(parseInt(e.slice(1), 16));
|
||
const map = { n: '\n', t: '\t', r: '\r', '"': '"', '\\': '\\', '/': '/', '0': '\0', ' ': ' ' };
|
||
if (!(e in map)) fail(`unknown escape \\${e}`, lineNo);
|
||
return map[e];
|
||
});
|
||
}
|
||
|
||
function scalar(raw, lineNo) {
|
||
const text = raw.trim();
|
||
if (text === '') return null;
|
||
if (/^[&*!]/.test(text)) fail('anchors, aliases and tags are not supported', lineNo);
|
||
if (text[0] === '"') {
|
||
if (!/^"(?:[^"\\]|\\.)*"$/.test(text)) fail('unterminated "string"', lineNo);
|
||
return unquoteDouble(text.slice(1, -1), lineNo);
|
||
}
|
||
if (text[0] === "'") {
|
||
if (!/^'(?:[^']|'')*'$/.test(text)) fail("unterminated 'string'", lineNo);
|
||
return text.slice(1, -1).replace(/''/g, "'");
|
||
}
|
||
if (text[0] === '[' || text[0] === '{') return flow(text, lineNo);
|
||
if (/^(null|~|Null|NULL)$/.test(text)) return null;
|
||
if (/^(true|True|TRUE)$/.test(text)) return true;
|
||
if (/^(false|False|FALSE)$/.test(text)) return false;
|
||
if (/^[-+]?\d+$/.test(text)) return Number(text);
|
||
if (/^[-+]?(\d+\.\d*|\.\d+)([eE][-+]?\d+)?$/.test(text)) return Number(text);
|
||
return text;
|
||
}
|
||
|
||
// One-line flow collections: [a, b], {a: b, c: [d]}.
|
||
function flow(text, lineNo) {
|
||
let i = 0;
|
||
const ws = () => { while (i < text.length && /\s/.test(text[i])) i += 1; };
|
||
const item = (stops) => {
|
||
ws();
|
||
if (text[i] === '[' || text[i] === '{') return collection();
|
||
let start = i;
|
||
if (text[i] === '"' || text[i] === "'") {
|
||
const q = text[i];
|
||
i += 1;
|
||
while (i < text.length && text[i] !== q) i += (text[i] === '\\' && q === '"') ? 2 : 1;
|
||
i += 1;
|
||
return scalar(text.slice(start, i), lineNo);
|
||
}
|
||
while (i < text.length && !stops.includes(text[i])) i += 1;
|
||
return scalar(text.slice(start, i), lineNo);
|
||
};
|
||
const collection = () => {
|
||
const open = text[i];
|
||
const close = open === '[' ? ']' : '}';
|
||
i += 1;
|
||
const out = open === '[' ? [] : {};
|
||
ws();
|
||
if (text[i] === close) { i += 1; return out; }
|
||
for (;;) {
|
||
if (open === '[') out.push(item([',', ']']));
|
||
else {
|
||
const key = item([':', ',', '}']);
|
||
ws();
|
||
if (text[i] !== ':') fail('expected ":" in a {mapping}', lineNo);
|
||
i += 1;
|
||
out[String(key)] = item([',', '}']);
|
||
}
|
||
ws();
|
||
if (text[i] === ',') { i += 1; continue; }
|
||
if (text[i] === close) { i += 1; return out; }
|
||
fail(`unterminated ${open}${close}`, lineNo);
|
||
}
|
||
};
|
||
const value = collection();
|
||
ws();
|
||
if (i < text.length) fail('text after a flow collection', lineNo);
|
||
return value;
|
||
}
|
||
|
||
// `key: rest` -> [key, rest], or null when the line is not a mapping entry.
|
||
function splitKey(text) {
|
||
const quoted = /^("(?:[^"\\]|\\.)*"|'(?:[^']|'')*')\s*:(?:\s+|$)(.*)$/.exec(text);
|
||
if (quoted) return [scalar(quoted[1]), quoted[2]];
|
||
const plain = /^([^\s"'[\]{}#,][^:#]*?)\s*:(?:\s+|$)(.*)$/.exec(text);
|
||
return plain ? [plain[1], plain[2]] : null;
|
||
}
|
||
|
||
function parseWorkflowYaml(source) {
|
||
const raw = String(source || '').replace(/^/, '').split(/\r?\n/);
|
||
const lines = [];
|
||
let docStarts = 0;
|
||
for (let n = 0; n < raw.length; n += 1) {
|
||
if (/\t/.test(raw[n].match(/^\s*/)[0])) fail('tabs are not allowed for indentation', n + 1);
|
||
const text = stripComment(raw[n]);
|
||
if (/^---\s*$/.test(text)) { docStarts += 1; if (docStarts > 1 || lines.some(l => !l.blank)) fail('one document only', n + 1); continue; }
|
||
if (/^\.\.\.\s*$/.test(text)) break;
|
||
lines.push({ n: n + 1, raw: raw[n], text, indent: text.match(/^ */)[0].length, blank: text.trim() === '',
|
||
rawBlank: raw[n].trim() === '' });
|
||
}
|
||
let at = 0;
|
||
const next = () => { while (at < lines.length && lines[at].blank) at += 1; return lines[at]; };
|
||
|
||
// A block scalar's lines: everything indented deeper than its parent. Read
|
||
// raw - a "# ..." line in a run: script is part of the script.
|
||
function blockScalar(style, parentIndent) {
|
||
const keep = style.includes('+');
|
||
const strip = style.includes('-');
|
||
const folded = style[0] === '>';
|
||
const body = [];
|
||
let indent = null;
|
||
while (at < lines.length) {
|
||
const line = lines[at];
|
||
if (!line.rawBlank) {
|
||
const ind = line.raw.match(/^ */)[0].length;
|
||
if (ind <= parentIndent) break;
|
||
if (indent === null) indent = ind;
|
||
if (ind < indent) break;
|
||
}
|
||
body.push(line.rawBlank ? '' : line.raw.slice(indent).replace(/\s+$/, (m) => (/\S/.test(line.raw) ? m : '')));
|
||
at += 1;
|
||
}
|
||
while (!keep && body.length && body[body.length - 1] === '') body.pop();
|
||
let text = folded ? body.join('\n').replace(/([^\n])\n(?=[^\n ])/g, '$1 ') : body.join('\n');
|
||
if (!strip) text += '\n';
|
||
return text;
|
||
}
|
||
|
||
// The value after "key:" or "- ": inline, a block scalar, or a nested block.
|
||
function valueAfter(rest, parentIndent, lineNo) {
|
||
const r = rest.trim();
|
||
if (/^[|>][-+]?$/.test(r)) return blockScalar(r, parentIndent);
|
||
if (r !== '') return scalar(r, lineNo);
|
||
const line = next();
|
||
if (!line || line.indent <= parentIndent) {
|
||
// A sequence may sit at its key's own indent ("steps:\n- uses: x").
|
||
if (line && line.indent === parentIndent && /^- |^-$/.test(line.text.trim())) return block(line.indent);
|
||
return null;
|
||
}
|
||
return block(line.indent);
|
||
}
|
||
|
||
function block(indent) {
|
||
const first = next();
|
||
if (!first) return null;
|
||
if (/^-(\s|$)/.test(first.text.trim())) {
|
||
const out = [];
|
||
for (let line = next(); line && line.indent === indent && /^-(\s|$)/.test(line.text.trim()); line = next()) {
|
||
at += 1;
|
||
const rest = line.text.trim().slice(1);
|
||
const inner = rest.replace(/^\s+/, '');
|
||
const kv = inner ? splitKey(inner) : null;
|
||
if (kv) {
|
||
// "- key: value" starts a mapping whose keys line up after "- ".
|
||
const keyIndent = indent + 1 + (rest.length - inner.length);
|
||
const obj = {};
|
||
obj[kv[0]] = valueAfter(kv[1], keyIndent, line.n);
|
||
const more = next();
|
||
if (more && more.indent === keyIndent && !/^-(\s|$)/.test(more.text.trim())) Object.assign(obj, mapping(keyIndent));
|
||
out.push(obj);
|
||
} else {
|
||
out.push(valueAfter(inner, indent, line.n));
|
||
}
|
||
}
|
||
const after = next();
|
||
if (after && after.indent > indent) fail('unexpected indentation', after.n);
|
||
return out;
|
||
}
|
||
return mapping(indent);
|
||
}
|
||
|
||
function mapping(indent) {
|
||
const out = {};
|
||
for (let line = next(); line && line.indent === indent; line = next()) {
|
||
const kv = splitKey(line.text.trim());
|
||
if (!kv) fail(`expected "key: value", found ${JSON.stringify(line.text.trim().slice(0, 40))}`, line.n);
|
||
at += 1;
|
||
if (Object.prototype.hasOwnProperty.call(out, kv[0])) fail(`duplicate key ${kv[0]}`, line.n);
|
||
out[kv[0]] = valueAfter(kv[1], indent, line.n);
|
||
}
|
||
const after = next();
|
||
if (after && after.indent > indent) fail('unexpected indentation', after.n);
|
||
return out;
|
||
}
|
||
|
||
const first = next();
|
||
if (!first) return null;
|
||
const doc = block(first.indent);
|
||
const rest = next();
|
||
if (rest) fail('unexpected indentation', rest.n);
|
||
return doc;
|
||
}
|
||
|
||
module.exports = { parseWorkflowYaml, WorkflowYamlError };
|