yadflow 4.9.0 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +184 -0
- package/README.md +15 -5
- package/bin/commands.mjs +11 -1
- package/bin/yad.mjs +203 -14
- package/cli/agent-guard-worker.mjs +13 -0
- package/cli/agent-guard.mjs +1797 -73
- package/cli/agent-rules.mjs +64 -5
- package/cli/argv-flags.mjs +2 -2
- package/cli/brain/vendor/LICENSE-graphology-library.txt +21 -0
- package/cli/brain/vendor/LICENSE-graphology.txt +21 -0
- package/cli/brain/vendor/LICENSE-sigma.txt +12 -0
- package/cli/brain/vendor/graphology-library.min.js +2 -0
- package/cli/brain/vendor/graphology.umd.min.js +2 -0
- package/cli/brain/vendor/sigma.min.js +1369 -0
- package/cli/brain/view/app.js +382 -0
- package/cli/brain/view/index.html +63 -0
- package/cli/brain/view/live.css +228 -0
- package/cli/brain/view/live.html +110 -0
- package/cli/brain/view/live.js +884 -0
- package/cli/brain/view/style.css +141 -0
- package/cli/brain-about.mjs +206 -0
- package/cli/brain-activity.mjs +285 -0
- package/cli/brain-skills.mjs +118 -0
- package/cli/brain-vendor.mjs +71 -0
- package/cli/brain-view.mjs +344 -0
- package/cli/brain.mjs +810 -0
- package/cli/ci-check.mjs +48 -4
- package/cli/codeowners-command.mjs +72 -6
- package/cli/commit.mjs +17 -3
- package/cli/docs.mjs +2 -0
- package/cli/doctor.mjs +151 -25
- package/cli/epic-state.mjs +186 -4
- package/cli/epic.mjs +26 -10
- package/cli/errors.mjs +2 -1
- package/cli/find-product.mjs +69 -15
- package/cli/fold.mjs +31 -6
- package/cli/foundation-publish.mjs +256 -0
- package/cli/gate.mjs +29 -3
- package/cli/git-read.mjs +38 -0
- package/cli/guard-stream.mjs +409 -0
- package/cli/guide.mjs +3 -1
- package/cli/hook.mjs +102 -9
- package/cli/issue-links.mjs +61 -0
- package/cli/issues-act.mjs +371 -0
- package/cli/issues-write.mjs +441 -0
- package/cli/issues.mjs +513 -0
- package/cli/lib.mjs +37 -2
- package/cli/lifecycle.mjs +7 -0
- package/cli/local-page.mjs +138 -0
- package/cli/machine.mjs +1 -1
- package/cli/manifest.mjs +59 -3
- package/cli/map/view/app.js +712 -0
- package/cli/map/view/index.html +50 -0
- package/cli/map/view/style.css +142 -0
- package/cli/map-view.mjs +186 -0
- package/cli/map.mjs +637 -0
- package/cli/members.mjs +9 -15
- package/cli/next.mjs +1 -1
- package/cli/openpr.mjs +26 -6
- package/cli/plan.mjs +127 -0
- package/cli/platform.mjs +159 -4
- package/cli/product-index.mjs +1 -1
- package/cli/protection.mjs +53 -3
- package/cli/reconcile.mjs +9 -3
- package/cli/report.mjs +9 -3
- package/cli/reports-dir.mjs +391 -0
- package/cli/reports.mjs +65 -0
- package/cli/run-checks.mjs +15 -4
- package/cli/scan.mjs +314 -0
- package/cli/setup.mjs +42 -8
- package/cli/standup.mjs +120 -78
- package/cli/status.mjs +6 -5
- package/cli/story-frames.mjs +17 -6
- package/cli/test-agents-file.mjs +985 -12
- package/cli/test-brain-activity.mjs +753 -0
- package/cli/test-brain-view.mjs +381 -0
- package/cli/test-brain.mjs +795 -0
- package/cli/test-build.mjs +25 -0
- package/cli/test-ci-check.mjs +469 -10
- package/cli/test-guide.mjs +218 -0
- package/cli/test-machine.mjs +59 -0
- package/cli/test-map.mjs +615 -0
- package/cli/test-node.mjs +15 -0
- package/cli/test-reports.mjs +382 -0
- package/cli/test-run-checks.mjs +21 -0
- package/cli/test-setup-agent.mjs +14 -0
- package/cli/test-story-frames.mjs +74 -4
- package/cli/test-worktree.mjs +24 -3
- package/cli/toolbox-connect.mjs +259 -0
- package/cli/toolbox.mjs +187 -12
- package/cli/update-commit.mjs +101 -9
- package/cli/usage.mjs +8 -4
- package/package.json +12 -4
- package/skills/sdlc/config.yaml +1 -1
- package/skills/sdlc/module-help.csv +3 -1
- package/skills/yad-analysis/SKILL.md +4 -0
- package/skills/yad-architecture/SKILL.md +22 -4
- package/skills/yad-backfill/SKILL.md +4 -0
- package/skills/yad-change/SKILL.md +7 -1
- package/skills/yad-change/references/triage.md +2 -0
- package/skills/yad-checks/SKILL.md +15 -7
- package/skills/yad-checks/references/check-gates.md +70 -35
- package/skills/yad-checks/templates/checks/build-test-lint.sh +7 -0
- package/skills/yad-checks/templates/checks/install-deps.sh +6 -0
- package/skills/yad-checks/templates/checks/member-check.mjs +3 -2
- package/skills/yad-checks/templates/checks/package-manager.sh +42 -0
- package/skills/yad-checks/templates/checks/risk-map-check.sh +2 -2
- package/skills/yad-checks/templates/checks/verified-commits.sh +3 -3
- package/skills/yad-checks/templates/github/yad-checks.yml +358 -69
- package/skills/yad-checks/templates/github/yad-pr-text.yml +118 -0
- package/skills/yad-checks/templates/github/yad-product-checks.yml +130 -54
- package/skills/yad-checks/templates/github/yad-product-pr-text.yml +131 -0
- package/skills/yad-checks/templates/github/yad-update-guard.yml +2 -1
- package/skills/yad-checks/templates/github/yad-verified-commits.yml +49 -3
- package/skills/yad-checks/templates/gitlab/yad-checks.gitlab-ci.yml +80 -20
- package/skills/yad-checks/templates/gitlab/yad-product-checks.gitlab-ci.yml +59 -9
- package/skills/yad-checks/templates/gitlab/yad-update-guard.gitlab-ci.yml +1 -0
- package/skills/yad-checks/templates/gitlab/yad-verified-commits.gitlab-ci.yml +49 -1
- package/skills/yad-checks/templates/hooks/agent-guard.mjs +44 -10
- package/skills/yad-checks/templates/hooks/ledger-guard-cursor.mjs +12 -6
- package/skills/yad-checks/templates/hooks/yad-activity.mjs +238 -0
- package/skills/yad-commit/SKILL.md +3 -1
- package/skills/yad-connect-design/SKILL.md +42 -10
- package/skills/yad-connect-design/references/design-context.md +35 -5
- package/skills/yad-connect-design/references/design-registry.md +3 -3
- package/skills/yad-connect-learning/SKILL.md +12 -2
- package/skills/yad-connect-repos/SKILL.md +18 -0
- package/skills/yad-connect-testing/SKILL.md +22 -5
- package/skills/yad-connect-testing/references/testing-context.md +1 -1
- package/skills/yad-discovery/SKILL.md +11 -0
- package/skills/yad-discovery/references/foundation-schema.md +11 -0
- package/skills/yad-docs/templates/app/package-lock.json +18 -10
- package/skills/yad-docs/templates/app/package.json +6 -1
- package/skills/yad-epic/SKILL.md +7 -0
- package/skills/yad-epic/references/state-schema.md +50 -1
- package/skills/yad-guide/SKILL.md +8 -4
- package/skills/yad-implement/SKILL.md +117 -4
- package/skills/yad-implement/references/implement-conventions.md +2 -1
- package/skills/yad-issues/SKILL.md +54 -0
- package/skills/yad-learn/SKILL.md +4 -0
- package/skills/yad-open-pr/SKILL.md +6 -0
- package/skills/yad-pr-template/SKILL.md +2 -1
- package/skills/yad-pr-template/templates/checks/pr-template.sh +8 -4
- package/skills/yad-pr-template/templates/checks/pr-title.sh +42 -6
- package/skills/yad-product-bridge/references/bridge.md +28 -0
- package/skills/yad-product-bridge/templates/github/yad-gate-sync.yml +25 -11
- package/skills/yad-product-bridge/templates/gitlab/yad-gate-sync.gitlab-ci.yml +38 -14
- package/skills/yad-run/SKILL.md +5 -0
- package/skills/yad-scan/SKILL.md +51 -0
- package/skills/yad-spec/SKILL.md +32 -2
- package/skills/yad-stories/SKILL.md +22 -4
- package/skills/yad-stories/references/story-schema.md +1 -1
- package/skills/yad-test-cases/SKILL.md +18 -2
- package/skills/yad-ui/SKILL.md +43 -8
- package/skills/yad-checks/templates/gitlab/.gitlab-ci.yml +0 -49
package/cli/hook.mjs
CHANGED
|
@@ -23,13 +23,15 @@
|
|
|
23
23
|
// brick an agent's ability to edit anything the moment a config went sideways. The asymmetry is the
|
|
24
24
|
// design: `ledger-guard` in CI fails closed and is what actually protects the ledger.
|
|
25
25
|
import { spawnSync } from 'node:child_process';
|
|
26
|
+
import { setTimeout, clearTimeout } from 'node:timers';
|
|
27
|
+
import { Worker } from 'node:worker_threads';
|
|
26
28
|
import fs from 'node:fs';
|
|
27
29
|
import path from 'node:path';
|
|
28
30
|
import { fileURLToPath } from 'node:url';
|
|
29
31
|
import { note, readJSON, run } from './lib.mjs';
|
|
30
32
|
import { isVerifiedLedger, mirrorDrift, productConfigPath, HOOK_PROJECT_DIR_ENVS, PROJECT_FILES } from './manifest.mjs';
|
|
31
33
|
import { DISCOVERY_EPIC, FOUNDATION_DIR, FOUNDATION_EPIC } from './epic-state.mjs';
|
|
32
|
-
import {
|
|
34
|
+
import { payloadCommand, isCursorPayload, JUDGE_BUDGET_MS } from './agent-guard.mjs';
|
|
33
35
|
|
|
34
36
|
// The CI-owned files, exactly as `templates/checks/ledger-guard.sh` lists them. NOT `contract-lock.json`
|
|
35
37
|
// (artifact-side: the architect commits it with the architecture) and NOT `change.json` — both are a
|
|
@@ -511,12 +513,38 @@ export function runStagedLedgerGuard({ env = process.env, cwd = process.cwd(), g
|
|
|
511
513
|
return 2;
|
|
512
514
|
}
|
|
513
515
|
|
|
516
|
+
// Read all of stdin. Asking `process.stdin.isTTY` makes Node open stdin as a stream, and on a pipe that
|
|
517
|
+
// switches fd 0 to non-blocking: a read then throws EAGAIN ("no data yet") whenever the payload arrives a
|
|
518
|
+
// moment after the hook starts. `fs.readFileSync(0)` gave up there, the hook read no payload, and it
|
|
519
|
+
// allowed — the macOS CI failures on #459 (a deny allowed) and #460 (no Cursor verdict: with no payload
|
|
520
|
+
// the hook could not tell it was Cursor). So this reads in a loop and, on EAGAIN, waits 5 ms and tries
|
|
521
|
+
// again, for up to `limitMs`; after that the error goes to the caller, which fails open. Keep the loop:
|
|
522
|
+
// the non-blocking mode is also what gives the limit — a blocking read would hang for ever on a parent
|
|
523
|
+
// that never closes stdin. A read of 0 bytes (or Windows' EOF error) is the end.
|
|
524
|
+
export function readStdin({ fd = 0, readSync = fs.readSync, limitMs = 5000 } = {}) {
|
|
525
|
+
const chunks = [];
|
|
526
|
+
const buf = Buffer.alloc(64 * 1024);
|
|
527
|
+
const nap = new Int32Array(new SharedArrayBuffer(4));
|
|
528
|
+
const until = Date.now() + limitMs;
|
|
529
|
+
for (;;) {
|
|
530
|
+
let n;
|
|
531
|
+
try { n = readSync(fd, buf, 0, buf.length, null); } catch (e) {
|
|
532
|
+
if (e?.code === 'EAGAIN' && Date.now() < until) { Atomics.wait(nap, 0, 0, 5); continue; }
|
|
533
|
+
if (e?.code === 'EOF') break;
|
|
534
|
+
throw e;
|
|
535
|
+
}
|
|
536
|
+
if (n === 0) break;
|
|
537
|
+
chunks.push(Buffer.from(buf.subarray(0, n)));
|
|
538
|
+
}
|
|
539
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
540
|
+
}
|
|
541
|
+
|
|
514
542
|
// Read the harness payload off stdin. Absent, empty, or unparseable all mean "nothing to inspect" —
|
|
515
543
|
// never an error, and never a block.
|
|
516
544
|
function readPayload() {
|
|
517
545
|
try {
|
|
518
546
|
if (process.stdin.isTTY) return null;
|
|
519
|
-
const raw =
|
|
547
|
+
const raw = readStdin().trim();
|
|
520
548
|
if (!raw) return null;
|
|
521
549
|
return JSON.parse(raw);
|
|
522
550
|
} catch {
|
|
@@ -581,10 +609,10 @@ const isReadOnlyCall = (payload) => {
|
|
|
581
609
|
};
|
|
582
610
|
|
|
583
611
|
export function runLedgerGuardHook({ paths = [], format = 'exit' } = {}) {
|
|
584
|
-
// Only read stdin when there is nothing else to go on.
|
|
585
|
-
//
|
|
586
|
-
//
|
|
587
|
-
//
|
|
612
|
+
// Only read stdin when there is nothing else to go on. A caller that passes `--path` (the documented
|
|
613
|
+
// stdin-free alternative) may well have inherited an open pipe from a long-lived parent — `isTTY` is
|
|
614
|
+
// false there, so the TTY guard does not trip, and `readStdin` would wait out its whole limit before
|
|
615
|
+
// going on. Every agent tool call would pay that wait.
|
|
588
616
|
const payload = paths.length ? null : readPayload();
|
|
589
617
|
const all = isReadOnlyCall(payload) ? [...paths] : [...new Set([...payloadPaths(payload), ...paths])];
|
|
590
618
|
const verdict = ledgerGuardDecision(all, {
|
|
@@ -618,20 +646,85 @@ export function runLedgerGuardHook({ paths = [], format = 'exit' } = {}) {
|
|
|
618
646
|
// `agentGuardDecision`, answered in the protocol the payload names (Cursor's `beforeShellExecution` wants a
|
|
619
647
|
// JSON verdict; Claude Code reads the exit code) unless `--format` says which. `YAD_HOOK_DISABLE` turns it
|
|
620
648
|
// off, as it does the ledger guard. Fail-open: no payload, or no command in it, is allowed.
|
|
621
|
-
|
|
649
|
+
//
|
|
650
|
+
// Pre-approval (E167, #353): under Claude Code, a line `agentGuardPreapproval` sees only reads is answered with
|
|
651
|
+
// a PreToolUse "allow" on stdout, so it runs with no permission prompt; any other allowed line gets no answer
|
|
652
|
+
// (exit 0, nothing printed), which leaves the prompt to Claude Code as before. `YAD_NO_PREAPPROVE=1` keeps the
|
|
653
|
+
// refusals and drops this half. Cursor's answer is unchanged.
|
|
654
|
+
//
|
|
655
|
+
// Fail-closed when judging fails (#498): the decision runs in a worker with a memory cap and a time limit
|
|
656
|
+
// (cli/agent-guard-worker.mjs). A line that runs it out of memory, past the limit, or into an error is refused:
|
|
657
|
+
// a hook that crashed would exit with a code the harness reads as no answer, and the command would run.
|
|
658
|
+
export async function runAgentGuardHook({ format = null } = {}) {
|
|
622
659
|
const payload = readPayload();
|
|
623
660
|
const cursor = format ? format === 'cursor' : isCursorPayload(payload);
|
|
624
661
|
if (format && !HOOK_FORMATS.includes(format)) {
|
|
625
662
|
note(`yad hook: unknown --format '${format}' — using '${HOOK_FORMATS[0]}' (known: ${HOOK_FORMATS.join(', ')})`);
|
|
626
663
|
}
|
|
627
|
-
|
|
664
|
+
// Claude Code names the project in CLAUDE_PROJECT_DIR and where the command runs in the payload's `cwd`.
|
|
665
|
+
const root = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
666
|
+
const preapproval = !cursor && !process.env.YAD_NO_PREAPPROVE ? { root, cwd: typeof payload?.cwd === 'string' && payload.cwd ? payload.cwd : root } : null;
|
|
667
|
+
// No command to judge (no payload, or none in it) fails open without a worker, as before.
|
|
668
|
+
const command = payloadCommand(payload);
|
|
669
|
+
const skip = process.env.YAD_HOOK_DISABLE || typeof command !== 'string' || !command.trim();
|
|
670
|
+
const { verdict, pre } = skip ? { verdict: { allow: true }, pre: null } : await judgeInWorker(command, preapproval);
|
|
628
671
|
if (cursor) {
|
|
629
672
|
process.stdout.write(`${verdict.allow ? CURSOR_ALLOW : cursorDeny(verdict.message)}\n`);
|
|
630
673
|
if (!verdict.allow) console.error(verdict.message);
|
|
631
674
|
return 0;
|
|
632
675
|
}
|
|
633
|
-
if (verdict.allow)
|
|
676
|
+
if (verdict.allow) {
|
|
677
|
+
if (pre?.preapproved) process.stdout.write(`${claudePreapproval(pre.reason)}\n`);
|
|
678
|
+
return 0;
|
|
679
|
+
}
|
|
634
680
|
console.error(verdict.message);
|
|
635
681
|
process.exitCode = 2;
|
|
636
682
|
return 2;
|
|
637
683
|
}
|
|
684
|
+
|
|
685
|
+
// The worker's limits: well above what the longest ordinary line takes (about 400 MB, under a second), and a
|
|
686
|
+
// wall clock past two of the guard's own 5 s budgets (pre-approval judges the line again), with slack. Past it
|
|
687
|
+
// with a verdict given, the verdict stands, not pre-approved; with none, the line is refused.
|
|
688
|
+
export const GUARD_HEAP_MB = 1024;
|
|
689
|
+
export const GUARD_WALL_MS = 2 * JUDGE_BUDGET_MS + 2000;
|
|
690
|
+
const NOT_JUDGED = (why) => ({ allow: false, message: `yad agent guard: this command could not be judged (${why}). Run it as separate commands, shorter, or ask the person to run it.` });
|
|
691
|
+
// The verdict (and pre-approval) for `command`, judged in a worker; a refusal when the worker does not answer.
|
|
692
|
+
export function judgeInWorker(command, preapproval, { heapMb = GUARD_HEAP_MB, wallMs = GUARD_WALL_MS } = {}) {
|
|
693
|
+
return new Promise((resolve) => {
|
|
694
|
+
let done = false;
|
|
695
|
+
let worker = null;
|
|
696
|
+
let timer = null;
|
|
697
|
+
// The verdict, once the worker has given it: after it, only pre-approval can be lost (it is then not given).
|
|
698
|
+
let verdict = null;
|
|
699
|
+
const lost = (why) => (verdict ? { verdict, pre: null } : { verdict: NOT_JUDGED(why), pre: null });
|
|
700
|
+
const finish = (answer) => {
|
|
701
|
+
if (done) return;
|
|
702
|
+
done = true;
|
|
703
|
+
clearTimeout(timer);
|
|
704
|
+
if (worker) worker.terminate().catch(() => {});
|
|
705
|
+
resolve(answer);
|
|
706
|
+
};
|
|
707
|
+
try {
|
|
708
|
+
worker = new Worker(new URL('./agent-guard-worker.mjs', import.meta.url), { workerData: { command, preapproval }, resourceLimits: { maxOldGenerationSizeMb: heapMb } });
|
|
709
|
+
} catch (e) {
|
|
710
|
+
finish({ verdict: NOT_JUDGED(e?.message ?? String(e)), pre: null });
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
timer = setTimeout(() => finish(lost(`no answer in ${wallMs / 1000} s`)), wallMs);
|
|
714
|
+
worker.on('message', (m) => {
|
|
715
|
+
if (!verdict) {
|
|
716
|
+
if (!m || typeof m.verdict?.allow !== 'boolean') { finish({ verdict: NOT_JUDGED('no verdict'), pre: null }); return; }
|
|
717
|
+
verdict = m.verdict;
|
|
718
|
+
if (!verdict.allow || !preapproval) finish({ verdict, pre: null });
|
|
719
|
+
return;
|
|
720
|
+
}
|
|
721
|
+
finish({ verdict, pre: m?.pre ?? null });
|
|
722
|
+
});
|
|
723
|
+
worker.once('error', (e) => finish(lost(e?.code === 'ERR_WORKER_OUT_OF_MEMORY' ? 'it ran out of memory' : (e?.message ?? String(e)))));
|
|
724
|
+
worker.once('exit', (code) => finish(lost(`the judge stopped (${code})`)));
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// Claude Code's PreToolUse answer that skips the permission prompt. A team's own `deny` or `ask` rule still wins.
|
|
729
|
+
export const claudePreapproval = (reason) => JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'allow',
|
|
730
|
+
permissionDecisionReason: `yad agent guard: a read-only command (${reason}), pre-approved (E167; YAD_NO_PREAPPROVE=1 turns this off)` } });
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// E170 — which tracker items (GitHub issues, GitLab work items) belong to an epic. One sidecar per epic,
|
|
2
|
+
// `.sdlc/issue-links.json`, holding only where the item is: its text stays on the platform, so private text
|
|
3
|
+
// does not spread into the repo. It is a separate file so `state.json` and `epic.md` keep their bytes, and
|
|
4
|
+
// the approvals bound to them stay valid.
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { readJSONStrict, writeJSON } from './lib.mjs';
|
|
8
|
+
import { err } from './errors.mjs';
|
|
9
|
+
import { epicIds, epicRoot, epicStories, featureStatus, loadLedger, PRODUCT_EPICS } from './epic-state.mjs';
|
|
10
|
+
|
|
11
|
+
export const ISSUE_LINKS_FILE = path.join('.sdlc', 'issue-links.json');
|
|
12
|
+
export const linksPath = (root, epic) => path.join(epicRoot(root, epic), ISSUE_LINKS_FILE);
|
|
13
|
+
export const itemKey = ({ host, repo, number }) => `${String(host).toLowerCase()}/${String(repo).toLowerCase()}#${number}`;
|
|
14
|
+
|
|
15
|
+
// The one rule for what a record is: an entry that fails it makes the whole file unreadable, never "not linked".
|
|
16
|
+
const validLink = (l) => !!l && typeof l === 'object' && !Array.isArray(l) && !!l.host && !!l.repo && Number.isInteger(l.number);
|
|
17
|
+
|
|
18
|
+
// Strict: this is read before a write, and a file that cannot be understood is refused, never replaced.
|
|
19
|
+
export function readLinks(root, epic) {
|
|
20
|
+
const p = linksPath(root, epic);
|
|
21
|
+
const doc = readJSONStrict(p, { items: [] });
|
|
22
|
+
if (!doc || typeof doc !== 'object' || !Array.isArray(doc.items) || !doc.items.every(validLink)) throw err('YAD-STATE-001', `${p} is not an issue-links file (it needs an "items" list of records)`, 'fix the file or restore it from git');
|
|
23
|
+
return doc.items;
|
|
24
|
+
}
|
|
25
|
+
export function writeLinks(root, epic, items) {
|
|
26
|
+
const p = linksPath(root, epic);
|
|
27
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
28
|
+
writeJSON(p, { items });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const featureEpics = (root) => epicIds(root).filter((e) => !PRODUCT_EPICS.includes(e));
|
|
32
|
+
|
|
33
|
+
// Every link in the Product, as Map(itemKey → [epic id]). An epic whose sidecar cannot be read is named in
|
|
34
|
+
// `unreadable`: its items would otherwise look unlinked, and "unknown" must never read as "none".
|
|
35
|
+
export function allLinks(root) {
|
|
36
|
+
const byItem = new Map();
|
|
37
|
+
const unreadable = [];
|
|
38
|
+
for (const epic of featureEpics(root)) {
|
|
39
|
+
let items;
|
|
40
|
+
try { items = readLinks(root, epic); } catch { unreadable.push(epic); continue; }
|
|
41
|
+
for (const l of items) {
|
|
42
|
+
const k = itemKey(l);
|
|
43
|
+
byItem.set(k, [...(byItem.get(k) || []), epic]);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { byItem, unreadable };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// An epic's stage (`featureStatus`), or null when its ledger cannot be read.
|
|
50
|
+
export function epicStage(root, epic) {
|
|
51
|
+
try {
|
|
52
|
+
const dir = epicRoot(root, epic);
|
|
53
|
+
return featureStatus(loadLedger(dir), { stories: epicStories(dir) });
|
|
54
|
+
} catch { return null; }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// One stage for an item with several epics: the first that has not shipped, else shipped. Null if any is unread.
|
|
58
|
+
export function combinedStage(stages) {
|
|
59
|
+
if (!stages.length || stages.some((s) => s === null)) return null;
|
|
60
|
+
return stages.find((s) => s !== 'shipped') ?? 'shipped';
|
|
61
|
+
}
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
// E173 — the agent acts on an issue or work item when an engineer asks: open one for an idea, assign it,
|
|
2
|
+
// set its priority or type, edit it, comment, close or reopen it. The request is the permission: the agent
|
|
3
|
+
// does it, then reports what changed with the link. It never does any of this on its own initiative.
|
|
4
|
+
//
|
|
5
|
+
// A new item goes to the Product repo unless the engineer names a registered repo. Every write is checked
|
|
6
|
+
// against what the platform answers, so a dropped label, assignee or state is reported, never hidden. A
|
|
7
|
+
// security item is never opened where it would announce the hole: on GitHub only a repo known to be private
|
|
8
|
+
// takes one, and on GitLab it is confidential. The text is the engineer's, so mentions and links stay, but
|
|
9
|
+
// anything shaped like a token is redacted.
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import { run, forTerminal, c, log, fail, hand, info, warn, emitJSON } from './lib.mjs';
|
|
12
|
+
import { addItemLabels, removeItemLabels, commentOnItem, closeItem, newItem, editItem, reopenItem, setAssignee, makeConfidential, platformLogin } from './platform.mjs';
|
|
13
|
+
import { api, httpStatus } from './protection.mjs';
|
|
14
|
+
import { labelRules, labelOwner, labelKey, readLabels, ITEM_TYPES, PRIORITIES } from './issues.mjs';
|
|
15
|
+
import { readItem, resolveTarget } from './issues-write.mjs';
|
|
16
|
+
import { allLinks, itemKey } from './issue-links.mjs';
|
|
17
|
+
import { redactTokens, repoFacts, routeFor } from './scan.mjs';
|
|
18
|
+
|
|
19
|
+
const MAX_TITLE = 256;
|
|
20
|
+
const MAX_BODY = 60000;
|
|
21
|
+
const LOGIN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
|
|
22
|
+
// Only tokens with a known prefix are redacted, so commit hashes and long names in the engineer's text stay.
|
|
23
|
+
const oneLine = (v) => forTerminal(redactTokens(String(v ?? ''))).replace(/\s+/g, ' ').trim();
|
|
24
|
+
const lines = (v) => redactTokens(String(v ?? '')).split(/\r?\n/).map((l) => forTerminal(l)).join('\n');
|
|
25
|
+
const REDACTED_NOTE = 'something shaped like a token was replaced with [redacted]';
|
|
26
|
+
// Only what redaction itself replaced: an engineer who typed `[redacted]` is not told something was removed.
|
|
27
|
+
const tokenIn = (...raws) => raws.some((r) => typeof r === 'string' && redactTokens(r) !== r);
|
|
28
|
+
// `p1` and `Feature` mean the same as `P1` and `feature`.
|
|
29
|
+
const normPriority = (v) => (v == null ? null : String(v).toUpperCase());
|
|
30
|
+
const normType = (v) => (v == null ? null : String(v).toLowerCase());
|
|
31
|
+
const NOT_PRIVATE = 'is not known to be a private repo (it is public, internal, or its visibility could not be read)';
|
|
32
|
+
|
|
33
|
+
class Refused extends Error {
|
|
34
|
+
constructor(message, hint = null) { super(message); this.hint = hint; }
|
|
35
|
+
}
|
|
36
|
+
const refusedOut = (json, e) => {
|
|
37
|
+
process.exitCode = 1;
|
|
38
|
+
if (json) return emitJSON({ ok: false, error: e.message, hint: e.hint });
|
|
39
|
+
fail(e.message); if (e.hint) hand(e.hint);
|
|
40
|
+
return undefined;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function titleOf(v) {
|
|
44
|
+
// The length is judged on what was written: redaction can shrink a long run to `[redacted]`.
|
|
45
|
+
if (String(v ?? '').trim().length > MAX_TITLE) throw new Refused(`the title is longer than ${MAX_TITLE} characters`);
|
|
46
|
+
const t = oneLine(v);
|
|
47
|
+
if (!t) throw new Refused('a title is needed', 'pass --title "<one line>"');
|
|
48
|
+
if (t.length > MAX_TITLE) throw new Refused(`the title is longer than ${MAX_TITLE} characters`);
|
|
49
|
+
return t;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The body from `--body` (one line) or `--body-file` (written by the agent outside the repo). Null if neither.
|
|
53
|
+
// The raw body last read, so a redaction note compares what was written with what is sent.
|
|
54
|
+
let lastRaw = null;
|
|
55
|
+
function bodyOf({ body, bodyFile }) {
|
|
56
|
+
lastRaw = null;
|
|
57
|
+
if (body != null && bodyFile != null) throw new Refused('pass --body or --body-file, not both');
|
|
58
|
+
if (bodyFile != null) {
|
|
59
|
+
let raw;
|
|
60
|
+
try { raw = fs.readFileSync(bodyFile, 'utf8'); } catch { throw new Refused('the --body-file could not be read'); }
|
|
61
|
+
if (raw.length > MAX_BODY) throw new Refused(`the body is longer than ${MAX_BODY} characters`);
|
|
62
|
+
lastRaw = raw;
|
|
63
|
+
const t = lines(raw);
|
|
64
|
+
if (t.length > MAX_BODY) throw new Refused(`the body is longer than ${MAX_BODY} characters`);
|
|
65
|
+
return t;
|
|
66
|
+
}
|
|
67
|
+
if (body != null && String(body).length > MAX_BODY) throw new Refused(`the body is longer than ${MAX_BODY} characters`);
|
|
68
|
+
lastRaw = body ?? null;
|
|
69
|
+
return body != null ? lines(body) : null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The repo: the Product unless a registered one is named. `ref` resolution does the registry and login checks.
|
|
73
|
+
export function targetOf(root, repo, { env, runner }) {
|
|
74
|
+
const r = resolveTarget(root, repo && repo !== 'Product' ? `${repo}#1` : '1', { env, runner });
|
|
75
|
+
if (!r.ok) throw new Refused(r.why, r.hint);
|
|
76
|
+
if (!r.t.ok) throw new Refused(`${forTerminal(r.label)}: ${r.t.why}`);
|
|
77
|
+
return { t: r.t, label: r.label, productConfig: r.productConfig };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function itemOf(root, ref, { env, runner }) {
|
|
81
|
+
const r = resolveTarget(root, ref, { env, runner });
|
|
82
|
+
if (!r.ok) throw new Refused(r.why, r.hint);
|
|
83
|
+
if (!r.t.ok) throw new Refused(`${forTerminal(r.label)}: ${r.t.why}`);
|
|
84
|
+
const item = readItem(r.t, r.number, { runner });
|
|
85
|
+
if (!item.ok) throw new Refused(item.why);
|
|
86
|
+
return { ...r, item, ref: `${r.label === 'Product' ? '' : r.label}#${r.number}` };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The login to assign: a name the engineer gave, or their own (`--me`).
|
|
90
|
+
function loginOf(root, t, { assignee, me, env, runner }) {
|
|
91
|
+
if (me) {
|
|
92
|
+
const l = platformLogin(root, t.platform, { host: t.host, runner, env });
|
|
93
|
+
if (!l) throw new Refused('your login on this platform could not be read', 'pass the login instead of --me');
|
|
94
|
+
return l;
|
|
95
|
+
}
|
|
96
|
+
if (!LOGIN.test(String(assignee ?? ''))) throw new Refused(`"${forTerminal(String(assignee ?? ''))}" is not a login`);
|
|
97
|
+
return assignee;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// GitLab assigns by user id. Exactly one user must have the login.
|
|
101
|
+
function gitlabUserId(t, login, runner) {
|
|
102
|
+
const res = api(runner, 'glab', t.host, `users?username=${encodeURIComponent(login)}`);
|
|
103
|
+
if (!res.ok || !Array.isArray(res.body)) throw new Refused(`the user ${forTerminal(login)} could not be looked up on ${t.host}`);
|
|
104
|
+
if (res.body.length !== 1 || !Number.isInteger(res.body[0]?.id)) throw new Refused(`${t.host} has no user ${forTerminal(login)}`);
|
|
105
|
+
return res.body[0].id;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const why = (t, r) => (httpStatus(r) ? `${t.host} answered HTTP ${httpStatus(r)}` : forTerminal(String(r.stderr || 'the platform refused it')).replace(/\s+/g, ' ').slice(0, 200));
|
|
109
|
+
|
|
110
|
+
function report(out, json) {
|
|
111
|
+
if (out.dropped.length) { out.ok = false; process.exitCode = 1; }
|
|
112
|
+
if (json) return out;
|
|
113
|
+
if (out.changed.length) log(` ${c.bold(forTerminal(out.ref))} ${out.changed.map(forTerminal).join('; ')}${out.url ? ` ${out.url}` : ''}`);
|
|
114
|
+
for (const n of out.notes) info(n);
|
|
115
|
+
for (const d of out.dropped) warn(`not as asked: ${forTerminal(d)}`);
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const answer = (ref, url) => ({ ok: true, ref, url: url ?? null, changed: [], dropped: [], notes: [] });
|
|
119
|
+
|
|
120
|
+
// ---- yad issues new --------------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
export async function runIssuesNew(root, { title, body = null, bodyFile = null, type: rawType = null, priority: rawPriority = null, assignee = null, me = false, repo = null, json = false, env = process.env, runner = run } = {}) {
|
|
123
|
+
const type = normType(rawType);
|
|
124
|
+
const priority = normPriority(rawPriority);
|
|
125
|
+
try {
|
|
126
|
+
const t0 = titleOf(title);
|
|
127
|
+
const text = bodyOf({ body, bodyFile }) ?? '';
|
|
128
|
+
if (type !== null && !ITEM_TYPES.includes(type)) throw new Refused(`unknown --type ${forTerminal(String(type))}`, `use one of ${ITEM_TYPES.join(', ')}`);
|
|
129
|
+
if (priority !== null && !PRIORITIES.includes(priority)) throw new Refused(`unknown --priority ${forTerminal(String(priority))}`, `use one of ${PRIORITIES.join(', ')}`);
|
|
130
|
+
if (assignee !== null && me) throw new Refused('pass --assignee or --me, not both');
|
|
131
|
+
const { t, label, productConfig } = targetOf(root, repo, { env, runner });
|
|
132
|
+
const rules = labelRules(productConfig);
|
|
133
|
+
// A vulnerability written in the open announces it before it is fixed.
|
|
134
|
+
let confidential = false;
|
|
135
|
+
if (type === 'security') {
|
|
136
|
+
if (t.platform === 'gitlab') confidential = true;
|
|
137
|
+
else if (routeFor('security', 'github', repoFacts(t, runner).vis) !== 'issue') {
|
|
138
|
+
throw new Refused(`${forTerminal(label)} ${NOT_PRIVATE}, so a security item there could be public`, 'open a private security advisory on GitHub instead, or file it in a private repo');
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const login = assignee !== null || me ? loginOf(root, t, { assignee, me, env, runner }) : null;
|
|
142
|
+
const who = login && t.platform === 'gitlab' ? gitlabUserId(t, login, runner) : login;
|
|
143
|
+
const labels = [...(type ? [rules.type[type][0]] : []), ...(priority ? [rules.priority[priority][0]] : [])];
|
|
144
|
+
if (t.platform === 'gitlab' && labels.some((l) => l.includes(','))) throw new Refused('a GitLab label name with a comma cannot be sent', 'rename that label in the issues settings of .sdlc/product.json');
|
|
145
|
+
const r = newItem(t, { title: t0, body: text, labels, assignee: who, confidential }, { runner });
|
|
146
|
+
if (!r.ok) throw new Refused(`the item was not created: ${why(t, r)}`);
|
|
147
|
+
let b = {};
|
|
148
|
+
try { b = JSON.parse(r.stdout); } catch { /* checked below */ }
|
|
149
|
+
const number = t.platform === 'github' ? b.number : b.iid;
|
|
150
|
+
const out = answer(`${label === 'Product' ? '' : label}#${number ?? '?'}`, b.html_url ?? b.web_url);
|
|
151
|
+
out.changed.push(`opened in ${label}${labels.length ? ` with ${labels.join(', ')}` : ''}${login ? `, assigned to ${login}` : ''}`);
|
|
152
|
+
const got = (Array.isArray(b.labels) ? b.labels : []).map((l) => String(typeof l === 'string' ? l : l?.name).toLowerCase());
|
|
153
|
+
for (const l of labels) if (!got.includes(l.toLowerCase())) out.dropped.push(`the label ${l} was not set`);
|
|
154
|
+
const logins = (Array.isArray(b.assignees) ? b.assignees : []).map((a) => String(a?.login ?? a?.username ?? '').toLowerCase());
|
|
155
|
+
if (login && !logins.includes(login.toLowerCase())) out.dropped.push(`${login} was not assigned`);
|
|
156
|
+
if (confidential && b.confidential !== true) out.dropped.push('it was NOT made confidential: restrict it now');
|
|
157
|
+
if (!Number.isInteger(number)) out.dropped.push('the answer named no item number');
|
|
158
|
+
if (tokenIn(title, lastRaw)) out.notes.push(REDACTED_NOTE);
|
|
159
|
+
return report(out, json);
|
|
160
|
+
} catch (e) {
|
|
161
|
+
if (e instanceof Refused) return refusedOut(json, e);
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ---- changing an item ------------------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
// Read, write, read again: what the platform shows afterwards is what is reported.
|
|
169
|
+
async function act(root, ref, { json, env, runner }, fn) {
|
|
170
|
+
try {
|
|
171
|
+
const got = itemOf(root, ref, { env, runner });
|
|
172
|
+
const out = answer(got.ref, got.item.url);
|
|
173
|
+
const r = await fn(got, out);
|
|
174
|
+
if (r && !r.ok) throw new Refused(`nothing was changed: ${why(got.t, r)}`);
|
|
175
|
+
return report(out, json);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
if (e instanceof Refused) return refusedOut(json, e);
|
|
178
|
+
throw e;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const reread = (got, runner) => { const r = readItem(got.t, got.number, { runner }); return r.ok ? r : null; };
|
|
183
|
+
|
|
184
|
+
export async function runIssuesAssign(root, { ref, assignee = null, me = false, none = false, json = false, env = process.env, runner = run } = {}) {
|
|
185
|
+
return act(root, ref, { json, env, runner }, async (got, out) => {
|
|
186
|
+
if ([assignee !== null, me, none].filter(Boolean).length !== 1) throw new Refused('name one: a login, --me or --none');
|
|
187
|
+
const login = none ? null : loginOf(root, got.t, { assignee, me, env, runner });
|
|
188
|
+
const who = login && got.t.platform === 'gitlab' ? gitlabUserId(got.t, login, runner) : login;
|
|
189
|
+
const r = setAssignee(got.t, got.number, who, { current: got.item.logins, runner });
|
|
190
|
+
if (!r.ok) return r;
|
|
191
|
+
const before = got.item.logins.filter((l) => !login || l.toLowerCase() !== login.toLowerCase());
|
|
192
|
+
out.changed.push(login ? `assigned to ${login}${before.length ? ` (replacing ${before.join(', ')})` : ''}` : `unassigned${before.length ? ` ${before.join(', ')}` : ''}`);
|
|
193
|
+
const after = reread(got, runner);
|
|
194
|
+
if (!after) out.notes.push('the item could not be read again to check the change');
|
|
195
|
+
else if (login ? !after.logins.map((l) => l.toLowerCase()).includes(login.toLowerCase()) : after.logins.length) out.dropped.push(login ? `${login} was not assigned` : 'someone is still assigned');
|
|
196
|
+
return null;
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// `--add` / `--remove` (#514) take any label but yad's own: those have a flag of their own, or yad writes them.
|
|
201
|
+
const OWNER_HINT = {
|
|
202
|
+
priority: 'set the priority with --priority',
|
|
203
|
+
type: 'set the type with --type',
|
|
204
|
+
status: 'yad writes the status label itself, from what the item and its epic say (`yad issues sync`)',
|
|
205
|
+
epic: 'yad writes an epic label itself: link the item with `yad issues link <ref> <EP-id>`',
|
|
206
|
+
};
|
|
207
|
+
const names = (v) => (v == null ? [] : Array.isArray(v) ? v : [v]).map((n) => String(n).trim());
|
|
208
|
+
// Every comparison of two label names is `labelKey` (cli/issues.mjs), never a spelling of its own (#514 reviews 1-2).
|
|
209
|
+
const fold = labelKey;
|
|
210
|
+
|
|
211
|
+
export async function runIssuesLabel(root, { ref, priority: rawPriority = null, type: rawType = null, add: rawAdd = [], remove: rawRemove = [], json = false, env = process.env, runner = run } = {}) {
|
|
212
|
+
const priority = normPriority(rawPriority);
|
|
213
|
+
const type = normType(rawType);
|
|
214
|
+
const plus = names(rawAdd);
|
|
215
|
+
const minus = names(rawRemove);
|
|
216
|
+
return act(root, ref, { json, env, runner }, async (got, out) => {
|
|
217
|
+
if (priority === null && type === null && !plus.length && !minus.length) throw new Refused('pass --priority, --type, --add or --remove');
|
|
218
|
+
if ([...plus, ...minus].some((n) => !n)) throw new Refused('--add and --remove need a label name');
|
|
219
|
+
const both = plus.filter((n) => minus.some((m) => fold(m) === fold(n)));
|
|
220
|
+
if (both.length) throw new Refused(`${forTerminal(both.join(', '))} is in both --add and --remove`, 'name each label once');
|
|
221
|
+
if (priority !== null && !PRIORITIES.includes(priority)) throw new Refused(`unknown --priority ${forTerminal(String(priority))}`, `use one of ${PRIORITIES.join(', ')}`);
|
|
222
|
+
if (type !== null && !ITEM_TYPES.includes(type)) throw new Refused(`unknown --type ${forTerminal(String(type))}`, `use one of ${ITEM_TYPES.join(', ')}`);
|
|
223
|
+
if (type === 'security' && got.t.platform === 'github' && !got.item.confidential && routeFor('security', 'github', repoFacts(got.t, runner).vis) !== 'issue') {
|
|
224
|
+
throw new Refused(`this repo ${NOT_PRIVATE}, so a security label could mark a public item as a vulnerability`, 'move the report to a private security advisory instead');
|
|
225
|
+
}
|
|
226
|
+
// A confidential item's labels are not read, so the old ones could not be removed.
|
|
227
|
+
if (got.item.confidential) throw new Refused('a confidential item\'s labels are not read, so they cannot be replaced here', 'change them on GitLab');
|
|
228
|
+
const rules = labelRules(got.productConfig);
|
|
229
|
+
for (const n of [...plus, ...minus]) {
|
|
230
|
+
const owner = labelOwner(n, rules);
|
|
231
|
+
if (owner) throw new Refused(`${forTerminal(n)} is a${owner === 'epic' ? 'n epic' : ` ${owner}`} label, which --add and --remove do not change`, OWNER_HINT[owner]);
|
|
232
|
+
}
|
|
233
|
+
const have = new Set(got.item.labels.map(fold));
|
|
234
|
+
const add = [];
|
|
235
|
+
const remove = [];
|
|
236
|
+
// The repo's labels, read once and only when needed.
|
|
237
|
+
let known = null;
|
|
238
|
+
const repoLabels = () => (known ??= readLabels(got.t, runner));
|
|
239
|
+
// A label is added only as the repo already spells it: a missing one is refused, never made by the platform.
|
|
240
|
+
if (plus.length) {
|
|
241
|
+
repoLabels();
|
|
242
|
+
if (!known.ok) throw new Refused(`the repo's labels could not be read, so --add was not checked: ${known.why}`, 'try again, or set the label on the platform');
|
|
243
|
+
const missing = plus.filter((n) => !known.keys.has(fold(n)));
|
|
244
|
+
if (missing.length) throw new Refused(`the repo has no label ${forTerminal(missing.join(', '))}`, 'create it on the platform first, then run this again; nothing was changed');
|
|
245
|
+
for (const n of plus) if (!have.has(fold(n)) && !add.some((a) => fold(a) === fold(n))) add.push(known.real.get(fold(n)) ?? n);
|
|
246
|
+
const already = [...new Map(plus.filter((n) => have.has(fold(n))).map((n) => [fold(n), n])).values()];
|
|
247
|
+
if (already.length) out.notes.push(`it already has ${already.map(forTerminal).join(', ')}`);
|
|
248
|
+
}
|
|
249
|
+
for (const n of minus) {
|
|
250
|
+
if (remove.some((r) => fold(r) === fold(n))) continue; // named twice: one removal, never a second that fails
|
|
251
|
+
if (have.has(fold(n))) remove.push(n);
|
|
252
|
+
else out.notes.push(`it has no label ${forTerminal(n)}, so there was nothing to remove`);
|
|
253
|
+
}
|
|
254
|
+
for (const [kind, value] of [['priority', priority], ['type', type]]) {
|
|
255
|
+
if (value === null) continue;
|
|
256
|
+
const want = rules[kind][value][0];
|
|
257
|
+
if (!rules[kind][value].some((l) => have.has(fold(l)))) add.push(want);
|
|
258
|
+
for (const [other, names] of Object.entries(rules[kind])) if (other !== value) remove.push(...names.filter((l) => have.has(fold(l))));
|
|
259
|
+
}
|
|
260
|
+
// Removed as the repo spells it: an item's labels are read cleaned (spaces folded), and a DELETE or a GitLab
|
|
261
|
+
// remove_labels of the cleaned spelling misses a label like `needs design`. If the repo's labels cannot be
|
|
262
|
+
// read, the item's own spelling is sent, and the read-back says if it stayed.
|
|
263
|
+
const realNames = new Map(got.item.labels.map((l) => [fold(l), l]));
|
|
264
|
+
if (remove.length) repoLabels();
|
|
265
|
+
const spelled = (l) => (known?.ok ? known.real.get(fold(l)) : null) ?? realNames.get(fold(l)) ?? l;
|
|
266
|
+
const stale = [...new Map(remove.map((l) => [fold(l), spelled(l)])).values()];
|
|
267
|
+
if (got.t.platform === 'gitlab' && [...add, ...stale].some((l) => l.includes(','))) throw new Refused('a GitLab label name with a comma cannot be sent', 'rename that label in the issues settings of .sdlc/product.json');
|
|
268
|
+
// On GitLab a vulnerability is confidential first, so the label is never on a public item. This runs even when
|
|
269
|
+
// the label is already there: a public item that carries it is exactly what must not stay.
|
|
270
|
+
let madeConfidential = false;
|
|
271
|
+
if (type === 'security' && got.t.platform === 'gitlab') {
|
|
272
|
+
const r = makeConfidential(got.t, got.number, { runner });
|
|
273
|
+
if (!r.ok) return r;
|
|
274
|
+
const after = reread(got, runner);
|
|
275
|
+
if (!after || !after.confidential) throw new Refused('the item could not be made confidential, so it was not marked as a vulnerability', 'make it confidential on GitLab first');
|
|
276
|
+
out.changed.push('made confidential');
|
|
277
|
+
madeConfidential = true;
|
|
278
|
+
}
|
|
279
|
+
if (!add.length && !stale.length) { if (priority !== null || type !== null || !out.notes.length) out.notes.push('it already has those labels'); return null; }
|
|
280
|
+
if (add.length) {
|
|
281
|
+
const r = addItemLabels(got.t, got.number, add, { runner });
|
|
282
|
+
if (!r.ok && !madeConfidential) return r;
|
|
283
|
+
if (r.ok) out.changed.push(`labels +${add.join(', +')}`); else out.dropped.push(`the labels ${add.join(', ')} were not set`);
|
|
284
|
+
}
|
|
285
|
+
if (stale.length) { const r = removeItemLabels(got.t, got.number, stale, { runner }); if (r.ok) out.changed.push(`labels -${stale.join(', -')}`); else out.dropped.push(`the old labels ${stale.join(', ')} were not removed`); }
|
|
286
|
+
// A confidential item's labels are not read back, so they cannot be checked.
|
|
287
|
+
const after = madeConfidential ? null : reread(got, runner);
|
|
288
|
+
if (madeConfidential) out.notes.push('it is confidential now, so its labels could not be read back to check');
|
|
289
|
+
else if (!after) out.notes.push('the item could not be read again to check the change');
|
|
290
|
+
else {
|
|
291
|
+
const now = new Set(after.labels.map(fold));
|
|
292
|
+
for (const l of add) if (!now.has(fold(l))) out.dropped.push(`the label ${l} was not set`);
|
|
293
|
+
for (const l of stale) if (now.has(fold(l))) out.dropped.push(`the label ${l} is still there`);
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export async function runIssuesEdit(root, { ref, title = null, body = null, bodyFile = null, json = false, env = process.env, runner = run } = {}) {
|
|
300
|
+
return act(root, ref, { json, env, runner }, async (got, out) => {
|
|
301
|
+
const fields = {};
|
|
302
|
+
if (title !== null) fields.title = titleOf(title);
|
|
303
|
+
const text = bodyOf({ body, bodyFile });
|
|
304
|
+
if (text !== null) fields.body = text;
|
|
305
|
+
if (!Object.keys(fields).length) throw new Refused('pass --title, --body or --body-file');
|
|
306
|
+
const r = editItem(got.t, got.number, fields, { runner });
|
|
307
|
+
if (!r.ok) return r;
|
|
308
|
+
out.changed.push(`edited the ${Object.keys(fields).join(' and ')}`);
|
|
309
|
+
if (tokenIn(title, lastRaw)) out.notes.push(REDACTED_NOTE);
|
|
310
|
+
if (fields.title && !got.item.confidential) {
|
|
311
|
+
const after = reread(got, runner);
|
|
312
|
+
if (after && after.title !== fields.title) out.dropped.push('the title is not the one sent');
|
|
313
|
+
}
|
|
314
|
+
return null;
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export async function runIssuesComment(root, { ref, body = null, bodyFile = null, json = false, env = process.env, runner = run } = {}) {
|
|
319
|
+
return act(root, ref, { json, env, runner }, async (got, out) => {
|
|
320
|
+
const text = bodyOf({ body, bodyFile });
|
|
321
|
+
if (!text || !text.trim()) throw new Refused('the comment is empty', 'pass --body "<text>" or --body-file <file>');
|
|
322
|
+
const r = commentOnItem(got.t, got.number, text, { runner });
|
|
323
|
+
if (!r.ok) return r;
|
|
324
|
+
out.changed.push('commented');
|
|
325
|
+
if (tokenIn(lastRaw)) out.notes.push(REDACTED_NOTE);
|
|
326
|
+
return null;
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// The epics an item is linked to, for the note a close or reopen adds.
|
|
331
|
+
function linkedEpics(root, got) {
|
|
332
|
+
try { return allLinks(root).byItem.get(itemKey({ host: got.t.host, repo: got.t.repo, number: got.number })) ?? []; } catch { return []; }
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export async function runIssuesClose(root, { ref, json = false, env = process.env, runner = run } = {}) {
|
|
336
|
+
return act(root, ref, { json, env, runner }, async (got, out) => {
|
|
337
|
+
if (got.item.state === 'closed') { out.notes.push('it is already closed'); return null; }
|
|
338
|
+
const r = closeItem(got.t, got.number, { runner });
|
|
339
|
+
if (!r.ok) return r;
|
|
340
|
+
out.changed.push('closed');
|
|
341
|
+
const epics = linkedEpics(root, got);
|
|
342
|
+
if (epics.length) out.notes.push(`it is linked to ${epics.join(', ')}; its status shows done from now on`);
|
|
343
|
+
const after = reread(got, runner);
|
|
344
|
+
if (after && after.state !== 'closed') out.dropped.push('it is still open');
|
|
345
|
+
return null;
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export async function runIssuesReopen(root, { ref, json = false, env = process.env, runner = run } = {}) {
|
|
350
|
+
return act(root, ref, { json, env, runner }, async (got, out) => {
|
|
351
|
+
if (got.item.state === 'open') { out.notes.push('it is already open'); return null; }
|
|
352
|
+
const r = reopenItem(got.t, got.number, { runner });
|
|
353
|
+
if (!r.ok) return r;
|
|
354
|
+
out.changed.push('reopened');
|
|
355
|
+
if (linkedEpics(root, got).length) out.notes.push('`yad issues sync` will not close it again: a reopened item is the team\'s call');
|
|
356
|
+
const after = reread(got, runner);
|
|
357
|
+
if (after && after.state !== 'open') out.dropped.push('it is still closed');
|
|
358
|
+
return null;
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const ACT_FLAGS = {
|
|
363
|
+
new: ['--title', '--body', '--body-file', '--type', '--priority', '--assignee', '--me', '--repo'],
|
|
364
|
+
assign: ['--me', '--none'],
|
|
365
|
+
label: ['--priority', '--type', '--add', '--remove'],
|
|
366
|
+
edit: ['--title', '--body', '--body-file'],
|
|
367
|
+
comment: ['--body', '--body-file'],
|
|
368
|
+
close: [], reopen: [],
|
|
369
|
+
};
|
|
370
|
+
export const ACT_ACTIONS = Object.keys(ACT_FLAGS);
|
|
371
|
+
export const actForeignFlags = (argv, action) => argv.filter((a) => a.startsWith('-') && ![...ACT_FLAGS[action], '--json', '--dir'].includes(a.split('=')[0]));
|