@chernabog/sdk 0.10.0 → 0.11.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.
@@ -780,6 +780,23 @@ export const handToPerson = (pullRequest: number, reason: string) =>
780
780
  }),
781
781
  ).pipe(Effect.andThen(Effect.fail(new NeedsHuman({ reason: `#${pullRequest}: ${reason}` }))));
782
782
 
783
+ // Gives the pull request to a person as the next ordinary step, such as
784
+ // merging it: labels it by the flow's ready-for-person label and comments
785
+ // `message`. Unlike handToPerson, the run goes on, and can end done.
786
+ export const readyForPerson = (pullRequest: number, message: string) =>
787
+ activity(
788
+ `ready-for-person-pr-${pullRequest}`,
789
+ Effect.gen(function* () {
790
+ const github = yield* GitHub;
791
+ const label = yield* Settings.readyForPerson;
792
+ yield* github.addLabel(pullRequest, label);
793
+ yield* github.comment(pullRequest, message);
794
+
795
+ return `#${pullRequest}: labelled ${label}; ${message}`;
796
+ }),
797
+ Schema.String,
798
+ );
799
+
783
800
  // Whether chernabog may merge the pull request, as `ships` decides it,
784
801
  // read once in an activity so a replay decides the same.
785
802
  export const shipping = (pullRequest: number) =>
@@ -0,0 +1,234 @@
1
+ // The re-review's blocks: finding this persona's own earlier review, capping
2
+ // the re-reviews, re-checking the earlier findings in the reviewer's own
3
+ // session, and handing the pull request to a person with a reason.
4
+
5
+ import { reportAs } from "@chernabog/core";
6
+ import * as Effect from "effect/Effect";
7
+ import * as Option from "effect/Option";
8
+
9
+ import { Approved, defineBlock, defineSource, type PullRequestNumber } from "./block.ts";
10
+ import * as PullRequest from "./pull-request.ts";
11
+ import * as Review from "./review.ts";
12
+ import * as Routing from "./routing.ts";
13
+ import * as Settings from "./settings.ts";
14
+
15
+ // An earlier review this persona can resume: one with the session it ran in.
16
+ export type Resumable = Review.ReviewResult & { readonly sessionId: string };
17
+
18
+ const resumable = (review: Review.ReviewResult): Option.Option<Resumable> =>
19
+ Option.map(Option.fromUndefinedOr(review.sessionId), (sessionId) => ({ ...review, sessionId }));
20
+
21
+ const reReviews = (count: number) => {
22
+ if (count === 1) {
23
+ return "1 re-review";
24
+ }
25
+
26
+ return `${count} re-reviews`;
27
+ };
28
+
29
+ const nonBlocking = (finding: Review.Finding) => {
30
+ if (finding.blocking === false) {
31
+ return " (doesn't block the merge)";
32
+ }
33
+
34
+ return "";
35
+ };
36
+
37
+ const listed = (findings: ReadonlyArray<Review.Finding>, remediated: boolean) =>
38
+ findings.flatMap((finding) =>
39
+ Option.toArray(
40
+ Option.liftPredicate(
41
+ `- ${finding.finding}${nonBlocking(finding)}`,
42
+ () => finding.remediated === remediated,
43
+ ),
44
+ ),
45
+ );
46
+
47
+ // The comment that says which of review `round`'s findings are remediated.
48
+ const reReviewed = (round: number, findings: ReadonlyArray<Review.Finding>) =>
49
+ [
50
+ `Review ${round}'s findings, re-reviewed.`,
51
+ "",
52
+ "Remediated:",
53
+ ...listed(findings, true),
54
+ "",
55
+ "Still open:",
56
+ ...listed(findings, false),
57
+ ].join("\n");
58
+
59
+ export const reReviewBlocks = {
60
+ "review.latestOwn": defineBlock({
61
+ name: "review.latestOwn",
62
+ about:
63
+ "Finds this persona's latest review of the pull request, from the marker on it, to resume. It comes to none when there is no review of its own with a session to resume, such as one posted before markers named their reviewer: review afresh with review.round then.",
64
+ inputKinds: { pr: "PullRequestNumber" },
65
+ outputKinds: { review: "Option<the earlier review, with its session>" },
66
+ outcomeKinds: {
67
+ found:
68
+ "the earlier review, with its round and the session to resume: for review.roundsLeft and review.recheck",
69
+ none: "nothing",
70
+ },
71
+ inputs: { pr: "required" },
72
+ run: (input: { readonly pr: PullRequestNumber }) =>
73
+ Review.latestOwnReview(input.pr).pipe(
74
+ Effect.map((earlier) => ({ review: Option.flatMap(earlier, resumable) })),
75
+ ),
76
+ failures: { NeedsHuman: "told", Skip: "skip", StepFailed: "fail" },
77
+ outcomes: {
78
+ found: (latest: { readonly review: Option.Option<Resumable> }) => latest.review,
79
+ none: (latest: { readonly review: Option.Option<Resumable> }) =>
80
+ Option.liftPredicate({}, () => Option.isNone(latest.review)),
81
+ },
82
+ mustRoute: ["found", "none"],
83
+ exhaustive: true,
84
+ contributes: {},
85
+ }),
86
+
87
+ "review.roundsLeft": defineBlock({
88
+ name: "review.roundsLeft",
89
+ about:
90
+ "Counts the re-reviews the pull request has had against reviewRounds (1 by default, set in config.yaml). It comes to exhausted when none are left, with the reason to hand the pull request to a person by, without reviewing again.",
91
+ inputKinds: { review: "the earlier review, from review.latestOwn's found" },
92
+ outputKinds: { reReviews: "how many re-reviews it has had", limit: "reviewRounds" },
93
+ outcomeKinds: {
94
+ exhausted: "a reason, for pr.handToPerson: how many re-reviews it had, and the limit",
95
+ },
96
+ before: ["review.recheck"],
97
+ inputs: { review: "required" },
98
+ run: (input: { readonly review: Resumable }) =>
99
+ Effect.gen(function* () {
100
+ const { rounds } = yield* Settings.reviewers;
101
+
102
+ return { reReviews: (input.review.round ?? 1) - 1, limit: rounds };
103
+ }),
104
+ failures: {},
105
+ outcomes: {
106
+ exhausted: (count: { readonly reReviews: number; readonly limit: number }) =>
107
+ Option.liftPredicate(
108
+ `asked to review again after ${reReviews(count.reReviews)}, and reviewRounds is ${count.limit}, so a person decides. Review ${count.reReviews + 1}'s findings are in its review above.`,
109
+ () => count.reReviews >= count.limit,
110
+ ),
111
+ },
112
+ mustRoute: ["exhausted"],
113
+ stepNames: [],
114
+ contributes: { settings: Settings.Reviewers.fields },
115
+ }),
116
+
117
+ "review.recheck": defineBlock({
118
+ name: "review.recheck",
119
+ about:
120
+ "Resumes the reviewer's session of the earlier review with the change since, posts the next review, and comments which of the earlier review's findings are remediated and which are still open. A finding that doesn't block the merge never requests changes.",
121
+ inputKinds: {
122
+ pr: "PullRequestNumber",
123
+ review: "the earlier review, from review.latestOwn's found",
124
+ },
125
+ outputKinds: {
126
+ verdict: "approve or request-changes",
127
+ round: "the new review's round",
128
+ },
129
+ outcomeKinds: {
130
+ approve: "an object: with as: verdict, verdict.approved is the Approved proof for pr.land",
131
+ changes:
132
+ "an object: with as: open, open.reason says the review still requests changes, for pr.handToPerson",
133
+ },
134
+ inputs: { pr: "required", review: "required" },
135
+ run: (input: { readonly pr: PullRequestNumber; readonly review: Resumable }) =>
136
+ Effect.gen(function* () {
137
+ const rechecked = yield* Review.recheck(input.pr, input.review);
138
+ yield* Review.doneReviewing(input.pr);
139
+ yield* PullRequest.comment(
140
+ input.pr,
141
+ reReviewed(input.review.round ?? 1, rechecked.findings),
142
+ );
143
+
144
+ return { verdict: rechecked.verdict, round: rechecked.round ?? 2 };
145
+ }),
146
+ failures: {
147
+ NeedsHuman: "told",
148
+ NotFound: "skip",
149
+ PullRequestClosed: "skip",
150
+ Skip: "skip",
151
+ StepFailed: "fail",
152
+ },
153
+ outcomes: {
154
+ approve: (review: { readonly verdict: string }) =>
155
+ Option.liftPredicate({ approved: new Approved({}) }, () => review.verdict === "approve"),
156
+ changes: (review: { readonly verdict: string; readonly round: number }) =>
157
+ Option.liftPredicate(
158
+ { reason: `review ${review.round} still requests changes after a round of fixes` },
159
+ () => review.verdict !== "approve",
160
+ ),
161
+ },
162
+ mustRoute: ["approve", "changes"],
163
+ exhaustive: true,
164
+ contributes: { settings: { ...Settings.Reviewers.fields, ...Settings.Rules.fields } },
165
+ }),
166
+
167
+ "pr.handToPerson": defineBlock({
168
+ name: "pr.handToPerson",
169
+ either: ["reason"],
170
+ about:
171
+ "Labels the pull request auto:needs-human (the repo's needs-human label) and comments why a person must take it over. Use it when the run couldn't do its part: a cap, a rule or a dispute stopped it, and the run ends needs-human. Its output is the reason to end with: follow it with end: needs-human. A way that ends so owes no hand-off, so it can follow pr.open in place of issue.handOff to give every pull request to a person. When the person is only asked to do the next ordinary thing, such as merge an approved pull request, use pr.readyForPerson, and end done.",
172
+ inputKinds: {
173
+ pr: "PullRequestNumber",
174
+ reason: "why, as text a person reads on the pull request",
175
+ },
176
+ outputKinds: "the reason, for `end: needs-human`",
177
+ outcomeKinds: {},
178
+ inputs: { pr: "required", reason: "required" },
179
+ run: (input: { readonly pr: PullRequestNumber; readonly reason: string }) =>
180
+ PullRequest.handToPerson(input.pr, input.reason).pipe(
181
+ Effect.catchTag("NeedsHuman", (told) => Effect.succeed(told.reason)),
182
+ ),
183
+ failures: { Skip: "skip", StepFailed: "fail" },
184
+ outcomes: {},
185
+ mustRoute: [],
186
+ contributes: {},
187
+ }),
188
+
189
+ "pr.readyForPerson": defineBlock({
190
+ name: "pr.readyForPerson",
191
+ fixed: ["message"],
192
+ about:
193
+ "Gives the pull request to a person as the run's success: labels it auto:ready-for-person (or the label the flow's settings name, labels.readyForPerson) and comments message, which says what they do next. Use it when the person is only asked to do the next ordinary thing, such as merge an approved pull request, and follow it with end: done. It hands off the pull request pr.open opens, in place of issue.handOff, and merges nothing. When a person must take over because the run couldn't do its part, use pr.handToPerson and end: needs-human.",
194
+ inputKinds: {
195
+ pr: "PullRequestNumber",
196
+ message: "text a person reads on the pull request: what they do next, such as merge it",
197
+ },
198
+ outputKinds: "a summary, for `end`'s summary",
199
+ outcomeKinds: {},
200
+ closes: ["hand-off"],
201
+ inputs: { pr: "required", message: "required" },
202
+ run: (input: { readonly pr: PullRequestNumber; readonly message: string }) =>
203
+ PullRequest.readyForPerson(input.pr, input.message),
204
+ failures: { NeedsHuman: "told", Skip: "skip", StepFailed: "fail" },
205
+ outcomes: {},
206
+ mustRoute: [],
207
+ contributes: { namedLabels: [Settings.readyForPersonLabel] },
208
+ }),
209
+ };
210
+
211
+ export const reReviewSources = {
212
+ // Pull requests that ask this persona again after it reviewed them, one item
213
+ // per request, unless a person has them (auto:needs-human): asking again
214
+ // during a dispute doesn't restart the loop. Each item's key carries the
215
+ // review it asks for, `pr-7-review-2`, so each request is a run of its own.
216
+ "review.requestedAgain": defineSource("review.requestedAgain", "pull-request", () => ({
217
+ trigger: Effect.gen(function* () {
218
+ const labelNames = yield* Settings.labels;
219
+
220
+ return yield* Effect.forEach(
221
+ (yield* Review.reviewRequests).filter(
222
+ ({ pull, reviewed }) => reviewed > 0 && !pull.labels.includes(labelNames.needsHuman),
223
+ ),
224
+ ({ pull, reviewed }) =>
225
+ Review.routed(pull, Review.reviewItem(pull, `pr-${pull.number}-review-${reviewed + 1}`)),
226
+ );
227
+ }).pipe(reportAs("trigger")),
228
+ contributes: {
229
+ settings: Routing.Settings.fields,
230
+ doctor: [Review.reviewerCheck],
231
+ ownWork: true,
232
+ },
233
+ })),
234
+ };