@trops/dash-check 0.3.2 → 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 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 33, 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.
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
 
@@ -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
- module.exports = { prOpen, ciPassed };
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 };
@@ -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/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 {
@@ -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.2",
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",