@chernabog/recipes 0.4.1 → 0.6.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.
@@ -13,6 +13,7 @@
13
13
 
14
14
  import {
15
15
  defineFlow,
16
+ Events,
16
17
  Implement,
17
18
  NeedsHuman,
18
19
  PullRequest,
@@ -23,46 +24,19 @@ import {
23
24
  Skip,
24
25
  type WorkItem,
25
26
  } from "@chernabog/sdk";
26
- import * as Arr from "effect/Array";
27
27
  import * as Effect from "effect/Effect";
28
28
  import * as Option from "effect/Option";
29
29
  import * as Schema from "effect/Schema";
30
30
 
31
- // Pull requests this persona wrote whose latest approval or request for
32
- // changes asks for changes on the current head, so nothing was pushed since.
31
+ // Pull requests this persona wrote whose record says changes asked, by a
32
+ // review on the current head, so nothing was pushed since. A review a person
33
+ // left moves the record there first.
33
34
  // A person has one labelled auto:needs-human, and a run of another flow one it
34
35
  // owns; both are left alone. Each is routed by its assignees and reviewers.
36
+ // Then pull requests whose fix a CI job pushed, ending the job while its CI
37
+ // ran: each is held until the checks are green or red.
35
38
  const trigger = Effect.gen(function* () {
36
- const labelNames = yield* Settings.labels;
37
- const items: Array<WorkItem> = [];
38
-
39
- for (const pull of yield* Review.authoredByMe) {
40
- if (pull.labels.includes(labelNames.needsHuman)) {
41
- continue;
42
- }
43
-
44
- const decisive = yield* Review.decisiveReviews(pull.number);
45
- const latest = Arr.last(decisive);
46
-
47
- if (
48
- !Option.exists(
49
- latest,
50
- (review) => review.state === "CHANGES_REQUESTED" && review.commitSha === pull.headSha,
51
- )
52
- ) {
53
- continue;
54
- }
55
-
56
- if (yield* Review.ownedByARun(pull)) {
57
- continue;
58
- }
59
-
60
- for (const review of Option.toArray(latest)) {
61
- items.push(yield* Review.routed(pull, Review.addressItem(pull, review, decisive.length)));
62
- }
63
- }
64
-
65
- return items;
39
+ return [...(yield* Review.changesToAddress), ...(yield* Review.fixesAwaitingCi)];
66
40
  }).pipe(reportAs("trigger"));
67
41
 
68
42
  const answerReview = (address: Review.AddressItem) =>
@@ -113,17 +87,55 @@ const answerReview = (address: Review.AddressItem) =>
113
87
  base: address.baseRef,
114
88
  withPerson: session.withPerson,
115
89
  });
90
+ // In a CI job, still-pending CI ends the job here, and the pull request
91
+ // comes back through Review.fixesAwaitingCi once CI finishes.
116
92
  yield* Implement.waitForCi(
117
93
  address.pullRequest,
118
94
  worktree,
119
95
  { ...session, branch: address.headRef, base: address.baseRef },
120
- { id: `review-${round}` },
96
+ { id: `review-${round}`, ...Review.parkOnPullRequest(address.pullRequest) },
121
97
  );
122
98
  yield* Review.askAgain(address.pullRequest, round, address.reviewer, outcome.summary);
123
99
 
124
100
  return `#${address.pullRequest}: addressed review ${round}; asked ${address.reviewer} to review again`;
125
101
  });
126
102
 
103
+ // The fix is pushed, and a CI job ended while its CI ran, which has finished:
104
+ // wait on from the round the record names (a fix of red CI resumes the
105
+ // author's session), then ask the reviewer again.
106
+ const resumeCi = (address: Review.AddressItem, resumed: Review.ResumingCi) =>
107
+ Effect.gen(function* () {
108
+ const review = yield* Review.changesAsked(address);
109
+ const round = review.round ?? 1;
110
+ const author = yield* Review.authorSession(address.pullRequest);
111
+
112
+ const session = {
113
+ sessionId: Option.some(author.sessionId),
114
+ adapter: Option.some(author.adapter),
115
+ withPerson: author.withPerson === true,
116
+ };
117
+
118
+ const worktree = yield* Review.prepareAuthorWorktree(address.headRef);
119
+ yield* Implement.waitForCi(
120
+ address.pullRequest,
121
+ worktree,
122
+ { ...session, branch: address.headRef, base: address.baseRef },
123
+ {
124
+ id: `review-${round}`,
125
+ ...Review.parkOnPullRequest(address.pullRequest),
126
+ firstRound: resumed.round,
127
+ },
128
+ );
129
+ yield* Review.askAgain(
130
+ address.pullRequest,
131
+ round,
132
+ address.reviewer,
133
+ "the commits pushed since that review. Their CI finished after the job that pushed them ended.",
134
+ );
135
+
136
+ return `#${address.pullRequest}: addressed review ${round}; asked ${address.reviewer} to review again`;
137
+ });
138
+
127
139
  const workflow = (item: WorkItem) =>
128
140
  Effect.gen(function* () {
129
141
  const address = yield* Review.addressItemOf(item);
@@ -133,7 +145,17 @@ const workflow = (item: WorkItem) =>
133
145
  // Anything that stops it hands the pull request to a person, unless it
134
146
  // closed meanwhile. It owns the pull request throughout, so no other run
135
147
  // pushes to the branch under it; one another run owns is skipped.
136
- return yield* answerReview(address).pipe(
148
+ const answering = Effect.gen(function* () {
149
+ const resumed = Review.resumingCi(address);
150
+
151
+ if (Option.isSome(resumed)) {
152
+ return yield* resumeCi(address, resumed.value);
153
+ }
154
+
155
+ return yield* answerReview(address);
156
+ });
157
+
158
+ return yield* answering.pipe(
137
159
  Effect.catchTags({
138
160
  NeedsHuman: (needsHuman) => toPerson(needsHuman.reason),
139
161
  StepFailed: (failed) => toPerson(`${failed.step}: ${failed.message}`),
@@ -160,7 +182,7 @@ const settings = Schema.Struct({
160
182
  ...Settings.Reviewers.fields,
161
183
  });
162
184
 
163
- export default defineFlow({
185
+ const flow = defineFlow({
164
186
  labels: Settings.declaredLabels,
165
187
  settings,
166
188
  trigger,
@@ -172,3 +194,12 @@ export default defineFlow({
172
194
  stillValid: Review.stillValid,
173
195
  workflow,
174
196
  });
197
+
198
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
199
+ // rather than only on the schedule: a review submitted, and a finished CI run,
200
+ // which frees a fix a CI job left waiting on its CI. Its trigger reads each
201
+ // pull request's phase, so a phase event wakes it too.
202
+ export default Events.declare(flow, {
203
+ events: [Events.reviewSubmitted, Events.ciCompleted],
204
+ readsPhases: true,
205
+ });
package/flows/epic.ts CHANGED
@@ -32,6 +32,7 @@ import {
32
32
  Claims,
33
33
  defineFlow,
34
34
  Epic,
35
+ Events,
35
36
  Git,
36
37
  GitHub,
37
38
  Implement,
@@ -902,7 +903,7 @@ const settings = Schema.Struct({
902
903
  ...Own.fields,
903
904
  });
904
905
 
905
- export default defineFlow({
906
+ const flow = defineFlow({
906
907
  trigger: Epic.queued.pipe(reportAs("trigger")),
907
908
  gate: Implement.gate,
908
909
  concurrency: 1,
@@ -929,3 +930,13 @@ export default defineFlow({
929
930
  return yield* workThrough(epic, ships, concurrency).pipe(Claims.releasing(Claims.epic(epic)));
930
931
  }),
931
932
  });
933
+
934
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
935
+ // rather than only on the schedule: an issue labelled auto:epic, and what moves
936
+ // an epic's layers (a task closed or relabelled, a push to, review of or merge
937
+ // of a layer's pull request). Its trigger reads the epic's phase, so a phase
938
+ // event wakes it too.
939
+ export default Events.declare(flow, {
940
+ events: [Events.issueLabelled, ...Events.layerChanged],
941
+ readsPhases: true,
942
+ });
@@ -24,6 +24,7 @@ import {
24
24
  type CiTimedOut,
25
25
  Claims,
26
26
  defineFlow,
27
+ Events,
27
28
  GitHub,
28
29
  Implement,
29
30
  NeedsHuman,
@@ -40,7 +41,9 @@ import * as Effect from "effect/Effect";
40
41
  import * as Option from "effect/Option";
41
42
  import * as Schema from "effect/Schema";
42
43
 
43
- // Issues labelled auto:eligible, then ones labelled auto:ship, the automated
44
+ // Issues whose run a CI job left waiting on its pull request's CI first, to
45
+ // finish what's started: each is held until the checks are green or red.
46
+ // Then issues labelled auto:eligible, then ones labelled auto:ship, the automated
44
47
  // path, whose pull request chernabog merges once it's approved. Oldest first,
45
48
  // but a post-merge follow-up, the fix for red CI on the base, before the rest;
46
49
  // with a board, only those whose Status is its readyStatus. An issue another
@@ -58,8 +61,9 @@ const trigger = Effect.gen(function* () {
58
61
  (issue) => !eligible.some((queued) => queued.number === issue.number),
59
62
  );
60
63
 
61
- // A follow-up fixing red CI on the base goes first, ahead of other work.
64
+ // A follow-up fixing red CI on the base goes first, ahead of other new work.
62
65
  return [
66
+ ...(yield* Implement.awaitingCi),
63
67
  ...(yield* Implement.queued(Implement.followUpsFirst(eligible))),
64
68
  ...(yield* Implement.queued(Implement.followUpsFirst(shipping), Option.some(labelNames.ship))),
65
69
  ...(yield* Implement.readied),
@@ -81,7 +85,7 @@ const settings = Schema.Struct({
81
85
  ...Settings.Reviewers.fields,
82
86
  });
83
87
 
84
- export default defineFlow({
88
+ const flow = defineFlow({
85
89
  trigger,
86
90
  gate: Implement.gate,
87
91
  concurrency: 1,
@@ -120,6 +124,30 @@ export default defineFlow({
120
124
  const claimed = yield* Implement.claim(issue, { ...queued, adopt: true });
121
125
  const { base, branch } = claimed;
122
126
  const adoption = Option.fromUndefinedOr(claimed.adopted);
127
+ const resuming = Implement.resumingCi(item);
128
+
129
+ // A CI job ended while this issue's pull request's CI ran, and it has
130
+ // finished: wait on from the round the record names (a fix of red CI
131
+ // starts a session of its own), then hand off.
132
+ if (Option.isSome(resuming)) {
133
+ const waited = resuming.value;
134
+ pullRequest = Option.some(waited.pullRequest);
135
+
136
+ return yield* Effect.gen(function* () {
137
+ yield* Implement.waitForCiMaking(
138
+ waited.pullRequest,
139
+ Implement.prepareWorktree(branch, { base }),
140
+ { ...Implement.thisRunsSession, branch, base },
141
+ { ...Implement.parkOnIssue(issue), firstRound: waited.round },
142
+ );
143
+
144
+ return yield* Implement.handOff(
145
+ issue,
146
+ { number: waited.pullRequest, url: waited.url },
147
+ Implement.resumedOutcome,
148
+ );
149
+ }).pipe(Effect.catchTags(forPerson));
150
+ }
123
151
 
124
152
  if (Option.isSome(adoption)) {
125
153
  const adopted = adoption.value;
@@ -168,11 +196,14 @@ export default defineFlow({
168
196
  yield* Implement.push("push", worktree, branch, { base });
169
197
  const opened = yield* Implement.openPullRequest(issue, outcome, { ...queued, base });
170
198
  pullRequest = Option.some(opened.number);
171
- yield* Implement.waitForCi(opened.number, worktree, {
172
- ...Implement.thisRunsSession,
173
- branch,
174
- base,
175
- });
199
+ // In a CI job, still-pending CI ends the job here, and the item
200
+ // comes back through Implement.awaitingCi once CI finishes.
201
+ yield* Implement.waitForCi(
202
+ opened.number,
203
+ worktree,
204
+ { ...Implement.thisRunsSession, branch, base },
205
+ Implement.parkOnIssue(issue),
206
+ );
176
207
 
177
208
  return yield* Implement.handOff(issue, opened, outcome);
178
209
  }).pipe(Effect.catchTags(forPerson));
@@ -189,3 +220,13 @@ export default defineFlow({
189
220
  Effect.catchTag("NotFound", (gone) => Effect.fail(new Skip({ reason: gone.message }))),
190
221
  ),
191
222
  });
223
+
224
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
225
+ // rather than only on the schedule: an issue labelled to queue it, a draft
226
+ // its author marks ready, which wakes an adopted one's run, and a finished CI
227
+ // run, which frees an issue a CI job left waiting on its pull request's CI.
228
+ // Its trigger reads issues' phases for those, so a phase event wakes it too.
229
+ export default Events.declare(flow, {
230
+ events: [Events.issueLabelled, Events.readyForReview, Events.ciCompleted],
231
+ readsPhases: true,
232
+ });
package/flows/plan.ts CHANGED
@@ -15,6 +15,7 @@
15
15
  import {
16
16
  Claims,
17
17
  defineFlow,
18
+ Events,
18
19
  GitHub,
19
20
  Implement,
20
21
  NeedsHuman,
@@ -134,7 +135,7 @@ const settings = Schema.Struct({
134
135
  ...Settings.Plans.fields,
135
136
  });
136
137
 
137
- export default defineFlow({
138
+ const flow = defineFlow({
138
139
  trigger,
139
140
  gate: Implement.gate,
140
141
  concurrency: 1,
@@ -144,3 +145,11 @@ export default defineFlow({
144
145
  stillValid: Plan.stillValid,
145
146
  workflow,
146
147
  });
148
+
149
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
150
+ // rather than only on the schedule: a spec labelled auto:plan, and a person's
151
+ // answer on one, which wakes its run.
152
+ export default Events.declare(flow, {
153
+ events: [Events.issueLabelled, Events.commentCreated],
154
+ readsPhases: false,
155
+ });
@@ -23,6 +23,7 @@ import {
23
23
  activity,
24
24
  type CiFailedRun,
25
25
  defineFlow,
26
+ Events,
26
27
  GitHub,
27
28
  type Json,
28
29
  NeedsHuman,
@@ -560,7 +561,7 @@ const settings = Schema.Struct({
560
561
  ...Own.fields,
561
562
  });
562
563
 
563
- export default defineFlow({
564
+ const flow = defineFlow({
564
565
  trigger,
565
566
  // No agent runs, so no usage gate. Watching is only polling, so several merges are watched at once.
566
567
  concurrency: 4,
@@ -570,3 +571,7 @@ export default defineFlow({
570
571
  settings,
571
572
  workflow,
572
573
  });
574
+
575
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
576
+ // rather than only on the schedule: a pull request closing, merged among them.
577
+ export default Events.declare(flow, { events: [Events.pullRequestClosed], readsPhases: false });
@@ -13,6 +13,7 @@
13
13
  import {
14
14
  activity,
15
15
  defineFlow,
16
+ Events,
16
17
  GitHub,
17
18
  PullRequest,
18
19
  Repo,
@@ -27,22 +28,13 @@ import * as Effect from "effect/Effect";
27
28
  import * as Option from "effect/Option";
28
29
  import * as Schema from "effect/Schema";
29
30
 
30
- // Pull requests that ask this persona again after it reviewed them, one item
31
- // per request, unless a person has them (auto:needs-human): asking again
32
- // during a dispute doesn't restart the loop. Each is routed by its assignees and reviewers.
31
+ // Pull requests whose record says a review is asked again of this persona,
32
+ // one item per request, unless a person has them (auto:needs-human): asking
33
+ // again during a dispute doesn't restart the loop. Each is routed by its
34
+ // assignees and reviewers.
33
35
  // And the runs holding a merge whose base branch is green again, to wake them.
34
36
  const trigger = Effect.gen(function* () {
35
- const labelNames = yield* Settings.labels;
36
-
37
- const asked = yield* Effect.forEach(
38
- (yield* Review.reviewRequests).filter(
39
- ({ pull, reviewed }) => reviewed > 0 && !pull.labels.includes(labelNames.needsHuman),
40
- ),
41
- ({ pull, reviewed }) =>
42
- Review.routed(pull, Review.reviewItem(pull, `pr-${pull.number}-review-${reviewed + 1}`)),
43
- );
44
-
45
- return [...asked, ...(yield* PullRequest.baseTurnedGreen)];
37
+ return [...(yield* Review.requestedAgain), ...(yield* PullRequest.baseTurnedGreen)];
46
38
  }).pipe(reportAs("trigger"));
47
39
 
48
40
  const MergeState = Schema.Struct({ mergeable_state: Schema.String });
@@ -200,7 +192,7 @@ const settings = Schema.Struct({
200
192
  ...Settings.MergeHold.fields,
201
193
  });
202
194
 
203
- export default defineFlow({
195
+ const flow = defineFlow({
204
196
  labels: Settings.declaredLabels,
205
197
  settings,
206
198
  trigger,
@@ -213,3 +205,12 @@ export default defineFlow({
213
205
  stillValid: Review.stillValid,
214
206
  workflow,
215
207
  });
208
+
209
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
210
+ // rather than only on the schedule: a review asked again, and CI finishing on a
211
+ // base, which wakes a run holding a merge. Its trigger reads each pull
212
+ // request's phase, so a phase event wakes it too.
213
+ export default Events.declare(flow, {
214
+ events: [Events.reviewRequestedAgain, Events.ciCompleted],
215
+ readsPhases: true,
216
+ });
@@ -33,6 +33,7 @@ import {
33
33
  activity,
34
34
  Agent,
35
35
  defineFlow,
36
+ Events,
36
37
  GitHub,
37
38
  Implement,
38
39
  type Json,
@@ -49,7 +50,6 @@ import {
49
50
  StepFailed,
50
51
  type WorkItem,
51
52
  } from "@chernabog/sdk";
52
- import * as Arr from "effect/Array";
53
53
  import * as Effect from "effect/Effect";
54
54
  import * as Option from "effect/Option";
55
55
  import * as Schema from "effect/Schema";
@@ -190,28 +190,6 @@ const resolve = (
190
190
  Resolution,
191
191
  );
192
192
 
193
- // When the pull request's latest approval or request for changes is an
194
- // approval, asks its reviewer to review again.
195
- const askApproverAgain = (pullRequest: number) =>
196
- activity(
197
- "ask-approver-again",
198
- Effect.gen(function* () {
199
- const approval = Option.filter(
200
- Arr.last(yield* Review.decisiveReviews(pullRequest)),
201
- (review) => review.state === "APPROVED",
202
- );
203
-
204
- if (Option.isNone(approval)) {
205
- return Option.none<string>();
206
- }
207
-
208
- yield* (yield* GitHub).requestReviewers(pullRequest, [approval.value.author]);
209
-
210
- return Option.some(approval.value.author);
211
- }),
212
- Schema.Option(Schema.String),
213
- );
214
-
215
193
  // Resolves the conflict, after recording a squash- or rebase-merged layer
216
194
  // below as already here when there is one. That record is a merge the flow
217
195
  // makes (Stack.syncBase), and the agent resolves what it leaves in progress.
@@ -319,7 +297,7 @@ const unstick = (conflict: ConflictItem) =>
319
297
 
320
298
  // An approval was of the code before the merge: its reviewer looks again,
321
299
  // and re-review merges an auto:ship pull request it approves.
322
- const approver = yield* askApproverAgain(conflict.pullRequest);
300
+ const approver = yield* Review.askApproverAgain(conflict.pullRequest);
323
301
 
324
302
  return Option.match(approver, {
325
303
  onNone: () =>
@@ -363,7 +341,7 @@ const settings = Schema.Struct({
363
341
  ...Settings.Ci.fields,
364
342
  });
365
343
 
366
- export default defineFlow({
344
+ const flow = defineFlow({
367
345
  labels: Settings.declaredLabels,
368
346
  settings,
369
347
  trigger,
@@ -375,3 +353,8 @@ export default defineFlow({
375
353
  stillValid: Review.stillValid,
376
354
  workflow,
377
355
  });
356
+
357
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
358
+ // rather than only on the schedule: a pull request closing, which may move the
359
+ // base under every other one.
360
+ export default Events.declare(flow, { events: [Events.baseMoved], readsPhases: false });
package/flows/review.ts CHANGED
@@ -14,6 +14,7 @@
14
14
  import {
15
15
  activity,
16
16
  defineFlow,
17
+ Events,
17
18
  GitHub,
18
19
  PullRequest,
19
20
  Repo,
@@ -28,21 +29,19 @@ import * as Effect from "effect/Effect";
28
29
  import * as Option from "effect/Option";
29
30
  import * as Schema from "effect/Schema";
30
31
 
31
- // Pull requests that ask this persona for a review, which it hasn't reviewed
32
- // yet, routed by their assignees and reviewers, those waiting on a reviewer
32
+ // Pull requests whose record says they wait for their first review, asked of
33
+ // this persona, routed by their assignees and reviewers (one whose record
34
+ // GitHub disagrees with is held, for status to show why), those waiting on a reviewer
33
35
  // no persona on this machine acts as, which status lists, and the runs
34
36
  // holding a merge whose base branch is green again, to wake them. Reading
35
37
  // review requests needs the persona's seat in the reviewer pool, which
36
38
  // chernabog checks before the flow runs.
37
39
  const trigger = Effect.gen(function* () {
38
- const found = yield* Review.reviewRequests;
39
-
40
- const mine = yield* Effect.forEach(
41
- found.filter(({ reviewed }) => reviewed === 0),
42
- ({ pull }) => Review.routed(pull, Review.reviewItem(pull, `pr-${pull.number}`)),
43
- );
44
-
45
- return [...mine, ...(yield* Review.waitingElsewhere), ...(yield* PullRequest.baseTurnedGreen)];
40
+ return [
41
+ ...(yield* Review.requestedFirst),
42
+ ...(yield* Review.waitingElsewhere),
43
+ ...(yield* PullRequest.baseTurnedGreen),
44
+ ];
46
45
  }).pipe(reportAs("trigger"));
47
46
 
48
47
  const MergeState = Schema.Struct({ mergeable_state: Schema.String });
@@ -129,7 +128,7 @@ const settings = Schema.Struct({
129
128
  ...Settings.MergeHold.fields,
130
129
  });
131
130
 
132
- export default defineFlow({
131
+ const flow = defineFlow({
133
132
  labels: Settings.declaredLabels,
134
133
  settings,
135
134
  trigger,
@@ -142,3 +141,12 @@ export default defineFlow({
142
141
  stillValid: Review.stillValid,
143
142
  workflow,
144
143
  });
144
+
145
+ // The GitHub events that can change what its trigger finds, so it wakes in CI
146
+ // rather than only on the schedule: a review asked of someone, and CI finishing
147
+ // on a base, which wakes a run holding a merge. Its trigger reads each pull
148
+ // request's phase, so a phase event wakes it too.
149
+ export default Events.declare(flow, {
150
+ events: [Events.reviewRequested, Events.ciCompleted],
151
+ readsPhases: true,
152
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chernabog/recipes",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "license": "MIT",
5
5
  "description": "Chernabog's recipes: complete flows a repo copies in, or imports, written from @chernabog/sdk",
6
6
  "repository": {
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "peerDependencies": {
18
18
  "@chernabog/core": "^1.1.0",
19
- "@chernabog/sdk": ">=0.10.0 <0.12.0"
19
+ "@chernabog/sdk": ">=0.13.0 <0.14.0"
20
20
  },
21
21
  "chernabog": {
22
22
  "recipes": {