@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,9 @@
|
|
|
1
|
+
// Walkthrough 10. Merge Bot: "The PR was merged only when allowed".
|
|
2
|
+
// Checks the outcome on GitHub, not the bot's word. Reads: handoff, github, settings.
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the PR was merged only when allowed", async ({ handoff, github, settings }) => {
|
|
6
|
+
const pr = await github.pr(handoff.prUrl);
|
|
7
|
+
if (settings.mergeWhenGreen) expect(pr.merged, "merged").toBe(true, `PR #${pr.number} wasn't merged`);
|
|
8
|
+
else expect(pr.merged, "merged").toBe(false, `PR #${pr.number} was merged, but Merge when green is off`);
|
|
9
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Walkthrough 3. Workspace Bot: "On the ticket's branch" (Dash's git-on-branch).
|
|
2
|
+
// Reads: git, workspace, row. Run it in the job's workspace repo.
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the workspace is on the job's branch", async ({ workspace, git }) => {
|
|
6
|
+
expect(await git.branch(), "the branch").toBe(workspace.branch, `The workspace is on another branch, not ${workspace.branch}`);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
check("the branch is new from the base", async ({ git, row }) => {
|
|
10
|
+
const base = `origin/${row.repo?.baseBranch || "main"}`;
|
|
11
|
+
expect(await git.commitsSince(base), `commits past ${base}`).toBe(0, `The branch already has commits past ${base}: start it fresh from ${base}`);
|
|
12
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Walkthrough 2. Triage: "The plan is complete".
|
|
2
|
+
// Lives in the shared package. Reads: handoff, picks, settings (the Repos table).
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the plan has a cause, a fix and a test", ({ handoff }) => {
|
|
6
|
+
for (const part of ["cause", "fix", "test"]) {
|
|
7
|
+
expect(handoff.plan?.[part], `handoff.plan.${part}`).toBeNonEmptyString(`The plan has no ${part}`);
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
check("the plan picks one of the team's repos", ({ picks, settings }) => {
|
|
12
|
+
const keys = (settings.repo || []).map((r) => r.key);
|
|
13
|
+
expect(picks.repo, "picks.repo").toBeOneOf(keys, `"${picks.repo}" isn't in the Repos table (${keys.join(", ")})`);
|
|
14
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
{
|
|
2
|
+
"input": { "answer": "```json\n{ \"plan\": { \"cause\": \"The slug drops dashes\", \"fix\": \"Keep dashes in slugify()\", \"test\": \"slugify('a-b') is 'a-b'\" } }\n```" },
|
|
3
|
+
"picks": { "repo": "code-factory-sandbox" },
|
|
4
|
+
"settings": { "repo": [{ "key": "code-factory-sandbox", "baseBranch": "main" }, { "key": "dash-core", "baseBranch": "master" }] }
|
|
5
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Walkthrough 9. PR Bot: "The PR's CI passes" (Dash's pr-ci). It fails closed:
|
|
2
|
+
// no CI is a failure that says what to do. Reads: handoff, github (GH_TOKEN).
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the PR's CI passed", async ({ handoff, github }) => {
|
|
6
|
+
const pr = await github.pr(handoff.prUrl);
|
|
7
|
+
expect(pr.state, `PR #${pr.number}'s state`).toBe("open", `PR #${pr.number} isn't open`);
|
|
8
|
+
|
|
9
|
+
const result = await github.waitForChecks(pr.head.sha, { timeoutSec: 1200, noChecksAfterSec: 180 });
|
|
10
|
+
expect(result.runs, "the CI runs").not.toBeEmpty(`${handoff.prUrl} has no CI. Enable Actions on the repo, or set ifNoCI: "pass" on this step`);
|
|
11
|
+
expect(result.failed, "the failed CI runs").toBeEmpty(`CI failed: ${result.failed.map((r) => `${r.name} (${r.conclusion})`).join(", ")}`);
|
|
12
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "The work is ready for review",
|
|
3
|
+
"steps": [
|
|
4
|
+
{ "use": "git.committedSince", "with": { "base": "{repo.baseBranch}", "min": 1 } },
|
|
5
|
+
{ "use": "node.testsPass", "with": { "command": "npm test" } },
|
|
6
|
+
{ "use": "files.noneMatch",
|
|
7
|
+
"with": { "glob": "src/**/*.js", "pattern": "console\\.log" },
|
|
8
|
+
"message": "Remove console.log from src" },
|
|
9
|
+
{ "expect": "handoff.files", "matcher": "toHaveLengthAtLeast", "args": [1],
|
|
10
|
+
"message": "The handoff lists no changed files" }
|
|
11
|
+
]
|
|
12
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"input": { "answer": "Done.\n\n```json\n{ \"status\": \"ok\", \"files\": [\"src/index.js\", \"test/index.test.js\"] }\n```" },
|
|
3
|
+
"item": { "id": "DASH-97" },
|
|
4
|
+
"workspace": { "dir": ".", "branch": "feat/DASH-97-titlecase" },
|
|
5
|
+
"row": { "repo": { "baseBranch": "main" } }
|
|
6
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Walkthrough 4. Workspace Bot: "Setup command passes".
|
|
2
|
+
// Lives in the repo: <repo>/.dash/checks/setup.check.js. Reads: run.
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("dependencies install", async ({ run }) => {
|
|
6
|
+
const r = await run("npm ci", { timeoutSec: 900 });
|
|
7
|
+
expect(r.code, "npm ci's exit code").toBe(0, `npm ci failed:\n${r.stderr.slice(-2000)}`);
|
|
8
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Walkthrough 1. Ticket Bot: "A ticket with acceptance items".
|
|
2
|
+
// Lives in the shared package @dash-checks/code-factory. Reads: handoff.
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the ticket has acceptance items", ({ handoff }) => {
|
|
6
|
+
expect(handoff.acceptance, "handoff.acceptance").toBeArray("The ticket's acceptance items aren't a list");
|
|
7
|
+
expect(handoff.acceptance, "handoff.acceptance").not.toBeEmpty(
|
|
8
|
+
"The ticket has no acceptance items: add them to the ticket, or write one per testable thing it asks for",
|
|
9
|
+
);
|
|
10
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Walkthrough 6. Coder: "Validate command passes".
|
|
2
|
+
// Lives in the repo: <repo>/.dash/checks/validate.check.js. Reads: run.
|
|
3
|
+
// Dash runs the base branch's copy, so the Coder can't edit it to pass.
|
|
4
|
+
const { check, expect } = require("@trops/dash-check");
|
|
5
|
+
|
|
6
|
+
check("lint passes", async ({ run }) => {
|
|
7
|
+
const r = await run("npm run lint");
|
|
8
|
+
expect(r.code, "npm run lint's exit code").toBe(0, r.stdout.slice(-2000));
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
check("tests pass", async ({ run }) => {
|
|
12
|
+
const r = await run("npm test", { timeoutSec: 900 });
|
|
13
|
+
expect(r.code, "npm test's exit code").toBe(0, r.stdout.slice(-3000));
|
|
14
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Walkthrough 7. Coder: "Work is committed" (Dash's git-committed).
|
|
2
|
+
// Reads: git, row, item.
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the work is committed", async ({ git, row }) => {
|
|
6
|
+
const base = `origin/${row.repo?.baseBranch || "main"}`;
|
|
7
|
+
expect(await git.commitsSince(base), `commits past ${base}`).toBeGreaterThan(0, `No commits past ${base}`);
|
|
8
|
+
expect(await git.isClean(), "the work tree is clean").toBe(true, "There are uncommitted changes");
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
check("the commit names the item", async ({ git, item }) => {
|
|
12
|
+
expect(await git.lastCommitMessage(), "the last commit's message").toContain(item.id, `The commit message should start with ${item.id}`);
|
|
13
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Walkthrough 11. Reporter: "The ticket's folder is gone". (Better still: Dash
|
|
2
|
+
// removes the job's workspace itself, and this check goes away.) Reads: workspace.
|
|
3
|
+
const { check, expect } = require("@trops/dash-check");
|
|
4
|
+
|
|
5
|
+
check("the workspace is cleaned up", async ({ workspace }) => {
|
|
6
|
+
expect(await workspace.dirExists(), `${workspace.dir} exists`).toBe(false, `${workspace.dir} is still there`);
|
|
7
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commands helpers: run the repo's own commands. Built only from Layer 0
|
|
3
|
+
* (ctx.run).
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { defineHelper, expect, fail, note } = require("..");
|
|
8
|
+
|
|
9
|
+
const tail = (s, n = 1500) => {
|
|
10
|
+
const t = String(s || "").trim();
|
|
11
|
+
return t.length > n ? `…${t.slice(-n)}` : t;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** Run a command; on a non-zero exit fail with its last output. Gives the run. */
|
|
15
|
+
async function runOrFail(ctx, command, timeoutSec, what = command) {
|
|
16
|
+
const r = await ctx.run(command, { timeoutSec });
|
|
17
|
+
if (r.timedOut) {
|
|
18
|
+
fail(`${what} didn't finish in ${timeoutSec} seconds: make it faster, or raise the time limit`, { at: tail(r.stdout + r.stderr, 800) || undefined });
|
|
19
|
+
} else if (r.code !== 0) {
|
|
20
|
+
const out = tail(`${r.stdout}\n${r.stderr}`);
|
|
21
|
+
fail(`${what} failed (exit ${r.code})`, { at: out || undefined, received: r.code, expected: 0 });
|
|
22
|
+
}
|
|
23
|
+
return r;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const passes = defineHelper({
|
|
27
|
+
id: "commands.passes",
|
|
28
|
+
group: "Commands",
|
|
29
|
+
label: "Command passes",
|
|
30
|
+
description: "Exit 0",
|
|
31
|
+
sentence: "{command} exits 0",
|
|
32
|
+
params: {
|
|
33
|
+
command: { type: "text", label: "Command", required: true },
|
|
34
|
+
timeoutSec: { type: "number", label: "Time limit (s)", default: 120 },
|
|
35
|
+
},
|
|
36
|
+
reads: ["run"],
|
|
37
|
+
async run(ctx, { command, timeoutSec }) {
|
|
38
|
+
const r = await runOrFail(ctx, command, timeoutSec);
|
|
39
|
+
if (r.code === 0) note(`${command} passed`);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const outputMatches = defineHelper({
|
|
44
|
+
id: "commands.outputMatches",
|
|
45
|
+
group: "Commands",
|
|
46
|
+
label: "Command output matches",
|
|
47
|
+
description: "Its output matches a pattern",
|
|
48
|
+
sentence: "the output of {command} matches {pattern}",
|
|
49
|
+
params: {
|
|
50
|
+
command: { type: "text", label: "Command", required: true },
|
|
51
|
+
pattern: { type: "regex", label: "Pattern", required: true },
|
|
52
|
+
timeoutSec: { type: "number", label: "Time limit (s)", default: 120 },
|
|
53
|
+
},
|
|
54
|
+
reads: ["run"],
|
|
55
|
+
async run(ctx, { command, pattern, timeoutSec }) {
|
|
56
|
+
const r = await ctx.run(command, { timeoutSec });
|
|
57
|
+
const out = `${r.stdout}${r.stderr ? `\n${r.stderr}` : ""}`;
|
|
58
|
+
expect(out, `the output of ${command}`).toMatch(pattern, `The output of ${command} doesn't match /${pattern.source}/`);
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
module.exports = { passes, outputMatches, runOrFail };
|
package/helpers/files.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files helpers: what's in the workspace. Built only from Layer 0 (ctx.fs).
|
|
3
|
+
*/
|
|
4
|
+
"use strict";
|
|
5
|
+
|
|
6
|
+
const { defineHelper, expect, fail, note } = require("..");
|
|
7
|
+
|
|
8
|
+
const exist = defineHelper({
|
|
9
|
+
id: "files.exist",
|
|
10
|
+
group: "Files",
|
|
11
|
+
label: "Files exist",
|
|
12
|
+
description: "Paths in the workspace",
|
|
13
|
+
sentence: "{paths} exist",
|
|
14
|
+
params: { paths: { type: "list", label: "Paths", required: true } },
|
|
15
|
+
reads: ["fs"],
|
|
16
|
+
async run(ctx, { paths }) {
|
|
17
|
+
for (const p of paths) expect(await ctx.fs.exists(p), p).toBe(true, `${p} isn't there: add it`);
|
|
18
|
+
note(`${paths.join(", ")} exist`);
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const contain = defineHelper({
|
|
23
|
+
id: "files.contain",
|
|
24
|
+
group: "Files",
|
|
25
|
+
label: "File contains text",
|
|
26
|
+
description: "A file has some text in it",
|
|
27
|
+
sentence: "{path} contains {text}",
|
|
28
|
+
params: {
|
|
29
|
+
path: { type: "path", label: "File", required: true },
|
|
30
|
+
text: { type: "text", label: "Text", required: true },
|
|
31
|
+
},
|
|
32
|
+
reads: ["fs"],
|
|
33
|
+
async run(ctx, { path, text }) {
|
|
34
|
+
if (!(await ctx.fs.exists(path))) {
|
|
35
|
+
fail(`${path} isn't there: add it`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const body = await ctx.fs.read(path);
|
|
39
|
+
if (!body.includes(text)) fail(`${path} doesn't contain ${JSON.stringify(text)}: add it`);
|
|
40
|
+
else note(`${path} contains it`);
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const noneMatch = defineHelper({
|
|
45
|
+
id: "files.noneMatch",
|
|
46
|
+
group: "Files",
|
|
47
|
+
label: "No file matches a pattern",
|
|
48
|
+
description: "e.g. no console.log in src",
|
|
49
|
+
sentence: "in {glob} no match for {pattern}",
|
|
50
|
+
params: {
|
|
51
|
+
glob: { type: "glob", label: "Files", required: true, placeholder: "src/**/*.js" },
|
|
52
|
+
pattern: { type: "regex", label: "Pattern", required: true, placeholder: "console\\.log" },
|
|
53
|
+
},
|
|
54
|
+
reads: ["fs"],
|
|
55
|
+
async run(ctx, { glob, pattern }) {
|
|
56
|
+
const files = await ctx.fs.list(glob);
|
|
57
|
+
const hits = [];
|
|
58
|
+
for (const f of files) {
|
|
59
|
+
const lines = (await ctx.fs.read(f)).split(/\r?\n/);
|
|
60
|
+
lines.forEach((line, i) => {
|
|
61
|
+
pattern.lastIndex = 0;
|
|
62
|
+
if (pattern.test(line)) hits.push(`${f}:${i + 1}`);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (hits.length) {
|
|
66
|
+
fail(`${glob} has ${hits.length} match${hits.length === 1 ? "" : "es"} for /${pattern.source}/: remove them`, {
|
|
67
|
+
at: `${hits.slice(0, 10).join(", ")}${hits.length > 10 ? `, and ${hits.length - 10} more` : ""}`,
|
|
68
|
+
received: hits,
|
|
69
|
+
});
|
|
70
|
+
} else note(`No match in ${files.length} file${files.length === 1 ? "" : "s"}`);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
module.exports = { exist, contain, noneMatch };
|
package/helpers/git.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git helpers: the state of the workspace's repo. Built only from Layer 0
|
|
3
|
+
* (ctx.git), never git directly.
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { defineHelper, expect, fail, note, matchGlob, DashCheckError } = require("..");
|
|
8
|
+
|
|
9
|
+
const listOf = (files, n = 10) => `${files.slice(0, n).join(", ")}${files.length > n ? `, and ${files.length - n} more` : ""}`;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The ref to compare with: "origin/<base>" when the remote has it, else the
|
|
13
|
+
* local branch. With no base, the repo's default branch.
|
|
14
|
+
*/
|
|
15
|
+
async function baseRef(ctx, base) {
|
|
16
|
+
let b = String(base || "").trim();
|
|
17
|
+
if (!b) b = (await ctx.git.defaultBranch()) || "";
|
|
18
|
+
if (!b) {
|
|
19
|
+
throw new DashCheckError("No base branch to compare with", {
|
|
20
|
+
fix: "set the base (e.g. main), or give the repo an origin/HEAD",
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
if (b.startsWith("origin/") || b.startsWith("refs/")) return b;
|
|
24
|
+
if (await ctx.git.hasRef(`origin/${b}`)) return `origin/${b}`;
|
|
25
|
+
if (await ctx.git.hasRef(b)) return b;
|
|
26
|
+
return `origin/${b}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const BASE = { type: "text", label: "Base branch", default: "{repo.baseBranch}", description: "Empty: the repo's default branch" };
|
|
30
|
+
|
|
31
|
+
const onBranch = defineHelper({
|
|
32
|
+
id: "git.onBranch",
|
|
33
|
+
group: "Git",
|
|
34
|
+
label: "On the job's branch",
|
|
35
|
+
description: "The workspace is on workspace.branch",
|
|
36
|
+
sentence: "the workspace is on {branch}",
|
|
37
|
+
params: { branch: { type: "text", label: "Branch", default: "{workspace.branch}" } },
|
|
38
|
+
reads: ["git", "workspace"],
|
|
39
|
+
async run(ctx, { branch }) {
|
|
40
|
+
if (!branch) {
|
|
41
|
+
fail("No branch to check: the job's workspace has none. Name it in the handoff's workspace.branch, or set the branch");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const cur = await ctx.git.branch();
|
|
45
|
+
const shown = cur === "HEAD" ? "no branch (a detached HEAD)" : cur;
|
|
46
|
+
expect(cur, "the branch").toBe(branch, `The workspace is on ${shown}, not ${branch}: check out ${branch}`);
|
|
47
|
+
note(`On ${branch}`);
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const committedSince = defineHelper({
|
|
52
|
+
id: "git.committedSince",
|
|
53
|
+
group: "Git",
|
|
54
|
+
label: "Work is committed",
|
|
55
|
+
description: "Commits past the base, nothing uncommitted",
|
|
56
|
+
sentence: "at least {min} commit(s) past {base}, and nothing uncommitted",
|
|
57
|
+
params: { base: BASE, min: { type: "number", label: "At least", default: 1 } },
|
|
58
|
+
reads: ["git"],
|
|
59
|
+
async run(ctx, { base, min }) {
|
|
60
|
+
const ref = await baseRef(ctx, base);
|
|
61
|
+
const n = await ctx.git.commitsSince(ref);
|
|
62
|
+
expect(n, `commits past ${ref}`).toBeGreaterThan(min - 1, n === 0 ? `No commits past ${ref}: commit the work (git add -A; git commit)` : `Only ${n} commit(s) past ${ref}; it needs ${min}`);
|
|
63
|
+
const dirty = await ctx.git.uncommitted();
|
|
64
|
+
if (dirty.length) fail("There are uncommitted changes: commit or discard them", { at: listOf(dirty, 5), received: dirty });
|
|
65
|
+
note(`${n} commit${n === 1 ? "" : "s"} past ${ref}, nothing uncommitted`);
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const clean = defineHelper({
|
|
70
|
+
id: "git.clean",
|
|
71
|
+
group: "Git",
|
|
72
|
+
label: "Nothing uncommitted",
|
|
73
|
+
description: "No staged, unstaged or untracked changes",
|
|
74
|
+
sentence: "nothing is uncommitted",
|
|
75
|
+
reads: ["git"],
|
|
76
|
+
async run(ctx) {
|
|
77
|
+
const dirty = await ctx.git.uncommitted();
|
|
78
|
+
if (dirty.length) fail("There are uncommitted changes: commit or discard them", { at: listOf(dirty, 5), received: dirty });
|
|
79
|
+
else note("Nothing uncommitted");
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const changedOnly = defineHelper({
|
|
84
|
+
id: "git.changedOnly",
|
|
85
|
+
group: "Git",
|
|
86
|
+
label: "Only these files changed",
|
|
87
|
+
description: "Changed files match globs",
|
|
88
|
+
sentence: "only files matching {globs} changed since {base}",
|
|
89
|
+
params: { globs: { type: "list", label: "Globs", required: true, description: 'e.g. src/**, test/**, !src/generated/**' }, base: BASE },
|
|
90
|
+
reads: ["git"],
|
|
91
|
+
async run(ctx, { globs, base }) {
|
|
92
|
+
const ref = await baseRef(ctx, base);
|
|
93
|
+
const changed = await ctx.git.changedFiles(ref);
|
|
94
|
+
const extra = changed.filter((f) => !matchGlob(globs, f));
|
|
95
|
+
if (extra.length) fail(`Files outside ${globs.join(", ")} changed: undo them, or widen the globs`, { at: listOf(extra), received: extra });
|
|
96
|
+
else note(`${changed.length} changed file${changed.length === 1 ? "" : "s"}, all in ${globs.join(", ")}`);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const noChangesTo = defineHelper({
|
|
101
|
+
id: "git.noChangesTo",
|
|
102
|
+
group: "Git",
|
|
103
|
+
label: "These files didn't change",
|
|
104
|
+
description: "No changed file matches the globs",
|
|
105
|
+
sentence: "nothing matching {globs} changed since {base}",
|
|
106
|
+
params: { globs: { type: "list", label: "Globs", required: true }, base: BASE },
|
|
107
|
+
reads: ["git"],
|
|
108
|
+
async run(ctx, { globs, base }) {
|
|
109
|
+
const ref = await baseRef(ctx, base);
|
|
110
|
+
const touched = (await ctx.git.changedFiles(ref)).filter((f) => matchGlob(globs, f));
|
|
111
|
+
if (touched.length) fail(`These must not change, but did: undo the changes to them`, { at: listOf(touched), received: touched });
|
|
112
|
+
else note(`Nothing in ${globs.join(", ")} changed`);
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
module.exports = { onBranch, committedSince, clean, changedOnly, noChangesTo, baseRef };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub helpers: the PR in the handoff, checked on GitHub itself. Built
|
|
3
|
+
* only from Layer 0 (ctx.github).
|
|
4
|
+
*/
|
|
5
|
+
"use strict";
|
|
6
|
+
|
|
7
|
+
const { defineHelper, expect, fail, note } = require("..");
|
|
8
|
+
|
|
9
|
+
const mins = (sec) => {
|
|
10
|
+
const m = Math.max(1, Math.round(sec / 60));
|
|
11
|
+
return `${m} minute${m === 1 ? "" : "s"}`;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
function prUrlOf(ctx, prKey) {
|
|
15
|
+
const url = ctx.get(`handoff.${prKey}`);
|
|
16
|
+
if (url === undefined || url === null || url === "") {
|
|
17
|
+
fail(`The handoff has no ${prKey}: put the PR's link in it`);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
return url;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const prOpen = defineHelper({
|
|
24
|
+
id: "github.prOpen",
|
|
25
|
+
group: "GitHub",
|
|
26
|
+
label: "The PR is open",
|
|
27
|
+
description: "The handoff's PR is open on GitHub",
|
|
28
|
+
sentence: "the PR in handoff.{prKey} is open",
|
|
29
|
+
params: { prKey: { type: "text", label: "PR key", default: "prUrl" } },
|
|
30
|
+
reads: ["handoff", "github"],
|
|
31
|
+
async run(ctx, { prKey }) {
|
|
32
|
+
const url = prUrlOf(ctx, prKey);
|
|
33
|
+
if (!url) return;
|
|
34
|
+
const pr = await ctx.github.pr(url);
|
|
35
|
+
expect(pr.state, `PR #${pr.number}'s state`).toBe("open", `PR #${pr.number} isn't open (it's ${pr.merged ? "merged" : pr.state})`);
|
|
36
|
+
note(`PR #${pr.number} is open`);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const ciPassed = defineHelper({
|
|
41
|
+
id: "github.ciPassed",
|
|
42
|
+
group: "GitHub",
|
|
43
|
+
label: "The PR's CI passed",
|
|
44
|
+
description: "Waits, with a deadline",
|
|
45
|
+
sentence: "the CI of the PR in handoff.{prKey} passed, waiting up to {timeoutSec} seconds",
|
|
46
|
+
params: {
|
|
47
|
+
prKey: { type: "text", label: "PR key", default: "prUrl" },
|
|
48
|
+
timeoutSec: { type: "number", label: "Wait up to (s)", default: 1200 },
|
|
49
|
+
noChecksAfterSec: { type: "number", label: "No CI after (s)", default: 180 },
|
|
50
|
+
ifNoChecks: { type: "select", label: "If it has no CI", options: ["fail", "pass"], default: "fail" },
|
|
51
|
+
},
|
|
52
|
+
reads: ["handoff", "github"],
|
|
53
|
+
async run(ctx, { prKey, timeoutSec, noChecksAfterSec, ifNoChecks }) {
|
|
54
|
+
const url = prUrlOf(ctx, prKey);
|
|
55
|
+
if (!url) return;
|
|
56
|
+
const pr = await ctx.github.pr(url);
|
|
57
|
+
if (!pr.head || !pr.head.sha) {
|
|
58
|
+
fail(`Couldn't read PR #${pr.number}'s last commit`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const r = await ctx.github.waitForChecks(pr.head.sha, { timeoutSec, noChecksAfterSec, repo: pr.slug });
|
|
62
|
+
if (r.noChecks) {
|
|
63
|
+
if (ifNoChecks === "pass") note(`${url} has no CI checks (allowed)`);
|
|
64
|
+
else fail(`${url} has no CI after ${mins(noChecksAfterSec)}: enable Actions on the repo, or set "If it has no CI" to pass`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (r.failed.length) {
|
|
68
|
+
fail("CI failed", { at: r.failed.map((x) => `${x.name} (${x.conclusion})`).join(", "), received: r.failed });
|
|
69
|
+
}
|
|
70
|
+
if (r.timedOut) {
|
|
71
|
+
fail(`CI is still running on ${url} after ${mins(timeoutSec)}`, { at: r.pending.map((x) => x.name).join(", ") || undefined });
|
|
72
|
+
}
|
|
73
|
+
if (!r.failed.length && !r.timedOut) note(`${r.passed.length} CI check${r.passed.length === 1 ? "" : "s"} passed`);
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
module.exports = { prOpen, ciPassed };
|