@trops/dash-check 0.3.1 → 0.4.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/README.md +4 -3
- package/helpers/github.js +28 -1
- package/helpers/index.d.ts +1 -1
- package/helpers/node.js +36 -3
- package/index.d.ts +1 -0
- package/lib/adapters/github.js +12 -0
- package/package.json +9 -2
package/README.md
CHANGED
|
@@ -122,7 +122,7 @@ You can also call `fail(message, { at })` to fail with a message and a location,
|
|
|
122
122
|
| `ctx.fs` | `exists(p)`, `read(p)`, `readJson(p)`, `list(glob)`, `dirExists(p)`, relative to the workspace |
|
|
123
123
|
| `ctx.run(cmd, { timeoutSec })` | Runs a command in the workspace: `{ code, stdout, stderr, timedOut }` |
|
|
124
124
|
| `ctx.git` | `branch()`, `isClean()`, `uncommitted()`, `commitsSince(ref)`, `changedFiles(ref)`, `addedLines(ref)`, `commits(ref)`, `lastCommitMessage()`, `remoteHas(branch)`, `defaultBranch()`, `fileAt(ref, path)` |
|
|
125
|
-
| `ctx.github` | `pr(urlOrNumber)`, `checksFor(sha)`, `waitForChecks(sha, { timeoutSec, noChecksAfterSec })`, using `GH_TOKEN`. A failure says the HTTP code and what to do |
|
|
125
|
+
| `ctx.github` | `pr(urlOrNumber)`, `checksFor(sha)`, `waitForChecks(sha, { timeoutSec, noChecksAfterSec })`, `compare(base, head)`, using `GH_TOKEN`. A failure says the HTTP code and what to do |
|
|
126
126
|
| `ctx.tool(provider, name, args)` | A read-only call to one of the bot's providers (Jira, …). Only Dash can answer it, so outside Dash it fails and says so |
|
|
127
127
|
| `ctx.get(path)`, `ctx.fill(text)`, `ctx.when(expr)` | Read a value by path (`"handoff.files"`), fill `"{placeholders}"`, test a `when` |
|
|
128
128
|
|
|
@@ -150,7 +150,7 @@ check("the work is ready for review", async (ctx) => {
|
|
|
150
150
|
});
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
There are
|
|
153
|
+
There are 34, in eight groups: `handoff`, `plan`, `git`, `files`, `commands`, `node`, `github` and `tracker`. The [helper reference](#helper-reference) lists every one with its params, defaults and an example step.
|
|
154
154
|
|
|
155
155
|
`npx dash-check --list-helpers` lists each one with its params and defaults.
|
|
156
156
|
|
|
@@ -208,7 +208,7 @@ Checks for a code factory, where a planning bot names the files and a coding bot
|
|
|
208
208
|
| `files.maxGrowth` | A changed file is new and over `maxLines`, crosses `maxLines`, or was already over it and grew by more than `maxAdded` | `glob` (JS/TS), `maxLines` (800), `maxAdded` (50), `exempt`, `base` |
|
|
209
209
|
| `git.validateLeavesTreeClean` | The tree isn't clean before validating, the command fails, or it leaves files changed (a formatter's `--write`, `npm install --save`) | `command` (`npm test`), `timeoutSec` (1800), `allowDirtyBefore` (false) |
|
|
210
210
|
| `node.testScriptIsReal` | The `test` script is missing, empty, a no-op (`exit 0`, `true`, `echo …`, npm's placeholder) or hides its failures (`… \|\| true`). With `run`, also when it reports fewer than `minTests` tests | `script` (`test`), `run` (false), `minTests` (1), `timeoutSec` (900) |
|
|
211
|
-
| `node.newTestsAreRun` | A **new** test file isn't named in a runner file, isn't matched by a glob written in one (`node --test 'test/*.test.js'`), and isn't in `autoGlobs` | `tests` (`**/*.{test,spec}.{js,cjs,mjs,jsx,ts,tsx}`), `runnerFiles` (`package.json`), `autoGlobs` (what the runner finds by itself, e.g. `src/**/*.test.js` for jest), `base` |
|
|
211
|
+
| `node.newTestsAreRun` | A **new** test file isn't named in a runner file, isn't matched by a glob written in one (`node --test 'test/*.test.js'`, brace globs such as `src/**/*.{test,spec}.js` included), and isn't in `autoGlobs` | `tests` (`**/*.{test,spec}.{js,cjs,mjs,jsx,ts,tsx}`), `runnerFiles` (`package.json`), `autoGlobs` (what the runner finds by itself, e.g. `src/**/*.test.js` for jest), `base` |
|
|
212
212
|
|
|
213
213
|
A coding bot's step might run them in this order:
|
|
214
214
|
|
|
@@ -378,6 +378,7 @@ Every helper in `@trops/dash-check/helpers`, from `dash-check --list-helpers --j
|
|
|
378
378
|
| Helper | Checks | Params (default) | Example step |
|
|
379
379
|
|---|---|---|---|
|
|
380
380
|
| `github.prOpen` | The handoff's PR is open on GitHub | `prKey` text (`prUrl`) | `{"use":"github.prOpen"}` |
|
|
381
|
+
| `github.upToDate` | The PR has every commit of its base branch. Put it before merging when the repo requires up-to-date branches: a PR that fell behind fails with how far, so the bot updates the branch | `prKey` text (`prUrl`) | `{"use":"github.upToDate"}` |
|
|
381
382
|
| `github.ciPassed` | Waits, with a deadline | `prKey` text (`prUrl`); `timeoutSec` number (`1200`); `noChecksAfterSec` number (`180`); `ifNoChecks` select (`fail`): `fail` or `pass` | `{"use":"github.ciPassed","with":{"timeoutSec":1200,"ifNoChecks":"fail"}}` |
|
|
382
383
|
|
|
383
384
|
### tracker
|
package/helpers/github.js
CHANGED
|
@@ -74,4 +74,31 @@ const ciPassed = defineHelper({
|
|
|
74
74
|
},
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
const upToDate = defineHelper({
|
|
78
|
+
id: "github.upToDate",
|
|
79
|
+
group: "GitHub",
|
|
80
|
+
label: "The PR has the latest base",
|
|
81
|
+
description: "Its branch has every commit of the base branch (put it before merging)",
|
|
82
|
+
sentence: "the PR in handoff.{prKey} has every commit of its base branch",
|
|
83
|
+
params: { prKey: { type: "text", label: "PR key", default: "prUrl" } },
|
|
84
|
+
reads: ["handoff", "github"],
|
|
85
|
+
async run(ctx, { prKey }) {
|
|
86
|
+
const url = prUrlOf(ctx, prKey);
|
|
87
|
+
if (!url) return;
|
|
88
|
+
const pr = await ctx.github.pr(url);
|
|
89
|
+
const base = pr.base && pr.base.ref;
|
|
90
|
+
if (!base || !pr.head || !pr.head.sha) {
|
|
91
|
+
fail(`Couldn't read PR #${pr.number}'s base branch or last commit`);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const c = await ctx.github.compare(base, pr.head.sha, { repo: pr.slug });
|
|
95
|
+
const n = c.behindBy;
|
|
96
|
+
expect(n, `PR #${pr.number}'s commits behind ${base}`).toBe(
|
|
97
|
+
0,
|
|
98
|
+
`PR #${pr.number} is ${n} commit${n === 1 ? "" : "s"} behind ${base}: update the branch (GitHub's "Update branch") and let CI run again`,
|
|
99
|
+
);
|
|
100
|
+
if (!n) note(`PR #${pr.number} has the latest ${base}`);
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
module.exports = { prOpen, ciPassed, upToDate };
|
package/helpers/index.d.ts
CHANGED
|
@@ -119,7 +119,7 @@ export const node: {
|
|
|
119
119
|
testScriptIsReal: Helper<TestScriptIsRealParams>;
|
|
120
120
|
newTestsAreRun: Helper<NewTestsAreRunParams>;
|
|
121
121
|
};
|
|
122
|
-
export const github: { prOpen: H; ciPassed: H };
|
|
122
|
+
export const github: { prOpen: H; ciPassed: H; upToDate: H };
|
|
123
123
|
export const tracker: { ticketExists: H; ticketHasLabel: H };
|
|
124
124
|
/** Every starter helper. */
|
|
125
125
|
export const all: H[];
|
package/helpers/node.js
CHANGED
|
@@ -254,9 +254,42 @@ const testScriptIsReal = defineHelper({
|
|
|
254
254
|
},
|
|
255
255
|
});
|
|
256
256
|
|
|
257
|
-
/**
|
|
257
|
+
/** "a,{b,c}" → ["a", "{b,c}"]: splits on commas outside a glob's {a,b}. */
|
|
258
|
+
function topLevelCommas(word) {
|
|
259
|
+
const out = [];
|
|
260
|
+
let depth = 0;
|
|
261
|
+
let cur = "";
|
|
262
|
+
for (const c of word) {
|
|
263
|
+
if (c === "{") depth++;
|
|
264
|
+
else if (c === "}" && depth > 0) depth--;
|
|
265
|
+
if (c === "," && depth === 0) {
|
|
266
|
+
out.push(cur);
|
|
267
|
+
cur = "";
|
|
268
|
+
} else cur += c;
|
|
269
|
+
}
|
|
270
|
+
out.push(cur);
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Are a word's { and } paired, in order? */
|
|
275
|
+
function bracesBalanced(word) {
|
|
276
|
+
let depth = 0;
|
|
277
|
+
for (const c of word) {
|
|
278
|
+
if (c === "{") depth++;
|
|
279
|
+
else if (c === "}" && --depth < 0) return false;
|
|
280
|
+
}
|
|
281
|
+
return depth === 0;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Glob-like words in a runner's text ("test/*.test.js", "src/**", "src/**\/*.{test,spec}.js"):
|
|
286
|
+
* what it finds by itself. A backslash (as in package.json's \\") ends a word. Commas inside
|
|
287
|
+
* a glob's {a,b} stay in the glob; a word whose braces don't pair (a fragment of code, not
|
|
288
|
+
* a glob) is left out.
|
|
289
|
+
*/
|
|
258
290
|
function globsIn(text) {
|
|
259
|
-
|
|
291
|
+
const words = String(text).split(/[\s"'`\\=()[\]]+/).flatMap(topLevelCommas);
|
|
292
|
+
return [...new Set(words.map((w) => w.replace(/^\.\//, "")).filter((w) => /[*?]/.test(w) && /[\w]/.test(w) && bracesBalanced(w)))];
|
|
260
293
|
}
|
|
261
294
|
|
|
262
295
|
const newTestsAreRun = defineHelper({
|
|
@@ -293,4 +326,4 @@ const newTestsAreRun = defineHelper({
|
|
|
293
326
|
},
|
|
294
327
|
});
|
|
295
328
|
|
|
296
|
-
module.exports = { testsPass, lintPasses, noNewDependencies, lockfileExplained, testScriptIsReal, newTestsAreRun, testCount, lockfileProblem, noOpScript };
|
|
329
|
+
module.exports = { testsPass, lintPasses, noNewDependencies, lockfileExplained, testScriptIsReal, newTestsAreRun, testCount, lockfileProblem, noOpScript, globsIn };
|
package/index.d.ts
CHANGED
|
@@ -105,6 +105,7 @@ export interface GitHub {
|
|
|
105
105
|
pr(urlOrNumber: string | number, opts?: { repo?: string }): Promise<any & { slug: string }>;
|
|
106
106
|
checksFor(sha: string, opts?: { repo?: string }): Promise<{ runs: CheckRun[]; source: "check-runs" | "actions" }>;
|
|
107
107
|
waitForChecks(sha: string, opts?: { timeoutSec?: number; noChecksAfterSec?: number; pollSec?: number; repo?: string }): Promise<WaitResult>;
|
|
108
|
+
compare(base: string, head: string, opts?: { repo?: string }): Promise<{ status: string; aheadBy: number; behindBy: number }>;
|
|
108
109
|
}
|
|
109
110
|
|
|
110
111
|
export interface Workspace {
|
package/lib/adapters/github.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* checksFor(sha, { repo }) { runs: [{ name, status, conclusion, url }], source }
|
|
7
7
|
* waitForChecks(sha, { timeoutSec, noChecksAfterSec, pollSec, repo })
|
|
8
8
|
* { runs, failed, pending, passed, noChecks, timedOut, waitedSec }
|
|
9
|
+
* compare(base, head, { repo }) { status, aheadBy, behindBy } of head against base
|
|
9
10
|
*
|
|
10
11
|
* Each call fails with GitHub's HTTP code and what to do about it.
|
|
11
12
|
*/
|
|
@@ -129,6 +130,17 @@ function makeGithub(opts) {
|
|
|
129
130
|
return { runs, source: "actions" };
|
|
130
131
|
},
|
|
131
132
|
|
|
133
|
+
async compare(base, head, { repo } = {}) {
|
|
134
|
+
if (!base || !head) throw new DashCheckError("No base or head to compare", { fix: "pass the PR's base branch and head sha" });
|
|
135
|
+
const slug = await repoSlug(repo);
|
|
136
|
+
const b = await need(
|
|
137
|
+
`repos/${slug}/compare/${encodeURIComponent(base)}...${encodeURIComponent(head)}`,
|
|
138
|
+
`comparing ${base} with ${String(head).slice(0, 12)} on ${slug}`,
|
|
139
|
+
slug,
|
|
140
|
+
);
|
|
141
|
+
return { status: b.status, aheadBy: b.ahead_by || 0, behindBy: b.behind_by || 0 };
|
|
142
|
+
},
|
|
143
|
+
|
|
132
144
|
async waitForChecks(sha, { timeoutSec = 1200, noChecksAfterSec = 180, pollSec = 15, repo } = {}) {
|
|
133
145
|
const started = now();
|
|
134
146
|
for (;;) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trops/dash-check",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Compose deterministic checks from small, described building blocks: Jest-style files, JSON recipes and helpers. Runs in Dash, in CI and on a laptop.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "trops",
|
|
@@ -10,7 +10,14 @@
|
|
|
10
10
|
},
|
|
11
11
|
"homepage": "https://github.com/trops/dash-check#readme",
|
|
12
12
|
"bugs": "https://github.com/trops/dash-check/issues",
|
|
13
|
-
"keywords": [
|
|
13
|
+
"keywords": [
|
|
14
|
+
"dash",
|
|
15
|
+
"checks",
|
|
16
|
+
"ci",
|
|
17
|
+
"testing",
|
|
18
|
+
"bots",
|
|
19
|
+
"deterministic"
|
|
20
|
+
],
|
|
14
21
|
"engines": {
|
|
15
22
|
"node": ">=18"
|
|
16
23
|
},
|