@mmerterden/multi-agent-pipeline 20.42.0 → 20.43.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 +12 -0
- package/README.md +7 -5
- package/README.tr.md +7 -5
- package/docs/architecture.md +3 -3
- package/docs/ecosystem.md +6 -6
- package/docs/facts.json +5 -5
- package/manifest.json +40 -33
- package/package.json +1 -1
- package/pipeline/commands/multi-agent/clean-disk/SKILL.md +134 -0
- package/pipeline/commands/multi-agent/review-priority/SKILL.md +143 -0
- package/pipeline/commands/multi-agent/sync/SKILL.md +4 -4
- package/pipeline/lib/low-disk.mjs +6 -5
- package/pipeline/multi-agent-refs/cross-cli-contract.md +5 -5
- package/pipeline/multi-agent-refs/features/doctor.md +17 -2
- package/pipeline/multi-agent-refs/features/review-priority.md +123 -0
- package/pipeline/multi-agent-refs/features/usage-reporting.md +1 -1
- package/pipeline/multi-agent-refs/help/en.md +2 -0
- package/pipeline/multi-agent-refs/help/tr.md +2 -0
- package/pipeline/multi-agent-refs/phases/phase-2-dev.md +1 -1
- package/pipeline/multi-agent-refs/review-priority/rules/example.md +137 -0
- package/pipeline/office/toolkit.json +1 -1
- package/pipeline/schemas/prefs.schema.json +19 -1
- package/pipeline/schemas/toolkit-path-params.json +1 -1
- package/pipeline/scripts/capture-resume.sh +1 -1
- package/pipeline/scripts/disk-cleaner.mjs +77 -63
- package/pipeline/scripts/doctor.mjs +140 -10
- package/pipeline/scripts/priority-review.mjs +912 -0
- package/pipeline/skills/.skill-manifest.json +12 -4
- package/pipeline/skills/.skills-index.json +24 -2
- package/pipeline/skills/shared/README.md +6 -4
- package/pipeline/skills/shared/core/multi-agent-clean-disk/SKILL.md +34 -0
- package/pipeline/skills/shared/core/multi-agent-review-priority/SKILL.md +36 -0
- package/pipeline/skills/shared/core/multi-agent-sync/SKILL.md +4 -4
- package/pipeline/skills/shared/external/disk-cleaner/SKILL.md +10 -4
- package/pipeline/skills/shared/external/disk-cleaner/evals/evals.json +6 -6
- package/pipeline/skills/shared/external/disk-cleaner/scripts/disk-cleaner.mjs +77 -63
- package/pipeline/skills/skills-index.md +4 -2
|
@@ -0,0 +1,912 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* @file priority-review.mjs - the engine behind /multi-agent:review-priority.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* priority-review.mjs paths [--json]
|
|
7
|
+
* priority-review.mjs teams [--json]
|
|
8
|
+
* priority-review.mjs rules --team SLUG [--json]
|
|
9
|
+
* priority-review.mjs presets --team SLUG [--json]
|
|
10
|
+
* priority-review.mjs fetch --team SLUG [--preset ID] [--days N] [--jql EXTRA]
|
|
11
|
+
* [--max N] --out FILE
|
|
12
|
+
* priority-review.mjs report --team SLUG --bugs FILE --evals FILE --out FILE
|
|
13
|
+
* priority-review.mjs propose --issue KEY --to PRIORITY --rule IDS --reason TEXT
|
|
14
|
+
* priority-review.mjs apply --issue KEY --to PRIORITY --confirm TOKEN
|
|
15
|
+
*
|
|
16
|
+
* Everything team-specific (the JQL presets, the evidence fields, the rule
|
|
17
|
+
* ids) comes from one rule file per team in the rules directory:
|
|
18
|
+
* prefs.global.reviewPriority.rulesDir, default
|
|
19
|
+
* ~/.claude/routine-assets/review-priority/rules. An optional corporate.md
|
|
20
|
+
* there is a baseline every team inherits; a team's own row with the same id
|
|
21
|
+
* replaces the baseline row, and `rules` and the report name the file each id
|
|
22
|
+
* came from. A team is offered only when its file parses. The format is in
|
|
23
|
+
* multi-agent-refs/features/review-priority.md.
|
|
24
|
+
*
|
|
25
|
+
* Every request is a GET except `apply`, which writes the priority field of
|
|
26
|
+
* one issue, only with the token `propose` computed from the live issue, so a
|
|
27
|
+
* priority that moved in between is refused. `propose` and `apply` refuse
|
|
28
|
+
* outright when MULTI_AGENT_UNATTENDED=1. The Jira host is
|
|
29
|
+
* prefs.global.hosts.jira; the token comes from the credential store (or
|
|
30
|
+
* JIRA_TOKEN) inside this process and travels only in a request header.
|
|
31
|
+
*
|
|
32
|
+
* Exit: 0 done, 1 refused or failed, 2 usage error.
|
|
33
|
+
*
|
|
34
|
+
* @module priority-review
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { createHash } from "node:crypto";
|
|
38
|
+
import {
|
|
39
|
+
existsSync,
|
|
40
|
+
mkdirSync,
|
|
41
|
+
readFileSync,
|
|
42
|
+
readdirSync,
|
|
43
|
+
realpathSync,
|
|
44
|
+
writeFileSync,
|
|
45
|
+
} from "node:fs";
|
|
46
|
+
import { homedir } from "node:os";
|
|
47
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
48
|
+
import { pathToFileURL } from "node:url";
|
|
49
|
+
|
|
50
|
+
import { ArgsError, parseArgs } from "../lib/args.mjs";
|
|
51
|
+
import { resolveTokenAsync } from "../lib/credential-resolve.mjs";
|
|
52
|
+
import { globalPref } from "../lib/prefs-value.mjs";
|
|
53
|
+
import { hostCommand, messages, outputLanguage } from "../lib/user-facing.mjs";
|
|
54
|
+
|
|
55
|
+
export const SCALE = ["Critical", "High", "Medium", "Low"];
|
|
56
|
+
export const VERDICT_IDS = ["too_high", "too_low", "insufficient", "correct"];
|
|
57
|
+
export const CONFIDENCE_IDS = ["high", "medium", "low"];
|
|
58
|
+
// Turkish spellings of the confidence ids, accepted so evaluations written with them validate.
|
|
59
|
+
const CONFIDENCE_ALIASES = { yuksek: "high", orta: "medium", dusuk: "low" };
|
|
60
|
+
const confidenceId = (c) => CONFIDENCE_ALIASES[c] ?? c;
|
|
61
|
+
const BASE_FIELDS = [
|
|
62
|
+
"summary",
|
|
63
|
+
"status",
|
|
64
|
+
"priority",
|
|
65
|
+
"created",
|
|
66
|
+
"updated",
|
|
67
|
+
"issuetype",
|
|
68
|
+
"components",
|
|
69
|
+
"labels",
|
|
70
|
+
"project",
|
|
71
|
+
"description",
|
|
72
|
+
"comment",
|
|
73
|
+
];
|
|
74
|
+
const TEXT_LIMIT = 1500;
|
|
75
|
+
const COMMENT_LIMIT = 3;
|
|
76
|
+
const COMMENT_TEXT_LIMIT = 600;
|
|
77
|
+
const PAGE = 100;
|
|
78
|
+
const TIMEOUT_MS = 90_000;
|
|
79
|
+
const RULE_ROW = /^\| (PRI-\d{3}) \|.*$/gm;
|
|
80
|
+
export const CORPORATE_FILE = "corporate.md";
|
|
81
|
+
const SELECTION_BLOCK = /```json jira-selection\n([\s\S]*?)\n```/;
|
|
82
|
+
|
|
83
|
+
export const TEXT = {
|
|
84
|
+
en: {
|
|
85
|
+
title: (name, day) => `# ${name} - Bug priority review (${day})`,
|
|
86
|
+
team: "Team",
|
|
87
|
+
scope: "Scope",
|
|
88
|
+
records: (total, n) => `total ${total}, reviewed ${n}`,
|
|
89
|
+
recordsLabel: "Records",
|
|
90
|
+
source: "Rule source",
|
|
91
|
+
baseline: "corporate baseline",
|
|
92
|
+
sources: "Where each cited rule comes from",
|
|
93
|
+
file: "File",
|
|
94
|
+
overrides: (ids) => `team overrides: ${ids.join(", ")}`,
|
|
95
|
+
rules: (n) => `${n} rules`,
|
|
96
|
+
readOnly: "Read-only report. No Jira field was changed.",
|
|
97
|
+
summary: "Summary",
|
|
98
|
+
result: "Result",
|
|
99
|
+
count: "Count",
|
|
100
|
+
current: "Current",
|
|
101
|
+
proposed: "Proposed",
|
|
102
|
+
verdicts: {
|
|
103
|
+
too_high: "Too high",
|
|
104
|
+
too_low: "Too low",
|
|
105
|
+
insufficient: "Not enough information",
|
|
106
|
+
correct: "Correct",
|
|
107
|
+
},
|
|
108
|
+
confidence: { high: "high", medium: "medium", low: "low" },
|
|
109
|
+
mismatches: "Mismatches",
|
|
110
|
+
evidence: "Evidence",
|
|
111
|
+
insufficient: "Not enough information",
|
|
112
|
+
correct: "Correct",
|
|
113
|
+
none: "None.",
|
|
114
|
+
open: "Show the list",
|
|
115
|
+
head: {
|
|
116
|
+
issue: "Issue",
|
|
117
|
+
summary: "Summary",
|
|
118
|
+
verdict: "Result",
|
|
119
|
+
rules: "Rules",
|
|
120
|
+
confidence: "Confidence",
|
|
121
|
+
reason: "Reason",
|
|
122
|
+
guess: "Best guess",
|
|
123
|
+
missing: "Missing",
|
|
124
|
+
priority: "Priority",
|
|
125
|
+
},
|
|
126
|
+
more: (n) => ` and ${n} more mismatch(es) in the report`,
|
|
127
|
+
fetched: (total, n, out) => `total ${total}, fetched ${n} -> ${out}`,
|
|
128
|
+
report: (out) => `report: ${out}`,
|
|
129
|
+
pending: "AWAITING APPROVAL - nothing was written.",
|
|
130
|
+
noRules: (slug, dir) => `no valid rule file for "${slug}" in ${dir}`,
|
|
131
|
+
noHost: (cmd) => `prefs.global.hosts.jira is not set; run ${cmd}`,
|
|
132
|
+
noToken: (cmd) => `no Jira token in the credential store (keychainMapping.jira); run ${cmd}`,
|
|
133
|
+
jqlFailed: (status) => `the search failed (HTTP ${status})`,
|
|
134
|
+
unreadable: (key, status) => `${key} could not be read (HTTP ${status})`,
|
|
135
|
+
unattended: "Unattended run: the write path is closed. Only the report is produced.",
|
|
136
|
+
badTarget: `--to must be one of ${SCALE.join(", ")}`,
|
|
137
|
+
already: (key, to) => `${key} is already ${to}; nothing to change`,
|
|
138
|
+
tokenMismatch:
|
|
139
|
+
"The token does not match: the issue changed after propose. Run propose again and ask the user again.",
|
|
140
|
+
writeFailed: (status) => `the write failed (HTTP ${status})`,
|
|
141
|
+
invalidEvals: (n) => `the evaluations file is invalid (${n} error(s)):`,
|
|
142
|
+
wrongTeam: (got, want) => `the bugs file is for "${got}", the report was asked for "${want}"`,
|
|
143
|
+
noPreset: (id, ids) => `no preset "${id}"; presets: ${ids.join(", ")}`,
|
|
144
|
+
skipped: "skipped",
|
|
145
|
+
},
|
|
146
|
+
tr: {
|
|
147
|
+
title: (name, day) => `# ${name} - Bug priority denetimi (${day})`,
|
|
148
|
+
team: "Ekip",
|
|
149
|
+
scope: "Kapsam",
|
|
150
|
+
records: (total, n) => `toplam ${total}, incelenen ${n}`,
|
|
151
|
+
recordsLabel: "Kayıt",
|
|
152
|
+
source: "Kural kaynağı",
|
|
153
|
+
baseline: "kurumsal temel",
|
|
154
|
+
sources: "Atıf yapılan kuralların kaynağı",
|
|
155
|
+
file: "Dosya",
|
|
156
|
+
overrides: (ids) => `ekibin ezdiği kurallar: ${ids.join(", ")}`,
|
|
157
|
+
rules: (n) => `${n} kural`,
|
|
158
|
+
readOnly: "Salt okuma raporudur. Jira'da hiçbir alan değiştirilmedi.",
|
|
159
|
+
summary: "Özet",
|
|
160
|
+
result: "Sonuç",
|
|
161
|
+
count: "Adet",
|
|
162
|
+
current: "Mevcut",
|
|
163
|
+
proposed: "Önerilen",
|
|
164
|
+
verdicts: {
|
|
165
|
+
too_high: "Fazla yüksek",
|
|
166
|
+
too_low: "Fazla düşük",
|
|
167
|
+
insufficient: "Yetersiz bilgi",
|
|
168
|
+
correct: "Doğru",
|
|
169
|
+
},
|
|
170
|
+
confidence: { high: "yüksek", medium: "orta", low: "düşük" },
|
|
171
|
+
mismatches: "Uyuşmayanlar",
|
|
172
|
+
evidence: "Kanıtlar",
|
|
173
|
+
insufficient: "Yetersiz bilgi",
|
|
174
|
+
correct: "Doğru",
|
|
175
|
+
none: "Yok.",
|
|
176
|
+
open: "Listeyi aç",
|
|
177
|
+
head: {
|
|
178
|
+
issue: "Kayıt",
|
|
179
|
+
summary: "Başlık",
|
|
180
|
+
verdict: "Sonuç",
|
|
181
|
+
rules: "Kurallar",
|
|
182
|
+
confidence: "Güven",
|
|
183
|
+
reason: "Gerekçe",
|
|
184
|
+
guess: "Tahmin",
|
|
185
|
+
missing: "Eksik",
|
|
186
|
+
priority: "Priority",
|
|
187
|
+
},
|
|
188
|
+
more: (n) => ` ve ${n} uyuşmayan kayıt daha (raporda)`,
|
|
189
|
+
fetched: (total, n, out) => `toplam ${total}, çekilen ${n} -> ${out}`,
|
|
190
|
+
report: (out) => `rapor: ${out}`,
|
|
191
|
+
pending: "ONAY BEKLENİYOR - hiçbir şey yazılmadı.",
|
|
192
|
+
noRules: (slug, dir) => `"${slug}" için ${dir} içinde geçerli kural dosyası yok`,
|
|
193
|
+
noHost: (cmd) => `prefs.global.hosts.jira tanımlı değil; ${cmd} çalıştır`,
|
|
194
|
+
noToken: (cmd) => `kimlik deposunda Jira token'ı yok (keychainMapping.jira); ${cmd} çalıştır`,
|
|
195
|
+
jqlFailed: (status) => `arama başarısız (HTTP ${status})`,
|
|
196
|
+
unreadable: (key, status) => `${key} okunamadı (HTTP ${status})`,
|
|
197
|
+
unattended: "Gözetimsiz koşu: yazma yolu kapalı. Yalnızca rapor üretilir.",
|
|
198
|
+
badTarget: `--to şunlardan biri olmalı: ${SCALE.join(", ")}`,
|
|
199
|
+
already: (key, to) => `${key} zaten ${to}; değişiklik yok`,
|
|
200
|
+
tokenMismatch:
|
|
201
|
+
"Token eşleşmedi: kayıt propose'dan sonra değişmiş. propose'u yeniden çalıştır ve kullanıcıya yeniden sor.",
|
|
202
|
+
writeFailed: (status) => `yazma başarısız (HTTP ${status})`,
|
|
203
|
+
invalidEvals: (n) => `değerlendirme dosyası geçersiz (${n} hata):`,
|
|
204
|
+
wrongTeam: (got, want) => `bugs dosyası "${got}" için, rapor "${want}" için istendi`,
|
|
205
|
+
noPreset: (id, ids) => `"${id}" adında preset yok; presetler: ${ids.join(", ")}`,
|
|
206
|
+
skipped: "atlandı",
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
class Refusal extends Error {}
|
|
211
|
+
class UsageError extends Error {}
|
|
212
|
+
|
|
213
|
+
// ---------------------------------------------------------------- rule files
|
|
214
|
+
|
|
215
|
+
/** The rules directory: the pref, else the default under the home directory. */
|
|
216
|
+
export function rulesDir(env = process.env) {
|
|
217
|
+
const home = env.HOME || homedir();
|
|
218
|
+
const fromPref = globalPref("reviewPriority.rulesDir", null, env);
|
|
219
|
+
if (typeof fromPref === "string" && fromPref.trim()) {
|
|
220
|
+
return resolve(fromPref.trim().replace(/^~(?=\/|$)/, home));
|
|
221
|
+
}
|
|
222
|
+
return join(home, ".claude", "routine-assets", "review-priority", "rules");
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Run data sits beside the rules directory, never inside a repository. */
|
|
226
|
+
export function runsDir(env = process.env) {
|
|
227
|
+
return join(dirname(rulesDir(env)), "runs");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function frontmatter(text) {
|
|
231
|
+
const m = text.match(/^---\n([\s\S]*?)\n---\n/);
|
|
232
|
+
const out = {};
|
|
233
|
+
if (!m) return out;
|
|
234
|
+
for (const line of m[1].split("\n")) {
|
|
235
|
+
const i = line.indexOf(":");
|
|
236
|
+
if (i > 0)
|
|
237
|
+
out[line.slice(0, i).trim()] = line
|
|
238
|
+
.slice(i + 1)
|
|
239
|
+
.trim()
|
|
240
|
+
.replace(/^"|"$/g, "");
|
|
241
|
+
}
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Rule rows of a file: `| PRI-nnn | ... |` lines keyed by id. */
|
|
246
|
+
export function ruleRows(text) {
|
|
247
|
+
const rows = new Map();
|
|
248
|
+
for (const m of text.matchAll(RULE_ROW)) rows.set(m[1], m[0]);
|
|
249
|
+
return rows;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The corporate baseline: rule rows only, shared by every team. */
|
|
253
|
+
export function parseCorporateFile(path) {
|
|
254
|
+
const rows = ruleRows(readFileSync(path, "utf8"));
|
|
255
|
+
if (!rows.size) throw new Error("no rule rows");
|
|
256
|
+
return { path, rows };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The rules a team is judged by: the corporate baseline, then the team's own
|
|
261
|
+
* rows, a team row replacing the baseline row with the same id. Each id keeps
|
|
262
|
+
* the file it came from, which the report prints.
|
|
263
|
+
*/
|
|
264
|
+
export function withBaseline(team, corporate) {
|
|
265
|
+
const merged = new Map();
|
|
266
|
+
if (corporate) {
|
|
267
|
+
for (const [id, row] of corporate.rows) {
|
|
268
|
+
merged.set(id, { id, row, source: basename(corporate.path) });
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const overrides = [];
|
|
272
|
+
for (const [id, row] of team.rows) {
|
|
273
|
+
if (merged.has(id)) overrides.push(id);
|
|
274
|
+
merged.set(id, { id, row, source: basename(team.path) });
|
|
275
|
+
}
|
|
276
|
+
if (!merged.size) throw new Error("no rule rows");
|
|
277
|
+
const ruleSet = [...merged.values()].sort((a, b) => a.id.localeCompare(b.id));
|
|
278
|
+
return {
|
|
279
|
+
...team,
|
|
280
|
+
corporate: corporate ? corporate.path : null,
|
|
281
|
+
ruleSet,
|
|
282
|
+
rules: ruleSet.map((r) => r.id),
|
|
283
|
+
overrides: overrides.sort(),
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** A team file on its own, merged with nothing. */
|
|
288
|
+
export function parseRuleFile(path) {
|
|
289
|
+
return withBaseline(readTeamFile(path), null);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* One team file: its frontmatter names the team (`team:`, or `directorate:`
|
|
294
|
+
* in older files), its `jira-selection` block says how the team's bugs are
|
|
295
|
+
* found, and its `| PRI-nnn |` rows are the team's own rules. It may carry no
|
|
296
|
+
* rows when a corporate baseline supplies them.
|
|
297
|
+
*/
|
|
298
|
+
function readTeamFile(path) {
|
|
299
|
+
const text = readFileSync(path, "utf8");
|
|
300
|
+
const fm = frontmatter(text);
|
|
301
|
+
const block = text.match(SELECTION_BLOCK);
|
|
302
|
+
if (!block) throw new Error("no jira-selection json block");
|
|
303
|
+
const selection = JSON.parse(block[1]);
|
|
304
|
+
for (const k of ["base", "default", "presets"]) {
|
|
305
|
+
if (!(k in selection)) throw new Error(`jira-selection: "${k}" is missing`);
|
|
306
|
+
}
|
|
307
|
+
if (!Array.isArray(selection.presets) || !selection.presets.length) {
|
|
308
|
+
throw new Error("jira-selection: presets must be a non-empty list");
|
|
309
|
+
}
|
|
310
|
+
for (const p of selection.presets) {
|
|
311
|
+
if (!p.id || !p.jql) throw new Error("jira-selection: every preset needs id and jql");
|
|
312
|
+
}
|
|
313
|
+
if (!selection.presets.some((p) => p.id === selection.default)) {
|
|
314
|
+
throw new Error("jira-selection: the default preset is not in the list");
|
|
315
|
+
}
|
|
316
|
+
const stem = basename(path).replace(/\.md$/, "");
|
|
317
|
+
const slug = fm.slug || stem;
|
|
318
|
+
if (slug !== stem) throw new Error("slug must equal the file name");
|
|
319
|
+
return { slug, name: fm.team || fm.directorate || slug, path, selection, rows: ruleRows(text) };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Every team whose file parses, merged with the corporate baseline when
|
|
324
|
+
* `corporate.md` exists, and every file that does not parse with its reason.
|
|
325
|
+
* A baseline that does not parse leaves no team usable: judging without it
|
|
326
|
+
* would drop rules every team is meant to follow.
|
|
327
|
+
*/
|
|
328
|
+
export function teams(env = process.env) {
|
|
329
|
+
const dir = rulesDir(env);
|
|
330
|
+
const good = [];
|
|
331
|
+
const bad = [];
|
|
332
|
+
let corporate = null;
|
|
333
|
+
if (!existsSync(dir)) return { good, bad, dir, corporate };
|
|
334
|
+
const files = readdirSync(dir)
|
|
335
|
+
.filter((f) => f.endsWith(".md"))
|
|
336
|
+
.sort();
|
|
337
|
+
if (files.includes(CORPORATE_FILE)) {
|
|
338
|
+
try {
|
|
339
|
+
corporate = parseCorporateFile(join(dir, CORPORATE_FILE));
|
|
340
|
+
} catch (e) {
|
|
341
|
+
bad.push({ file: CORPORATE_FILE, error: e.message });
|
|
342
|
+
for (const f of files.filter((x) => x !== CORPORATE_FILE)) {
|
|
343
|
+
bad.push({ file: f, error: `${CORPORATE_FILE} does not parse` });
|
|
344
|
+
}
|
|
345
|
+
return { good, bad, dir, corporate };
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
for (const f of files.filter((x) => x !== CORPORATE_FILE)) {
|
|
349
|
+
try {
|
|
350
|
+
good.push(withBaseline(readTeamFile(join(dir, f)), corporate));
|
|
351
|
+
} catch (e) {
|
|
352
|
+
bad.push({ file: f, error: e.message });
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return { good, bad, dir, corporate: corporate && corporate.path };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function loadTeam(slug, env, t) {
|
|
359
|
+
const { good, dir } = teams(env);
|
|
360
|
+
const team = good.find((d) => d.slug === slug);
|
|
361
|
+
if (!team) throw new Refusal(t.noRules(slug, dir));
|
|
362
|
+
return team;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function buildJql(team, presetId, days, extra, t = TEXT.en) {
|
|
366
|
+
const sel = team.selection;
|
|
367
|
+
const id = presetId || sel.default;
|
|
368
|
+
const preset = sel.presets.find((p) => p.id === id);
|
|
369
|
+
if (!preset) {
|
|
370
|
+
throw new Refusal(
|
|
371
|
+
t.noPreset(
|
|
372
|
+
id,
|
|
373
|
+
sel.presets.map((p) => p.id),
|
|
374
|
+
),
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
const parts = [sel.base, `(${preset.jql})`];
|
|
378
|
+
if (days) parts.push(`created >= -${Number.parseInt(days, 10)}d`);
|
|
379
|
+
if (extra) parts.push(`(${extra})`);
|
|
380
|
+
return { jql: `${parts.join(" AND ")} ORDER BY ${sel.order || "created DESC"}`, preset };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ---------------------------------------------------------------- Jira
|
|
384
|
+
|
|
385
|
+
function jiraHost(env, t) {
|
|
386
|
+
const h = globalPref("hosts.jira", null, env);
|
|
387
|
+
if (typeof h !== "string" || !h.trim()) throw new Refusal(t.noHost(hostCommand("setup")));
|
|
388
|
+
const host = h.trim().replace(/\/+$/, "");
|
|
389
|
+
return /^https?:\/\//.test(host) ? host : `https://${host}`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function jiraClient(env, t) {
|
|
393
|
+
const host = jiraHost(env, t);
|
|
394
|
+
let token = null;
|
|
395
|
+
return async function request(path, method = "GET", body = undefined) {
|
|
396
|
+
if (!token) token = await resolveTokenAsync("jira", "JIRA_TOKEN", { env });
|
|
397
|
+
if (!token) throw new Refusal(t.noToken(hostCommand("setup")));
|
|
398
|
+
const headers = { Authorization: `Bearer ${token}`, Accept: "application/json" };
|
|
399
|
+
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
400
|
+
const res = await fetch(host + path, {
|
|
401
|
+
method,
|
|
402
|
+
headers,
|
|
403
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
404
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
405
|
+
});
|
|
406
|
+
const raw = await res.text();
|
|
407
|
+
let data;
|
|
408
|
+
try {
|
|
409
|
+
data = raw ? JSON.parse(raw) : null;
|
|
410
|
+
} catch {
|
|
411
|
+
data = raw.slice(0, 300);
|
|
412
|
+
}
|
|
413
|
+
return { status: res.status, data, host };
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async function search(request, jql, fields, limit, t) {
|
|
418
|
+
const issues = [];
|
|
419
|
+
let start = 0;
|
|
420
|
+
let total = 0;
|
|
421
|
+
for (;;) {
|
|
422
|
+
const size = limit == null ? PAGE : Math.min(PAGE, limit - issues.length);
|
|
423
|
+
if (size <= 0) break;
|
|
424
|
+
const q = new URLSearchParams({
|
|
425
|
+
jql,
|
|
426
|
+
startAt: String(start),
|
|
427
|
+
maxResults: String(size),
|
|
428
|
+
fields: fields.join(","),
|
|
429
|
+
});
|
|
430
|
+
const { status, data } = await request(`/rest/api/2/search?${q}`);
|
|
431
|
+
if (status !== 200) throw new Refusal(t.jqlFailed(status));
|
|
432
|
+
total = data.total;
|
|
433
|
+
issues.push(...data.issues);
|
|
434
|
+
start += data.issues.length;
|
|
435
|
+
if (!data.issues.length || start >= total) break;
|
|
436
|
+
}
|
|
437
|
+
return { issues, total };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function plain(v) {
|
|
441
|
+
if (v == null) return null;
|
|
442
|
+
if (Array.isArray(v)) {
|
|
443
|
+
const out = v.map(plain);
|
|
444
|
+
return out.length ? out : null;
|
|
445
|
+
}
|
|
446
|
+
if (typeof v === "object") return v.value ?? v.name ?? v.displayName ?? v.key ?? null;
|
|
447
|
+
return v;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
export function stripHtml(s) {
|
|
451
|
+
const decoded = String(s || "")
|
|
452
|
+
.replace(/<br\s*\/?>|<\/p>|<\/li>|<\/tr>/gi, "\n")
|
|
453
|
+
.replace(/<[^>]+>/g, " ")
|
|
454
|
+
.replace(/ /g, " ")
|
|
455
|
+
.replace(/</g, "<")
|
|
456
|
+
.replace(/>/g, ">")
|
|
457
|
+
.replace(/"/g, '"')
|
|
458
|
+
.replace(/'/g, "'")
|
|
459
|
+
.replace(/&/g, "&");
|
|
460
|
+
return decoded
|
|
461
|
+
.replace(/[ \t\r\f\v]+/g, " ")
|
|
462
|
+
.replace(/ *\n */g, "\n")
|
|
463
|
+
.replace(/\n{2,}/g, "\n")
|
|
464
|
+
.trim();
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function toBug(issue, evidenceFields) {
|
|
468
|
+
const f = issue.fields;
|
|
469
|
+
const comments = f.comment?.comments ?? [];
|
|
470
|
+
return {
|
|
471
|
+
key: issue.key,
|
|
472
|
+
summary: f.summary ?? null,
|
|
473
|
+
priority: plain(f.priority),
|
|
474
|
+
status: plain(f.status),
|
|
475
|
+
created: String(f.created || "").slice(0, 10),
|
|
476
|
+
project: plain(f.project),
|
|
477
|
+
components: plain(f.components) ?? [],
|
|
478
|
+
labels: f.labels ?? [],
|
|
479
|
+
fields: Object.fromEntries(
|
|
480
|
+
Object.entries(evidenceFields).map(([id, label]) => [label, plain(f[id])]),
|
|
481
|
+
),
|
|
482
|
+
description: stripHtml(f.description).slice(0, TEXT_LIMIT),
|
|
483
|
+
comments: comments
|
|
484
|
+
.slice(-COMMENT_LIMIT)
|
|
485
|
+
.map((c) => stripHtml(c.body).slice(0, COMMENT_TEXT_LIMIT)),
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// ---------------------------------------------------------------- evaluations
|
|
490
|
+
|
|
491
|
+
const rank = (p) => (SCALE.includes(p) ? SCALE.indexOf(p) : null);
|
|
492
|
+
|
|
493
|
+
/** The verdict a current and a proposed priority imply, or null off the scale. */
|
|
494
|
+
export function expectedVerdict(current, proposed) {
|
|
495
|
+
const rc = rank(current);
|
|
496
|
+
const rp = rank(proposed);
|
|
497
|
+
if (rc == null || rp == null) return null;
|
|
498
|
+
if (rc === rp) return "correct";
|
|
499
|
+
return rc < rp ? "too_high" : "too_low";
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/** Errors in an evaluations list against the fetched bugs and the team's rule ids. */
|
|
503
|
+
export function validate(team, bugs, evals) {
|
|
504
|
+
const errors = [];
|
|
505
|
+
const byKey = new Map();
|
|
506
|
+
for (const e of evals) {
|
|
507
|
+
if (byKey.has(e.key)) errors.push(`${e.key}: evaluated more than once`);
|
|
508
|
+
byKey.set(e.key, e);
|
|
509
|
+
}
|
|
510
|
+
const known = new Set(team.rules);
|
|
511
|
+
for (const b of bugs) {
|
|
512
|
+
const e = byKey.get(b.key);
|
|
513
|
+
if (!e) {
|
|
514
|
+
errors.push(`${b.key}: no evaluation`);
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
const v = e.verdict;
|
|
518
|
+
if (!VERDICT_IDS.includes(v)) errors.push(`${b.key}: unknown verdict ${JSON.stringify(v)}`);
|
|
519
|
+
const rules = Array.isArray(e.rules) ? e.rules : [];
|
|
520
|
+
if (!rules.length) errors.push(`${b.key}: no rule id`);
|
|
521
|
+
const unknown = rules.filter((r) => !known.has(r));
|
|
522
|
+
if (unknown.length) errors.push(`${b.key}: unknown rule ${unknown.join(", ")}`);
|
|
523
|
+
if (!CONFIDENCE_IDS.includes(confidenceId(e.confidence))) {
|
|
524
|
+
errors.push(`${b.key}: confidence must be one of ${CONFIDENCE_IDS.join(", ")}`);
|
|
525
|
+
}
|
|
526
|
+
if (v === "insufficient" && !(e.missing && e.missing.length)) {
|
|
527
|
+
errors.push(`${b.key}: insufficient with an empty missing list`);
|
|
528
|
+
}
|
|
529
|
+
if (["correct", "too_high", "too_low"].includes(v)) {
|
|
530
|
+
const want = expectedVerdict(b.priority, e.proposed);
|
|
531
|
+
if (want == null) {
|
|
532
|
+
errors.push(
|
|
533
|
+
`${b.key}: priority ${b.priority} or proposed ${e.proposed} is off the scale; the verdict must be insufficient`,
|
|
534
|
+
);
|
|
535
|
+
} else if (want !== v) {
|
|
536
|
+
errors.push(`${b.key}: ${b.priority} -> ${e.proposed} is ${want}, not ${v}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (!(e.evidence && e.evidence.length) && v !== "insufficient") {
|
|
540
|
+
errors.push(`${b.key}: no evidence`);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const fetched = new Set(bugs.map((b) => b.key));
|
|
544
|
+
const extra = [...byKey.keys()].filter((k) => !fetched.has(k)).sort();
|
|
545
|
+
if (extra.length) errors.push(`evaluations for issues not in the bugs file: ${extra.join(", ")}`);
|
|
546
|
+
return { errors, byKey };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const cell = (s) =>
|
|
550
|
+
String(s === null || s === undefined || s === "" ? "-" : s)
|
|
551
|
+
.replace(/\|/g, "/")
|
|
552
|
+
.replace(/\n/g, " ");
|
|
553
|
+
|
|
554
|
+
/** The report as markdown lines. */
|
|
555
|
+
export function renderReport({ team, snap, byKey, host, t }) {
|
|
556
|
+
const bugs = snap.bugs;
|
|
557
|
+
const count = (fn) => {
|
|
558
|
+
const m = new Map();
|
|
559
|
+
for (const b of bugs) m.set(fn(b), (m.get(fn(b)) || 0) + 1);
|
|
560
|
+
return m;
|
|
561
|
+
};
|
|
562
|
+
const verdictN = count((b) => byKey.get(b.key).verdict);
|
|
563
|
+
const curN = count((b) => b.priority || "-");
|
|
564
|
+
const propN = count((b) => byKey.get(b.key).proposed || "-");
|
|
565
|
+
const link = (k) => `[${k}](${host}/browse/${k})`;
|
|
566
|
+
const h = t.head;
|
|
567
|
+
const L = [];
|
|
568
|
+
L.push(t.title(team.name, snap.date), "");
|
|
569
|
+
L.push(`- **${t.team}:** ${team.name} (\`${team.slug}\`)`);
|
|
570
|
+
L.push(`- **${t.scope}:** ${snap.presetLabel}`);
|
|
571
|
+
L.push(`- **JQL:** \`${snap.jql}\``);
|
|
572
|
+
L.push(`- **${t.recordsLabel}:** ${t.records(snap.total, bugs.length)}`);
|
|
573
|
+
const sourceFiles = [team.path, ...(team.corporate ? [`${team.corporate} (${t.baseline})`] : [])];
|
|
574
|
+
L.push(`- **${t.source}:** ${sourceFiles.join(", ")} (${t.rules(team.rules.length)})`);
|
|
575
|
+
if (team.overrides?.length) L.push(`- ${t.overrides(team.overrides)}`);
|
|
576
|
+
L.push(`- ${t.readOnly}`, "");
|
|
577
|
+
L.push(`## ${t.summary}`, "", `| ${t.result} | ${t.count} |`, "|---|---|");
|
|
578
|
+
for (const v of VERDICT_IDS) L.push(`| ${t.verdicts[v]} | ${verdictN.get(v) || 0} |`);
|
|
579
|
+
L.push("", `| ${h.priority} | ${t.current} | ${t.proposed} |`, "|---|---|---|");
|
|
580
|
+
const offScale = [...new Set([...curN.keys(), ...propN.keys()])]
|
|
581
|
+
.filter((p) => !SCALE.includes(p))
|
|
582
|
+
.sort();
|
|
583
|
+
for (const p of [...SCALE, ...offScale]) {
|
|
584
|
+
L.push(`| ${p} | ${curN.get(p) || 0} | ${propN.get(p) || 0} |`);
|
|
585
|
+
}
|
|
586
|
+
L.push("");
|
|
587
|
+
|
|
588
|
+
const order = (b) => rank(b.priority) ?? 9;
|
|
589
|
+
const mism = bugs
|
|
590
|
+
.filter((b) => ["too_high", "too_low"].includes(byKey.get(b.key).verdict))
|
|
591
|
+
.sort((a, b) => order(a) - order(b) || a.key.localeCompare(b.key));
|
|
592
|
+
L.push(`## ${t.mismatches} (${mism.length})`, "");
|
|
593
|
+
if (mism.length) {
|
|
594
|
+
L.push(
|
|
595
|
+
`| ${h.issue} | ${h.summary} | ${t.current} | ${t.proposed} | ${h.verdict} | ${h.rules} | ${h.confidence} | ${h.reason} |`,
|
|
596
|
+
"|---|---|---|---|---|---|---|---|",
|
|
597
|
+
);
|
|
598
|
+
for (const b of mism) {
|
|
599
|
+
const e = byKey.get(b.key);
|
|
600
|
+
L.push(
|
|
601
|
+
`| ${link(b.key)} | ${cell(b.summary)} | ${cell(b.priority)} | ${cell(e.proposed)} | ${t.verdicts[e.verdict]} | ${e.rules.join(", ")} | ${t.confidence[confidenceId(e.confidence)]} | ${cell(e.reason)} |`,
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
L.push("", `### ${t.evidence}`, "");
|
|
605
|
+
for (const b of mism) {
|
|
606
|
+
L.push(`- **${b.key}**: ${(byKey.get(b.key).evidence || []).map(cell).join("; ")}`);
|
|
607
|
+
}
|
|
608
|
+
} else {
|
|
609
|
+
L.push(t.none);
|
|
610
|
+
}
|
|
611
|
+
L.push("");
|
|
612
|
+
|
|
613
|
+
const ins = bugs.filter((b) => byKey.get(b.key).verdict === "insufficient");
|
|
614
|
+
L.push(`## ${t.insufficient} (${ins.length})`, "");
|
|
615
|
+
if (ins.length) {
|
|
616
|
+
L.push(
|
|
617
|
+
`| ${h.issue} | ${h.summary} | ${t.current} | ${h.guess} | ${h.missing} | ${h.rules} |`,
|
|
618
|
+
"|---|---|---|---|---|---|",
|
|
619
|
+
);
|
|
620
|
+
for (const b of ins) {
|
|
621
|
+
const e = byKey.get(b.key);
|
|
622
|
+
L.push(
|
|
623
|
+
`| ${link(b.key)} | ${cell(b.summary)} | ${cell(b.priority)} | ${cell(e.proposed)} | ${cell((e.missing || []).join(", "))} | ${e.rules.join(", ")} |`,
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
} else {
|
|
627
|
+
L.push(t.none);
|
|
628
|
+
}
|
|
629
|
+
L.push("");
|
|
630
|
+
|
|
631
|
+
const ok = bugs.filter((b) => byKey.get(b.key).verdict === "correct");
|
|
632
|
+
L.push(`## ${t.correct} (${ok.length})`, "");
|
|
633
|
+
if (ok.length) {
|
|
634
|
+
L.push(
|
|
635
|
+
`<details><summary>${t.open}</summary>`,
|
|
636
|
+
"",
|
|
637
|
+
`| ${h.issue} | ${h.summary} | ${h.priority} | ${h.rules} |`,
|
|
638
|
+
"|---|---|---|---|",
|
|
639
|
+
);
|
|
640
|
+
for (const b of ok) {
|
|
641
|
+
L.push(
|
|
642
|
+
`| ${link(b.key)} | ${cell(b.summary)} | ${cell(b.priority)} | ${byKey.get(b.key).rules.join(", ")} |`,
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
L.push("", "</details>");
|
|
646
|
+
} else {
|
|
647
|
+
L.push(t.none);
|
|
648
|
+
}
|
|
649
|
+
L.push("");
|
|
650
|
+
|
|
651
|
+
const cited = new Set(bugs.flatMap((b) => byKey.get(b.key).rules || []));
|
|
652
|
+
const bySource = (team.ruleSet || []).filter((r) => cited.has(r.id));
|
|
653
|
+
if (bySource.length) {
|
|
654
|
+
L.push(`## ${t.sources}`, "", `| ${h.rules} | ${t.file} |`, "|---|---|");
|
|
655
|
+
for (const r of bySource) L.push(`| ${r.id} | ${r.source} |`);
|
|
656
|
+
L.push("");
|
|
657
|
+
}
|
|
658
|
+
return { lines: L, verdictN, mism };
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// ---------------------------------------------------------------- one approved change
|
|
662
|
+
|
|
663
|
+
/** Ties an approval to the issue's state when it was proposed. */
|
|
664
|
+
export function opToken(key, from, to, updated) {
|
|
665
|
+
return createHash("sha256")
|
|
666
|
+
.update(`${key}|priority|${from}|${to}|${updated}`)
|
|
667
|
+
.digest("hex")
|
|
668
|
+
.slice(0, 12);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function refuseUnattended(env, t) {
|
|
672
|
+
if (env.MULTI_AGENT_UNATTENDED === "1") throw new Refusal(t.unattended);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
async function live(request, key, t) {
|
|
676
|
+
const { status, data } = await request(
|
|
677
|
+
`/rest/api/2/issue/${encodeURIComponent(key)}?fields=priority,summary,updated`,
|
|
678
|
+
);
|
|
679
|
+
if (status !== 200) throw new Refusal(t.unreadable(key, status));
|
|
680
|
+
const f = data.fields;
|
|
681
|
+
return { priority: plain(f.priority), summary: f.summary, updated: f.updated };
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// ---------------------------------------------------------------- CLI
|
|
685
|
+
|
|
686
|
+
const SUBCOMMANDS = ["paths", "teams", "rules", "presets", "fetch", "report", "propose", "apply"];
|
|
687
|
+
|
|
688
|
+
function writeFile(file, text) {
|
|
689
|
+
mkdirSync(dirname(resolve(file)), { recursive: true });
|
|
690
|
+
writeFileSync(file, text);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function need(flags, names) {
|
|
694
|
+
for (const n of names) {
|
|
695
|
+
if (flags[n] === undefined || flags[n] === true || flags[n] === "") {
|
|
696
|
+
throw new UsageError(`--${n} is required`);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
async function run(sub, flags, env, out, t) {
|
|
702
|
+
if (sub === "paths") {
|
|
703
|
+
const p = { rulesDir: rulesDir(env), runsDir: runsDir(env) };
|
|
704
|
+
out(flags.json ? JSON.stringify(p, null, 2) : `${p.rulesDir}\n${p.runsDir}`);
|
|
705
|
+
return 0;
|
|
706
|
+
}
|
|
707
|
+
if (sub === "teams") {
|
|
708
|
+
const { good, bad, dir, corporate } = teams(env);
|
|
709
|
+
const rows = good.map((d) => ({
|
|
710
|
+
slug: d.slug,
|
|
711
|
+
name: d.name,
|
|
712
|
+
rules: d.rules.length,
|
|
713
|
+
overrides: d.overrides,
|
|
714
|
+
default: d.selection.default,
|
|
715
|
+
}));
|
|
716
|
+
if (flags.json) {
|
|
717
|
+
out(JSON.stringify({ rulesDir: dir, corporate, teams: rows, skipped: bad }, null, 2));
|
|
718
|
+
} else {
|
|
719
|
+
if (corporate) out(`${CORPORATE_FILE} ${corporate}`);
|
|
720
|
+
for (const r of rows) out(`${r.slug}\t${r.name}\t${t.rules(r.rules)}\t${r.default}`);
|
|
721
|
+
for (const b of bad) out(`${t.skipped} ${b.file}: ${b.error}`);
|
|
722
|
+
}
|
|
723
|
+
return 0;
|
|
724
|
+
}
|
|
725
|
+
if (sub === "rules") {
|
|
726
|
+
need(flags, ["team"]);
|
|
727
|
+
const team = loadTeam(flags.team, env, t);
|
|
728
|
+
if (flags.json) {
|
|
729
|
+
out(
|
|
730
|
+
JSON.stringify(
|
|
731
|
+
{
|
|
732
|
+
team: team.slug,
|
|
733
|
+
corporate: team.corporate,
|
|
734
|
+
overrides: team.overrides,
|
|
735
|
+
rules: team.ruleSet,
|
|
736
|
+
},
|
|
737
|
+
null,
|
|
738
|
+
2,
|
|
739
|
+
),
|
|
740
|
+
);
|
|
741
|
+
} else {
|
|
742
|
+
for (const r of team.ruleSet) out(`${r.source}\t${r.row}`);
|
|
743
|
+
}
|
|
744
|
+
return 0;
|
|
745
|
+
}
|
|
746
|
+
if (sub === "presets") {
|
|
747
|
+
need(flags, ["team"]);
|
|
748
|
+
const team = loadTeam(flags.team, env, t);
|
|
749
|
+
const rows = team.selection.presets.map((p) => ({
|
|
750
|
+
id: p.id,
|
|
751
|
+
label: p.label || p.id,
|
|
752
|
+
jql: p.jql,
|
|
753
|
+
default: p.id === team.selection.default,
|
|
754
|
+
}));
|
|
755
|
+
out(
|
|
756
|
+
flags.json
|
|
757
|
+
? JSON.stringify(rows, null, 2)
|
|
758
|
+
: rows.map((r) => `${r.id}\t${r.label}`).join("\n"),
|
|
759
|
+
);
|
|
760
|
+
return 0;
|
|
761
|
+
}
|
|
762
|
+
if (sub === "fetch") {
|
|
763
|
+
need(flags, ["team", "out"]);
|
|
764
|
+
const team = loadTeam(flags.team, env, t);
|
|
765
|
+
const { jql, preset } = buildJql(team, flags.preset, flags.days, flags.jql, t);
|
|
766
|
+
const ev = team.selection.evidenceFields || {};
|
|
767
|
+
const max = flags.max === undefined ? 50 : Number.parseInt(flags.max, 10);
|
|
768
|
+
if (!Number.isInteger(max) || max < 0) throw new UsageError("--max needs a whole number");
|
|
769
|
+
const request = jiraClient(env, t);
|
|
770
|
+
const { issues, total } = await search(
|
|
771
|
+
request,
|
|
772
|
+
jql,
|
|
773
|
+
[...BASE_FIELDS, ...Object.keys(ev)],
|
|
774
|
+
max === 0 ? null : max,
|
|
775
|
+
t,
|
|
776
|
+
);
|
|
777
|
+
const bugs = issues.map((i) => toBug(i, ev));
|
|
778
|
+
const snap = {
|
|
779
|
+
team: team.slug,
|
|
780
|
+
name: team.name,
|
|
781
|
+
preset: preset.id,
|
|
782
|
+
presetLabel: preset.label || preset.id,
|
|
783
|
+
jql,
|
|
784
|
+
total,
|
|
785
|
+
fetched: bugs.length,
|
|
786
|
+
date: new Date().toISOString().slice(0, 10),
|
|
787
|
+
bugs,
|
|
788
|
+
};
|
|
789
|
+
writeFile(flags.out, JSON.stringify(snap, null, 2) + "\n");
|
|
790
|
+
const byPriority = new Map();
|
|
791
|
+
for (const b of bugs) byPriority.set(b.priority, (byPriority.get(b.priority) || 0) + 1);
|
|
792
|
+
out(`JQL: ${jql}`);
|
|
793
|
+
out(t.fetched(total, bugs.length, flags.out));
|
|
794
|
+
out(
|
|
795
|
+
`priority: ${[...byPriority]
|
|
796
|
+
.sort((a, b) => b[1] - a[1])
|
|
797
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
798
|
+
.join(", ")}`,
|
|
799
|
+
);
|
|
800
|
+
return 0;
|
|
801
|
+
}
|
|
802
|
+
if (sub === "report") {
|
|
803
|
+
need(flags, ["team", "bugs", "evals", "out"]);
|
|
804
|
+
const team = loadTeam(flags.team, env, t);
|
|
805
|
+
const snap = JSON.parse(readFileSync(flags.bugs, "utf8"));
|
|
806
|
+
const snapTeam = snap.team ?? snap.directorate;
|
|
807
|
+
if (snapTeam !== team.slug) throw new Refusal(t.wrongTeam(snapTeam, team.slug));
|
|
808
|
+
let evals = JSON.parse(readFileSync(flags.evals, "utf8"));
|
|
809
|
+
if (!Array.isArray(evals)) evals = evals.evals || [];
|
|
810
|
+
const { errors, byKey } = validate(team, snap.bugs, evals);
|
|
811
|
+
if (errors.length) {
|
|
812
|
+
throw new Refusal([t.invalidEvals(errors.length), ...errors.slice(0, 40)].join("\n "));
|
|
813
|
+
}
|
|
814
|
+
const host = jiraHost(env, t);
|
|
815
|
+
const { lines, verdictN, mism } = renderReport({ team, snap, byKey, host, t });
|
|
816
|
+
writeFile(flags.out, lines.join("\n"));
|
|
817
|
+
out(t.report(flags.out));
|
|
818
|
+
out(VERDICT_IDS.map((v) => `${t.verdicts[v]}: ${verdictN.get(v) || 0}`).join(" | "));
|
|
819
|
+
for (const b of mism.slice(0, 5)) {
|
|
820
|
+
const e = byKey.get(b.key);
|
|
821
|
+
out(` ${b.key}: ${b.priority} -> ${e.proposed} (${e.rules.join(", ")})`);
|
|
822
|
+
}
|
|
823
|
+
if (mism.length > 5) out(t.more(mism.length - 5));
|
|
824
|
+
return 0;
|
|
825
|
+
}
|
|
826
|
+
if (sub === "propose" || sub === "apply") {
|
|
827
|
+
refuseUnattended(env, t);
|
|
828
|
+
need(flags, sub === "propose" ? ["issue", "to", "rule", "reason"] : ["issue", "to", "confirm"]);
|
|
829
|
+
if (!SCALE.includes(flags.to)) throw new Refusal(t.badTarget);
|
|
830
|
+
const request = jiraClient(env, t);
|
|
831
|
+
const now = await live(request, flags.issue, t);
|
|
832
|
+
const token = opToken(flags.issue, now.priority, flags.to, now.updated);
|
|
833
|
+
if (sub === "propose") {
|
|
834
|
+
if (now.priority === flags.to) throw new Refusal(t.already(flags.issue, flags.to));
|
|
835
|
+
out(t.pending);
|
|
836
|
+
out(` issue : ${flags.issue} - ${now.summary}`);
|
|
837
|
+
out(" field : priority");
|
|
838
|
+
out(` change : ${now.priority} -> ${flags.to}`);
|
|
839
|
+
out(` rules : ${flags.rule}`);
|
|
840
|
+
out(` reason : ${flags.reason}`);
|
|
841
|
+
out(` token : ${token}`);
|
|
842
|
+
return 0;
|
|
843
|
+
}
|
|
844
|
+
if (flags.confirm !== token) throw new Refusal(t.tokenMismatch);
|
|
845
|
+
const { status } = await request(
|
|
846
|
+
`/rest/api/2/issue/${encodeURIComponent(flags.issue)}`,
|
|
847
|
+
"PUT",
|
|
848
|
+
{
|
|
849
|
+
fields: { priority: { name: flags.to } },
|
|
850
|
+
},
|
|
851
|
+
);
|
|
852
|
+
if (status !== 200 && status !== 204) throw new Refusal(t.writeFailed(status));
|
|
853
|
+
const after = await live(request, flags.issue, t);
|
|
854
|
+
out(`${flags.issue}: priority ${now.priority} -> ${after.priority}`);
|
|
855
|
+
return 0;
|
|
856
|
+
}
|
|
857
|
+
throw new UsageError(`unknown subcommand ${sub}; one of ${SUBCOMMANDS.join(", ")}`);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
export async function main(argv = process.argv.slice(2), env = process.env) {
|
|
861
|
+
const t = messages(TEXT, outputLanguage());
|
|
862
|
+
const out = (s) => process.stdout.write(`${s}\n`);
|
|
863
|
+
const [sub, ...rest] = argv;
|
|
864
|
+
if (!sub || sub === "-h" || sub === "--help") {
|
|
865
|
+
const lines = readFileSync(new URL(import.meta.url), "utf8").split("\n");
|
|
866
|
+
const start = lines.findIndex((l) => l.includes("Usage:"));
|
|
867
|
+
const end = lines.findIndex((l, i) => i > start && l.trim() === "*");
|
|
868
|
+
out(
|
|
869
|
+
lines
|
|
870
|
+
.slice(start, end)
|
|
871
|
+
.map((l) => l.replace(/^ \*\s?/, ""))
|
|
872
|
+
.join("\n"),
|
|
873
|
+
);
|
|
874
|
+
return sub ? 0 : 2;
|
|
875
|
+
}
|
|
876
|
+
try {
|
|
877
|
+
const { flags } = parseArgs(rest, {
|
|
878
|
+
booleans: ["json"],
|
|
879
|
+
strings: [
|
|
880
|
+
"team",
|
|
881
|
+
"preset",
|
|
882
|
+
"days",
|
|
883
|
+
"jql",
|
|
884
|
+
"max",
|
|
885
|
+
"out",
|
|
886
|
+
"bugs",
|
|
887
|
+
"evals",
|
|
888
|
+
"issue",
|
|
889
|
+
"to",
|
|
890
|
+
"rule",
|
|
891
|
+
"reason",
|
|
892
|
+
"confirm",
|
|
893
|
+
],
|
|
894
|
+
unknown: "error",
|
|
895
|
+
positionals: false,
|
|
896
|
+
});
|
|
897
|
+
return await run(sub, flags, env, out, t);
|
|
898
|
+
} catch (e) {
|
|
899
|
+
process.stderr.write(`priority-review: ${e.message}\n`);
|
|
900
|
+
return e instanceof ArgsError || e instanceof UsageError ? 2 : 1;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function invokedDirectly() {
|
|
905
|
+
try {
|
|
906
|
+
return import.meta.url === pathToFileURL(realpathSync(process.argv[1] ?? "")).href;
|
|
907
|
+
} catch {
|
|
908
|
+
return false;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
if (invokedDirectly()) process.exitCode = await main();
|