@trops/dash-check 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +309 -2
- package/cli/bin.js +12 -0
- package/cli/index.d.ts +7 -0
- package/cli/index.js +566 -0
- package/examples/changes-match-plan.bad.json +4 -0
- package/examples/changes-match-plan.check.js +10 -0
- package/examples/changes-match-plan.sample.json +4 -0
- package/examples/every-acceptance-has-a-test.bad.json +3 -0
- package/examples/every-acceptance-has-a-test.check.js +19 -0
- package/examples/every-acceptance-has-a-test.sample.json +3 -0
- package/examples/helpers/changelog.js +23 -0
- package/examples/jira-comment-from-job.bad.json +5 -0
- package/examples/jira-comment-from-job.check.js +10 -0
- package/examples/jira-comment-from-job.sample.json +5 -0
- package/examples/merge-outcome.bad.json +4 -0
- package/examples/merge-outcome.check.js +9 -0
- package/examples/merge-outcome.sample.json +4 -0
- package/examples/on-branch.bad.json +6 -0
- package/examples/on-branch.check.js +12 -0
- package/examples/on-branch.sample.json +6 -0
- package/examples/plan-complete.bad.json +5 -0
- package/examples/plan-complete.check.js +14 -0
- package/examples/plan-complete.sample.json +5 -0
- package/examples/pr-ci.bad.json +4 -0
- package/examples/pr-ci.check.js +12 -0
- package/examples/pr-ci.sample.json +4 -0
- package/examples/ready-for-review.bad.json +6 -0
- package/examples/ready-for-review.check.json +12 -0
- package/examples/ready-for-review.sample.json +6 -0
- package/examples/setup.bad.json +6 -0
- package/examples/setup.check.js +8 -0
- package/examples/setup.sample.json +6 -0
- package/examples/ticket-has-acceptance.bad.json +4 -0
- package/examples/ticket-has-acceptance.check.js +10 -0
- package/examples/ticket-has-acceptance.sample.json +4 -0
- package/examples/validate.bad.json +6 -0
- package/examples/validate.check.js +14 -0
- package/examples/validate.sample.json +6 -0
- package/examples/work-committed.bad.json +13 -0
- package/examples/work-committed.check.js +13 -0
- package/examples/work-committed.sample.json +5 -0
- package/examples/workspace-cleaned.bad.json +4 -0
- package/examples/workspace-cleaned.check.js +7 -0
- package/examples/workspace-cleaned.sample.json +4 -0
- package/helpers/commands.js +62 -0
- package/helpers/files.js +74 -0
- package/helpers/git.js +116 -0
- package/helpers/github.js +77 -0
- package/helpers/handoff.js +80 -0
- package/helpers/index.d.ts +15 -0
- package/helpers/index.js +36 -0
- package/helpers/node.js +106 -0
- package/helpers/plan.js +55 -0
- package/helpers/tracker.js +60 -0
- package/index.d.ts +325 -0
- package/index.js +86 -0
- package/lib/adapters/fs.js +85 -0
- package/lib/adapters/git.js +87 -0
- package/lib/adapters/github.js +151 -0
- package/lib/adapters/run.js +76 -0
- package/lib/adapters/tool.js +64 -0
- package/lib/context.js +102 -0
- package/lib/discover.js +102 -0
- package/lib/expect.js +238 -0
- package/lib/glob.js +89 -0
- package/lib/handoff.js +68 -0
- package/lib/helper.js +219 -0
- package/lib/register.js +35 -0
- package/lib/runner.js +214 -0
- package/lib/scope.js +107 -0
- package/lib/util.js +122 -0
- package/lib/values.js +138 -0
- package/package.json +59 -4
- package/recipe/index.d.ts +70 -0
- package/recipe/index.js +381 -0
- package/recipe/schema.json +55 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handoff helpers: what the bot's handoff (its last json block) says.
|
|
3
|
+
* Built only from Layer 0.
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { defineHelper, expect, fail, note } = require("..");
|
|
8
|
+
|
|
9
|
+
/** Fails (and says why) when the answer has no handoff. */
|
|
10
|
+
function needHandoff(ctx) {
|
|
11
|
+
const info = ctx.handoffInfo || {};
|
|
12
|
+
if (info.found) return true;
|
|
13
|
+
if (info.broken) fail(`The answer's last \`\`\`json block doesn't parse (${info.error}): end the answer with one JSON object`);
|
|
14
|
+
else if (info.list) fail('The handoff is a JSON list: it must be one JSON object, e.g. { "summary": "…" }');
|
|
15
|
+
else fail("The answer has no handoff: end it with a ```json block holding a JSON object");
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const has = defineHelper({
|
|
20
|
+
id: "handoff.has",
|
|
21
|
+
group: "Handoff",
|
|
22
|
+
label: "Handoff has keys",
|
|
23
|
+
description: "The bot's handoff includes these, not empty",
|
|
24
|
+
sentence: "the handoff has {keys}",
|
|
25
|
+
params: {
|
|
26
|
+
keys: { type: "list", label: "Keys", required: true, description: 'e.g. prUrl, or plan.cause to reach in' },
|
|
27
|
+
},
|
|
28
|
+
reads: ["handoff"],
|
|
29
|
+
run(ctx, { keys }) {
|
|
30
|
+
if (!needHandoff(ctx)) return;
|
|
31
|
+
for (const k of keys) {
|
|
32
|
+
expect(ctx.get(`handoff.${k}`), `handoff.${k}`).not.toBeEmpty(`The handoff has no ${k}`);
|
|
33
|
+
}
|
|
34
|
+
note(`The handoff has ${keys.join(", ")}`);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const status = defineHelper({
|
|
39
|
+
id: "handoff.status",
|
|
40
|
+
group: "Handoff",
|
|
41
|
+
label: "Handoff status is",
|
|
42
|
+
description: "The handoff's status (ok, none, ask or split)",
|
|
43
|
+
sentence: "the handoff's status is {is}",
|
|
44
|
+
params: {
|
|
45
|
+
is: { type: "select", label: "Status", options: ["ok", "none", "ask", "split"], default: "ok" },
|
|
46
|
+
},
|
|
47
|
+
reads: ["handoff"],
|
|
48
|
+
run(ctx, { is }) {
|
|
49
|
+
if (!needHandoff(ctx)) return;
|
|
50
|
+
const s = ctx.handoff.status;
|
|
51
|
+
expect(s, "handoff.status").toBe(is, `The handoff's status is "${s}", not "${is}"`);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const eachHas = defineHelper({
|
|
56
|
+
id: "handoff.eachHas",
|
|
57
|
+
group: "Handoff",
|
|
58
|
+
label: "Each item in a list has keys",
|
|
59
|
+
description: "Every entry of a handoff list has these, not empty",
|
|
60
|
+
sentence: "each of handoff.{list} has {keys}",
|
|
61
|
+
params: {
|
|
62
|
+
list: { type: "text", label: "List", required: true, description: "e.g. tests" },
|
|
63
|
+
keys: { type: "list", label: "Keys", required: true },
|
|
64
|
+
},
|
|
65
|
+
reads: ["handoff"],
|
|
66
|
+
run(ctx, { list, keys }) {
|
|
67
|
+
if (!needHandoff(ctx)) return;
|
|
68
|
+
const items = ctx.get(`handoff.${list}`);
|
|
69
|
+
if (!expect(items, `handoff.${list}`).toBeArray(`The handoff's ${list} isn't a list`)) return;
|
|
70
|
+
items.forEach((it, i) => {
|
|
71
|
+
for (const k of keys) {
|
|
72
|
+
const v = it && typeof it === "object" ? ctx.get(`handoff.${list}.${i}.${k}`) : undefined;
|
|
73
|
+
expect(v, `handoff.${list}[${i}].${k}`).not.toBeEmpty(`${list}[${i}] has no ${k}`);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
note(`${items.length} in ${list}, each with ${keys.join(", ")}`);
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
module.exports = { has, status, eachHas, needHandoff };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Types for @trops/dash-check/helpers: the starter helpers.
|
|
2
|
+
import type { Helper } from "..";
|
|
3
|
+
|
|
4
|
+
type H = Helper<Record<string, any>>;
|
|
5
|
+
|
|
6
|
+
export const handoff: { has: H; status: H; eachHas: H };
|
|
7
|
+
export const plan: { hasSections: H; acceptanceEachHasTest: H };
|
|
8
|
+
export const git: { onBranch: H; committedSince: H; clean: H; changedOnly: H; noChangesTo: H };
|
|
9
|
+
export const files: { exist: H; contain: H; noneMatch: H };
|
|
10
|
+
export const commands: { passes: H; outputMatches: H };
|
|
11
|
+
export const node: { testsPass: H; lintPasses: H; noNewDependencies: H };
|
|
12
|
+
export const github: { prOpen: H; ciPassed: H };
|
|
13
|
+
export const tracker: { ticketExists: H; ticketHasLabel: H };
|
|
14
|
+
/** Every starter helper. */
|
|
15
|
+
export const all: H[];
|
package/helpers/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @trops/dash-check/helpers: the starter helpers, each a few lines of
|
|
3
|
+
* Layer 0. Call one with the check's ctx and its params:
|
|
4
|
+
*
|
|
5
|
+
* const { git, node, files } = require("@trops/dash-check/helpers");
|
|
6
|
+
* await git.committedSince(ctx, { base: "main" });
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
const { registerHelpers } = require("..");
|
|
11
|
+
|
|
12
|
+
const groups = {
|
|
13
|
+
handoff: require("./handoff"),
|
|
14
|
+
plan: require("./plan"),
|
|
15
|
+
git: require("./git"),
|
|
16
|
+
files: require("./files"),
|
|
17
|
+
commands: require("./commands"),
|
|
18
|
+
node: require("./node"),
|
|
19
|
+
github: require("./github"),
|
|
20
|
+
tracker: require("./tracker"),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const exported = {};
|
|
24
|
+
const all = [];
|
|
25
|
+
for (const [name, mod] of Object.entries(groups)) {
|
|
26
|
+
exported[name] = {};
|
|
27
|
+
for (const [k, v] of Object.entries(mod)) {
|
|
28
|
+
if (v && v.isDashCheckHelper) {
|
|
29
|
+
exported[name][k] = v;
|
|
30
|
+
all.push(v);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
registerHelpers(all, { source: "@trops/dash-check" });
|
|
35
|
+
|
|
36
|
+
module.exports = { ...exported, all };
|
package/helpers/node.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node helpers: a Node repo's tests, lint and dependencies. Built only
|
|
3
|
+
* from Layer 0 (ctx.run, ctx.fs, ctx.git).
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { defineHelper, fail, note } = require("..");
|
|
8
|
+
const { runOrFail } = require("./commands");
|
|
9
|
+
const { baseRef } = require("./git");
|
|
10
|
+
|
|
11
|
+
/** How many tests passed, from common runners' summaries, or null. */
|
|
12
|
+
function testCount(out) {
|
|
13
|
+
const s = String(out || "");
|
|
14
|
+
const pats = [
|
|
15
|
+
/Tests:\s+(?:\d+ \w+, )*(\d+) passed/, // jest
|
|
16
|
+
/^# pass (\d+)/m, // node --test (TAP)
|
|
17
|
+
/ℹ pass (\d+)/, // node --test (spec)
|
|
18
|
+
/(\d+) passing/, // mocha
|
|
19
|
+
/Tests\s+(\d+) passed/, // vitest
|
|
20
|
+
];
|
|
21
|
+
for (const p of pats) {
|
|
22
|
+
const m = s.match(p);
|
|
23
|
+
if (m) return Number(m[1]);
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const testsPass = defineHelper({
|
|
29
|
+
id: "node.testsPass",
|
|
30
|
+
group: "Node",
|
|
31
|
+
label: "Tests pass",
|
|
32
|
+
description: "npm test, or a command",
|
|
33
|
+
sentence: "run {command}",
|
|
34
|
+
params: {
|
|
35
|
+
command: { type: "text", label: "Command", default: "npm test" },
|
|
36
|
+
timeoutSec: { type: "number", label: "Time limit (s)", default: 900 },
|
|
37
|
+
},
|
|
38
|
+
reads: ["run"],
|
|
39
|
+
async run(ctx, { command, timeoutSec }) {
|
|
40
|
+
const r = await runOrFail(ctx, command, timeoutSec, `${command}`);
|
|
41
|
+
if (r.code === 0 && !r.timedOut) {
|
|
42
|
+
const n = testCount(`${r.stdout}\n${r.stderr}`);
|
|
43
|
+
note(n === null ? `${command} passed` : `${n} test${n === 1 ? "" : "s"} passed`);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const lintPasses = defineHelper({
|
|
49
|
+
id: "node.lintPasses",
|
|
50
|
+
group: "Node",
|
|
51
|
+
label: "Lint passes",
|
|
52
|
+
description: "npm run lint, or a command",
|
|
53
|
+
sentence: "run {command}",
|
|
54
|
+
params: {
|
|
55
|
+
command: { type: "text", label: "Command", default: "npm run lint" },
|
|
56
|
+
timeoutSec: { type: "number", label: "Time limit (s)", default: 300 },
|
|
57
|
+
},
|
|
58
|
+
reads: ["run"],
|
|
59
|
+
async run(ctx, { command, timeoutSec }) {
|
|
60
|
+
const r = await runOrFail(ctx, command, timeoutSec);
|
|
61
|
+
if (r.code === 0 && !r.timedOut) note(`${command} passed`);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const DEP_KEYS = ["dependencies", "devDependencies", "optionalDependencies", "peerDependencies"];
|
|
66
|
+
|
|
67
|
+
const noNewDependencies = defineHelper({
|
|
68
|
+
id: "node.noNewDependencies",
|
|
69
|
+
group: "Node",
|
|
70
|
+
label: "No new dependencies",
|
|
71
|
+
description: "package.json adds none since the base",
|
|
72
|
+
sentence: "package.json adds no dependencies since {base}",
|
|
73
|
+
params: {
|
|
74
|
+
base: { type: "text", label: "Base branch", default: "{repo.baseBranch}" },
|
|
75
|
+
allow: { type: "list", label: "Allowed", description: "Packages that may be added" },
|
|
76
|
+
},
|
|
77
|
+
reads: ["git", "fs"],
|
|
78
|
+
async run(ctx, { base, allow }) {
|
|
79
|
+
const ref = await baseRef(ctx, base);
|
|
80
|
+
const before = await ctx.git.fileAt(ref, "package.json");
|
|
81
|
+
if (before === null) {
|
|
82
|
+
fail(`package.json isn't on ${ref}, so there's nothing to compare with`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (!(await ctx.fs.exists("package.json"))) {
|
|
86
|
+
fail("package.json isn't in the workspace");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
let old;
|
|
90
|
+
try {
|
|
91
|
+
old = JSON.parse(before);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
fail(`package.json on ${ref} isn't valid JSON (${e.message})`);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const now = await ctx.fs.readJson("package.json");
|
|
97
|
+
const names = (p) => new Set(DEP_KEYS.flatMap((k) => Object.keys((p && p[k]) || {})));
|
|
98
|
+
const had = names(old);
|
|
99
|
+
const added = [...names(now)].filter((n) => !had.has(n) && !allow.includes(n)).sort();
|
|
100
|
+
if (added.length) {
|
|
101
|
+
fail(`package.json adds ${added.length === 1 ? "a dependency" : "dependencies"}: remove ${added.length === 1 ? "it" : "them"}, or allow ${added.length === 1 ? "it" : "them"} on the check`, { at: added.join(", "), received: added });
|
|
102
|
+
} else note(`No new dependencies since ${ref}`);
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
module.exports = { testsPass, lintPasses, noNewDependencies, testCount };
|
package/helpers/plan.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan helpers: the plan a planning bot hands on. Built only from Layer 0.
|
|
3
|
+
*/
|
|
4
|
+
"use strict";
|
|
5
|
+
|
|
6
|
+
const { defineHelper, expect, note } = require("..");
|
|
7
|
+
const { needHandoff } = require("./handoff");
|
|
8
|
+
|
|
9
|
+
const hasSections = defineHelper({
|
|
10
|
+
id: "plan.hasSections",
|
|
11
|
+
group: "Handoff",
|
|
12
|
+
label: "The plan has sections",
|
|
13
|
+
description: "The handoff's plan has each section, not empty",
|
|
14
|
+
sentence: "handoff.{key} has {sections}",
|
|
15
|
+
params: {
|
|
16
|
+
key: { type: "text", label: "Plan key", default: "plan" },
|
|
17
|
+
sections: { type: "list", label: "Sections", default: ["cause", "fix", "test"] },
|
|
18
|
+
},
|
|
19
|
+
reads: ["handoff"],
|
|
20
|
+
run(ctx, { key, sections }) {
|
|
21
|
+
if (!needHandoff(ctx)) return;
|
|
22
|
+
for (const s of sections) {
|
|
23
|
+
expect(ctx.get(`handoff.${key}.${s}`), `handoff.${key}.${s}`).not.toBeEmpty(`The ${key} has no ${s}`);
|
|
24
|
+
}
|
|
25
|
+
note(`The ${key} has ${sections.join(", ")}`);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const acceptanceEachHasTest = defineHelper({
|
|
30
|
+
id: "plan.acceptanceEachHasTest",
|
|
31
|
+
group: "Handoff",
|
|
32
|
+
label: "Each acceptance item has a test",
|
|
33
|
+
description: "Plan: every item is tested",
|
|
34
|
+
sentence: "every item in handoff.{acceptance} has a test in handoff.{tests}",
|
|
35
|
+
params: {
|
|
36
|
+
acceptance: { type: "text", label: "Acceptance key", default: "acceptance" },
|
|
37
|
+
tests: { type: "text", label: "Tests key", default: "tests" },
|
|
38
|
+
},
|
|
39
|
+
reads: ["handoff"],
|
|
40
|
+
run(ctx, { acceptance, tests }) {
|
|
41
|
+
if (!needHandoff(ctx)) return;
|
|
42
|
+
const items = ctx.get(`handoff.${acceptance}`);
|
|
43
|
+
if (!expect(items, `handoff.${acceptance}`).toBeArray(`The handoff has no ${acceptance} list`)) return;
|
|
44
|
+
if (!expect(items, `handoff.${acceptance}`).not.toBeEmpty(`The handoff's ${acceptance} is empty`)) return;
|
|
45
|
+
const list = ctx.get(`handoff.${tests}`);
|
|
46
|
+
const all = Array.isArray(list) ? list : [];
|
|
47
|
+
for (const a of items) {
|
|
48
|
+
const t = all.find((x) => x && typeof x === "object" && x.acceptance === a);
|
|
49
|
+
expect(t && t.test, `the test for "${a}"`).toBeNonEmptyString(`No test for: ${a}`);
|
|
50
|
+
}
|
|
51
|
+
note(`${items.length} acceptance item${items.length === 1 ? "" : "s"}, each with a test`);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
module.exports = { hasSections, acceptanceEachHasTest };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracker helpers: the work item in its tracker, through ctx.tool (so they
|
|
3
|
+
* run only in Dash, which supplies the provider). Built only from Layer 0.
|
|
4
|
+
* The defaults are Jira's; for another tracker set provider, tool and idArg.
|
|
5
|
+
*/
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const { defineHelper, expect, fail, note } = require("..");
|
|
9
|
+
|
|
10
|
+
const TRACKER = {
|
|
11
|
+
provider: { type: "text", label: "Provider", default: "jira" },
|
|
12
|
+
tool: { type: "text", label: "Tool", default: "getJiraIssue" },
|
|
13
|
+
idArg: { type: "text", label: "Id argument", default: "issueIdOrKey" },
|
|
14
|
+
id: { type: "text", label: "Item id", default: "{item.id}" },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
async function fetchTicket(ctx, { provider, tool, idArg, id }) {
|
|
18
|
+
if (!id) {
|
|
19
|
+
fail("The job has no work item id: run it on a job with an item, or set the item id");
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const t = await ctx.tool(provider, tool, { [idArg]: id });
|
|
23
|
+
if (!t || (typeof t === "object" && !Object.keys(t).length)) {
|
|
24
|
+
fail(`${id} isn't in ${provider}: check the id`);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return t;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const ticketExists = defineHelper({
|
|
31
|
+
id: "tracker.ticketExists",
|
|
32
|
+
group: "Tracker",
|
|
33
|
+
label: "The ticket exists",
|
|
34
|
+
description: "The work item is in the tracker",
|
|
35
|
+
sentence: "{id} exists in {provider}",
|
|
36
|
+
params: TRACKER,
|
|
37
|
+
reads: ["tool", "item"],
|
|
38
|
+
async run(ctx, p) {
|
|
39
|
+
if (await fetchTicket(ctx, p)) note(`${p.id} is in ${p.provider}`);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const ticketHasLabel = defineHelper({
|
|
44
|
+
id: "tracker.ticketHasLabel",
|
|
45
|
+
group: "Tracker",
|
|
46
|
+
label: "The ticket has a label",
|
|
47
|
+
description: "The work item carries a label",
|
|
48
|
+
sentence: "{id} has the label {label}",
|
|
49
|
+
params: { label: { type: "text", label: "Label", required: true }, ...TRACKER },
|
|
50
|
+
reads: ["tool", "item"],
|
|
51
|
+
async run(ctx, p) {
|
|
52
|
+
const t = await fetchTicket(ctx, p);
|
|
53
|
+
if (!t) return;
|
|
54
|
+
const raw = (t.fields && t.fields.labels) || t.labels || [];
|
|
55
|
+
const labels = raw.map((l) => (l && typeof l === "object" ? l.name : l));
|
|
56
|
+
expect(labels, `${p.id}'s labels`).toContain(p.label, `${p.id} has no label "${p.label}": add it`);
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
module.exports = { ticketExists, ticketHasLabel };
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// Types for @trops/dash-check (Layer 0: the runner, expect, the context, defineHelper).
|
|
2
|
+
|
|
3
|
+
/** One thing that went wrong, said so it can be fixed. */
|
|
4
|
+
export interface Failure {
|
|
5
|
+
message: string;
|
|
6
|
+
/** Where (a file:line, a list of names), shown after the message. */
|
|
7
|
+
at?: string;
|
|
8
|
+
expected?: unknown;
|
|
9
|
+
received?: unknown;
|
|
10
|
+
matcher?: string;
|
|
11
|
+
label?: string;
|
|
12
|
+
/** It was a thrown error, not an expectation. */
|
|
13
|
+
error?: boolean;
|
|
14
|
+
/** The helper's own text, when a step's `message` replaced it. */
|
|
15
|
+
original?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CheckResult {
|
|
19
|
+
name: string;
|
|
20
|
+
pass: boolean;
|
|
21
|
+
skipped?: boolean;
|
|
22
|
+
reason: string;
|
|
23
|
+
failures: Failure[];
|
|
24
|
+
ms?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface RunResult {
|
|
28
|
+
pass: boolean;
|
|
29
|
+
reason: string;
|
|
30
|
+
checks: CheckResult[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ---- the context ----------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
export interface RunOutput {
|
|
36
|
+
code: number;
|
|
37
|
+
stdout: string;
|
|
38
|
+
stderr: string;
|
|
39
|
+
timedOut: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface Fs {
|
|
43
|
+
path(p: string): string;
|
|
44
|
+
exists(p: string): Promise<boolean>;
|
|
45
|
+
dirExists(p?: string): Promise<boolean>;
|
|
46
|
+
read(p: string): Promise<string>;
|
|
47
|
+
readJson<T = any>(p: string): Promise<T>;
|
|
48
|
+
/** Files matching a glob (or globs; "!glob" leaves out), relative and sorted. */
|
|
49
|
+
list(globs: string | string[]): Promise<string[]>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface Git {
|
|
53
|
+
branch(): Promise<string>;
|
|
54
|
+
uncommitted(): Promise<string[]>;
|
|
55
|
+
isClean(): Promise<boolean>;
|
|
56
|
+
commitsSince(ref: string): Promise<number>;
|
|
57
|
+
changedFiles(ref: string): Promise<string[]>;
|
|
58
|
+
lastCommitMessage(): Promise<string>;
|
|
59
|
+
remoteHas(branch: string, remote?: string): Promise<boolean>;
|
|
60
|
+
defaultBranch(): Promise<string | null>;
|
|
61
|
+
fileAt(ref: string, path: string): Promise<string | null>;
|
|
62
|
+
hasRef(ref: string): Promise<boolean>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface CheckRun {
|
|
66
|
+
name: string;
|
|
67
|
+
status: string;
|
|
68
|
+
conclusion: string | null;
|
|
69
|
+
url: string | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface WaitResult {
|
|
73
|
+
runs: CheckRun[];
|
|
74
|
+
failed: CheckRun[];
|
|
75
|
+
pending: CheckRun[];
|
|
76
|
+
passed: CheckRun[];
|
|
77
|
+
noChecks: boolean;
|
|
78
|
+
timedOut: boolean;
|
|
79
|
+
waitedSec: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface GitHub {
|
|
83
|
+
pr(urlOrNumber: string | number, opts?: { repo?: string }): Promise<any & { slug: string }>;
|
|
84
|
+
checksFor(sha: string, opts?: { repo?: string }): Promise<{ runs: CheckRun[]; source: "check-runs" | "actions" }>;
|
|
85
|
+
waitForChecks(sha: string, opts?: { timeoutSec?: number; noChecksAfterSec?: number; pollSec?: number; repo?: string }): Promise<WaitResult>;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface Workspace {
|
|
89
|
+
dir: string;
|
|
90
|
+
branch?: string;
|
|
91
|
+
file(p: string): string;
|
|
92
|
+
exists(p: string): Promise<boolean>;
|
|
93
|
+
read(p: string): Promise<string>;
|
|
94
|
+
dirExists(): Promise<boolean>;
|
|
95
|
+
[key: string]: any;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface Context {
|
|
99
|
+
/** The raw check input (Dash's $DASH_INPUT). */
|
|
100
|
+
input: Record<string, any>;
|
|
101
|
+
/** The bot's whole answer. */
|
|
102
|
+
answer: string;
|
|
103
|
+
/** The answer's last json block, parsed; {} when there's none. */
|
|
104
|
+
handoff: Record<string, any>;
|
|
105
|
+
handoffInfo: { found: boolean; broken?: boolean; error?: string; list?: boolean; given?: boolean };
|
|
106
|
+
item: Record<string, any>;
|
|
107
|
+
picks: Record<string, any>;
|
|
108
|
+
row: Record<string, any>;
|
|
109
|
+
settings: Record<string, any>;
|
|
110
|
+
step: Record<string, any>;
|
|
111
|
+
job: Record<string, any>;
|
|
112
|
+
workspace: Workspace;
|
|
113
|
+
env: Record<string, string | undefined>;
|
|
114
|
+
/** Where fs, run and git work. */
|
|
115
|
+
cwd: string;
|
|
116
|
+
fs: Fs;
|
|
117
|
+
run(cmd: string, opts?: { timeoutSec?: number; env?: Record<string, string>; cwd?: string }): Promise<RunOutput>;
|
|
118
|
+
git: Git;
|
|
119
|
+
github: GitHub;
|
|
120
|
+
/** A read-only provider call; only Dash can answer it (DASH_TOOL_SOCKET). */
|
|
121
|
+
tool(provider: string, name: string, args?: Record<string, any>): Promise<any>;
|
|
122
|
+
/** A value by path: "handoff.files", "repo.baseBranch". */
|
|
123
|
+
get(path: string): any;
|
|
124
|
+
/** Fill "{placeholders}" from the context. */
|
|
125
|
+
fill<T = any>(value: T, opts?: FillOptions): any;
|
|
126
|
+
/** Is a `when` true? A path or a {placeholder}; "!" turns it around. */
|
|
127
|
+
when(expr: string | boolean | undefined): boolean;
|
|
128
|
+
/** Say how the running check passed. */
|
|
129
|
+
note(text: string): void;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface ContextOptions {
|
|
133
|
+
cwd?: string;
|
|
134
|
+
env?: Record<string, string | undefined>;
|
|
135
|
+
fetch?: (url: string, init?: any) => Promise<{ status: number; json(): Promise<any> }>;
|
|
136
|
+
tool?: (provider: string, name: string, args: Record<string, any>) => any;
|
|
137
|
+
now?: () => number;
|
|
138
|
+
sleep?: (ms: number) => Promise<void>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface FillOptions {
|
|
142
|
+
/** What a placeholder with no value does: throw (default), become empty, or stay as is. */
|
|
143
|
+
missing?: "throw" | "empty" | "keep";
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function createContext(input?: Record<string, any>, opts?: ContextOptions): Context;
|
|
147
|
+
|
|
148
|
+
// ---- the runner -----------------------------------------------------------
|
|
149
|
+
|
|
150
|
+
export type CheckFn = (ctx: Context) => unknown | Promise<unknown>;
|
|
151
|
+
|
|
152
|
+
export interface CheckApi {
|
|
153
|
+
(name: string, fn: CheckFn): void;
|
|
154
|
+
skip(name: string, fn?: CheckFn): void;
|
|
155
|
+
only(name: string, fn: CheckFn): void;
|
|
156
|
+
}
|
|
157
|
+
export const check: CheckApi;
|
|
158
|
+
export function describe(name: string, fn: () => void): void;
|
|
159
|
+
|
|
160
|
+
export interface DefinedCheck {
|
|
161
|
+
name: string;
|
|
162
|
+
fn: CheckFn;
|
|
163
|
+
mode: null | "skip" | "only";
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Run the checks defined so far. Default input: $DASH_INPUT. */
|
|
167
|
+
export function run(input?: Record<string, any>, opts?: ContextOptions & { ctx?: Context; filter?: (name: string) => boolean }): Promise<RunResult>;
|
|
168
|
+
export function runChecks(checks: DefinedCheck[], ctx: Context, opts?: { filter?: (name: string) => boolean }): Promise<RunResult>;
|
|
169
|
+
export function loadChecks(file: string): DefinedCheck[];
|
|
170
|
+
export function takeChecks(): DefinedCheck[];
|
|
171
|
+
/** Run a .check.js or .check.json file in this process. */
|
|
172
|
+
export function runFile(file: string, input?: Record<string, any>, opts?: ContextOptions & { ctx?: Context }): Promise<RunResult>;
|
|
173
|
+
/** {"reason", "details"}: Dash's check contract, one line. */
|
|
174
|
+
export function lastLine(result: RunResult): string;
|
|
175
|
+
export function report(result: RunResult, opts?: { title?: string; color?: boolean }): string;
|
|
176
|
+
|
|
177
|
+
// ---- expect ---------------------------------------------------------------
|
|
178
|
+
|
|
179
|
+
/** Each matcher returns whether it passed; its last argument is the failure's message. */
|
|
180
|
+
export interface Matchers<R = boolean> {
|
|
181
|
+
toBe(expected: unknown, message?: string): R;
|
|
182
|
+
toEqual(expected: unknown, message?: string): R;
|
|
183
|
+
toBeDefined(message?: string): R;
|
|
184
|
+
toBeTruthy(message?: string): R;
|
|
185
|
+
toBeArray(message?: string): R;
|
|
186
|
+
toBeString(message?: string): R;
|
|
187
|
+
toBeNonEmptyString(message?: string): R;
|
|
188
|
+
toBeNumber(message?: string): R;
|
|
189
|
+
toBeEmpty(message?: string): R;
|
|
190
|
+
toHaveLength(n: number, message?: string): R;
|
|
191
|
+
toHaveLengthAtLeast(n: number, message?: string): R;
|
|
192
|
+
toContain(item: unknown, message?: string): R;
|
|
193
|
+
toBeOneOf(options: unknown[], message?: string): R;
|
|
194
|
+
toHaveKeys(keys: string[], message?: string): R;
|
|
195
|
+
toMatch(pattern: RegExp | string, message?: string): R;
|
|
196
|
+
toBeGreaterThan(n: number, message?: string): R;
|
|
197
|
+
toBeLessThan(n: number, message?: string): R;
|
|
198
|
+
[custom: string]: any;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface Expectation extends Matchers<boolean> {
|
|
202
|
+
not: Matchers<boolean>;
|
|
203
|
+
resolves: Matchers<Promise<boolean>> & { not: Matchers<Promise<boolean>> };
|
|
204
|
+
rejects: Matchers<Promise<boolean>> & { not: Matchers<Promise<boolean>> };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface MatcherParam {
|
|
208
|
+
name: string;
|
|
209
|
+
type: string;
|
|
210
|
+
label: string;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface MatcherMeta {
|
|
214
|
+
id: string;
|
|
215
|
+
label: string;
|
|
216
|
+
sentence: string;
|
|
217
|
+
notSentence: string;
|
|
218
|
+
params: MatcherParam[];
|
|
219
|
+
description: string;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export interface MatcherDef {
|
|
223
|
+
id: string;
|
|
224
|
+
label?: string;
|
|
225
|
+
sentence: string;
|
|
226
|
+
notSentence?: string;
|
|
227
|
+
params?: Array<{ name: string; type?: string; label?: string }>;
|
|
228
|
+
description?: string;
|
|
229
|
+
test(received: any, ...args: any[]): boolean | { pass: boolean; got?: string };
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export interface ExpectFn {
|
|
233
|
+
(value: unknown, label?: string): Expectation;
|
|
234
|
+
matchers(): MatcherMeta[];
|
|
235
|
+
extend(defs: MatcherDef | MatcherDef[]): MatcherMeta[];
|
|
236
|
+
has(id: string): boolean;
|
|
237
|
+
meta(id: string): MatcherMeta | null;
|
|
238
|
+
sentence(id: string, label: string, args?: unknown[], not?: boolean): string | null;
|
|
239
|
+
}
|
|
240
|
+
export const expect: ExpectFn;
|
|
241
|
+
export function defineMatcher(def: MatcherDef): MatcherMeta;
|
|
242
|
+
|
|
243
|
+
/** Fail the running check with a message (the fix), going on with the rest. */
|
|
244
|
+
export function fail(message: string, extra?: { at?: string; expected?: unknown; received?: unknown }): false;
|
|
245
|
+
/** Say how the running check passed. */
|
|
246
|
+
export function note(text: string): void;
|
|
247
|
+
/** Run fn with its own failure list. Never throws. */
|
|
248
|
+
export function collect(fn: () => unknown): Promise<{ failures: Failure[]; notes: string[] }>;
|
|
249
|
+
|
|
250
|
+
export class CheckFailure extends Error {
|
|
251
|
+
failures: Failure[];
|
|
252
|
+
}
|
|
253
|
+
export class DashCheckError extends Error {
|
|
254
|
+
fix?: string;
|
|
255
|
+
code?: number;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// ---- helpers --------------------------------------------------------------
|
|
259
|
+
|
|
260
|
+
export type ParamType = "text" | "number" | "boolean" | "select" | "path" | "glob" | "regex" | "list";
|
|
261
|
+
|
|
262
|
+
export interface ParamDef {
|
|
263
|
+
type?: ParamType;
|
|
264
|
+
label?: string;
|
|
265
|
+
description?: string;
|
|
266
|
+
default?: unknown;
|
|
267
|
+
required?: boolean;
|
|
268
|
+
options?: Array<string | number>;
|
|
269
|
+
placeholder?: string;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export interface HelperMeta {
|
|
273
|
+
id: string;
|
|
274
|
+
group: string;
|
|
275
|
+
label: string;
|
|
276
|
+
description: string;
|
|
277
|
+
sentence: string;
|
|
278
|
+
params: Record<string, ParamDef & { type: ParamType; label: string }>;
|
|
279
|
+
reads: string[];
|
|
280
|
+
source?: string;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export interface HelperDef<P = Record<string, any>> {
|
|
284
|
+
/** "group.name", e.g. "team.changelogUpdated". */
|
|
285
|
+
id: string;
|
|
286
|
+
group?: string;
|
|
287
|
+
label: string;
|
|
288
|
+
description?: string;
|
|
289
|
+
/** "at least {min} commit(s) past {base}" */
|
|
290
|
+
sentence?: string;
|
|
291
|
+
params?: Record<string, ParamDef>;
|
|
292
|
+
/** What it reads: git, fs, run, github, tool, handoff… */
|
|
293
|
+
reads?: string[];
|
|
294
|
+
run(ctx: Context, params: P): unknown | Promise<unknown>;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export interface Helper<P = Record<string, any>> {
|
|
298
|
+
(ctx: Context, params?: Partial<P> & { message?: string }): Promise<boolean>;
|
|
299
|
+
meta: HelperMeta;
|
|
300
|
+
isDashCheckHelper: true;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function defineHelper<P = Record<string, any>>(def: HelperDef<P>): Helper<P>;
|
|
304
|
+
/** A registered helper by id, to call from code (a repo's own are found from ctx.cwd). */
|
|
305
|
+
export function helper(id: string): (ctx: Context, params?: Record<string, any>) => Promise<boolean>;
|
|
306
|
+
export function getHelper(id: string): Helper | null;
|
|
307
|
+
export function listHelpers(): HelperMeta[];
|
|
308
|
+
export function registerHelpers(helpers: Helper | Helper[], opts?: { source?: string; replace?: boolean }): Helper[];
|
|
309
|
+
/** Find a repo's own helpers: .dash/checks/helpers/*.js and packages with "dashCheck": { "helpers": [...] }. */
|
|
310
|
+
export function loadHelpers(opts?: { cwd?: string }): { helpers: Array<{ id: string; source: string }>; errors: Array<{ source: string; error: string }> };
|
|
311
|
+
export function sentenceOf(meta: HelperMeta, params?: Record<string, any>): string;
|
|
312
|
+
export function resolveParams(meta: HelperMeta, ctx: Context | null, params?: Record<string, any>): Record<string, any>;
|
|
313
|
+
|
|
314
|
+
// ---- small tools ----------------------------------------------------------
|
|
315
|
+
|
|
316
|
+
export function readHandoff(input: Record<string, any>): { handoff: Record<string, any>; info: Context["handoffInfo"] };
|
|
317
|
+
export function lastJsonBlock(text: string): { found: boolean; value?: any; fenced?: boolean; broken?: boolean; error?: string };
|
|
318
|
+
export function get(ctx: Context, path: string): any;
|
|
319
|
+
export function fill(ctx: Context, value: unknown, opts?: FillOptions): any;
|
|
320
|
+
export function when(ctx: Context, expr: string | boolean | undefined): boolean;
|
|
321
|
+
export function valuePaths(ctx: Context, opts?: { maxDepth?: number; max?: number }): Array<{ path: string; type: string; sample?: unknown }>;
|
|
322
|
+
export function matchGlob(globs: string | string[], path: string): boolean;
|
|
323
|
+
export function globToRegExp(glob: string): RegExp;
|
|
324
|
+
export function isEmpty(value: unknown): boolean;
|
|
325
|
+
export function deepEqual(a: unknown, b: unknown): boolean;
|