@chernabog/sdk 0.4.0 → 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.
- package/README.md +32 -26
- package/package.json +1 -1
- package/src/comment.ts +6 -6
- package/src/default-prompts.ts +50 -0
- package/src/errors.ts +209 -20
- package/src/flow.ts +217 -62
- package/src/hybrid.ts +286 -0
- package/src/implement.ts +422 -171
- package/src/index.ts +15 -14
- package/src/issue.ts +2 -2
- package/src/json.ts +7 -4
- package/src/markers.ts +48 -15
- package/src/paths.ts +18 -10
- package/src/prompt.ts +36 -14
- package/src/pull-request.ts +176 -40
- package/src/review.ts +413 -300
- package/src/roles.ts +15 -8
- package/src/services.ts +179 -51
- package/src/stage.ts +4 -4
package/src/index.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import * as IssueSteps from
|
|
2
|
-
import * as PullRequestSteps from
|
|
3
|
-
import type * as Services from
|
|
1
|
+
import * as IssueSteps from "./issue.ts";
|
|
2
|
+
import * as PullRequestSteps from "./pull-request.ts";
|
|
3
|
+
import type * as Services from "./services.ts";
|
|
4
4
|
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * as
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
11
|
-
export * from
|
|
12
|
-
export *
|
|
13
|
-
export * from
|
|
14
|
-
export * from
|
|
15
|
-
export * from
|
|
5
|
+
export * from "./errors.ts";
|
|
6
|
+
export * from "./flow.ts";
|
|
7
|
+
export * as Hybrid from "./hybrid.ts";
|
|
8
|
+
export * as Implement from "./implement.ts";
|
|
9
|
+
export * from "./json.ts";
|
|
10
|
+
export * from "./markers.ts";
|
|
11
|
+
export * from "./paths.ts";
|
|
12
|
+
export * from "./prompt.ts";
|
|
13
|
+
export * as Review from "./review.ts";
|
|
14
|
+
export * from "./roles.ts";
|
|
15
|
+
export * from "./services.ts";
|
|
16
|
+
export * from "./stage.ts";
|
|
16
17
|
|
|
17
18
|
// PullRequest and Issue are each both a type and the steps on one.
|
|
18
19
|
export const PullRequest = PullRequestSteps;
|
package/src/issue.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Steps on an issue that any flow can use.
|
|
2
2
|
|
|
3
|
-
import { commentStep } from
|
|
3
|
+
import { commentStep } from "./comment.ts";
|
|
4
4
|
|
|
5
5
|
// Comments on the issue, and does nothing else.
|
|
6
|
-
export const comment = (issue: number, body: string) => commentStep(
|
|
6
|
+
export const comment = (issue: number, body: string) => commentStep("issue", issue, body);
|
package/src/json.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// JSON at the edges: every parse and print goes through a Schema codec, so a
|
|
2
2
|
// value is checked the moment it crosses the boundary.
|
|
3
3
|
|
|
4
|
-
import * as Option from
|
|
5
|
-
import * as Schema from
|
|
4
|
+
import * as Option from "effect/Option";
|
|
5
|
+
import * as Schema from "effect/Schema";
|
|
6
6
|
|
|
7
7
|
export type Json = Schema.Json;
|
|
8
8
|
|
|
@@ -14,5 +14,8 @@ export const parseJson = (text: string): Option.Option<Json> => Schema.decodeOpt
|
|
|
14
14
|
export const printJson = (value: Json): string => Schema.encodeSync(JsonText)(value);
|
|
15
15
|
|
|
16
16
|
// Each line of a JSON Lines file that the schema accepts; the others are skipped.
|
|
17
|
-
export const decodeJsonLines = <A>(
|
|
18
|
-
|
|
17
|
+
export const decodeJsonLines = <A>(
|
|
18
|
+
schema: Schema.Codec<A, string>,
|
|
19
|
+
text: string,
|
|
20
|
+
): ReadonlyArray<A> =>
|
|
21
|
+
text.split("\n").flatMap((line) => Option.toArray(Schema.decodeOption(schema)(line)));
|
package/src/markers.ts
CHANGED
|
@@ -1,17 +1,36 @@
|
|
|
1
1
|
// Hidden markers in pull request comments, so a later run can find what an
|
|
2
2
|
// earlier one left: who wrote the pull request, and what each review saw.
|
|
3
3
|
|
|
4
|
-
import * as Arr from
|
|
5
|
-
import * as Option from
|
|
6
|
-
import * as Schema from
|
|
4
|
+
import * as Arr from "effect/Array";
|
|
5
|
+
import * as Option from "effect/Option";
|
|
6
|
+
import * as Schema from "effect/Schema";
|
|
7
7
|
|
|
8
|
-
import type { Json } from
|
|
8
|
+
import type { Json } from "./json.ts";
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
// The session that wrote a pull request. withPerson: a person worked in it
|
|
11
|
+
// with the agent, on the hybrid path, so answering a review reopens it in
|
|
12
|
+
// front of them.
|
|
13
|
+
export const SessionMarker = Schema.Struct({
|
|
14
|
+
adapter: Schema.String,
|
|
15
|
+
sessionId: Schema.String,
|
|
16
|
+
issue: Schema.Finite,
|
|
17
|
+
branch: Schema.String,
|
|
18
|
+
withPerson: Schema.optionalKey(Schema.Boolean),
|
|
19
|
+
});
|
|
11
20
|
|
|
12
21
|
export type SessionMarker = typeof SessionMarker.Type;
|
|
13
22
|
|
|
14
|
-
|
|
23
|
+
// What a review saw and said, and who gave it in which session, so a
|
|
24
|
+
// re-review resumes it. reviewer, sessionId and adapter are optional only so
|
|
25
|
+
// that markers posted before they existed still read back.
|
|
26
|
+
export const ReviewMarker = Schema.Struct({
|
|
27
|
+
sha: Schema.String,
|
|
28
|
+
round: Schema.Finite,
|
|
29
|
+
verdict: Schema.Literals(["approve", "request-changes"]),
|
|
30
|
+
reviewer: Schema.optionalKey(Schema.String),
|
|
31
|
+
sessionId: Schema.optionalKey(Schema.String),
|
|
32
|
+
adapter: Schema.optionalKey(Schema.String),
|
|
33
|
+
});
|
|
15
34
|
|
|
16
35
|
export type ReviewMarker = typeof ReviewMarker.Type;
|
|
17
36
|
|
|
@@ -22,25 +41,39 @@ const marker = <A>(name: string, schema: Schema.Codec<A, Json>) => {
|
|
|
22
41
|
return {
|
|
23
42
|
write: (value: A) => `<!-- chernabog-${name} ${Schema.encodeSync(codec)(value)} -->`,
|
|
24
43
|
read: (body: string): Option.Option<A> =>
|
|
25
|
-
Option.fromNullishOr(body.match(pattern)).pipe(
|
|
44
|
+
Option.fromNullishOr(body.match(pattern)).pipe(
|
|
45
|
+
Option.flatMap((match) => Schema.decodeOption(codec)(match[1])),
|
|
46
|
+
),
|
|
26
47
|
};
|
|
27
48
|
};
|
|
28
49
|
|
|
29
|
-
|
|
50
|
+
// withPerson: claimed by a person's `chernabog start`, which another start of the issue may take over.
|
|
51
|
+
export const ClaimMarker = Schema.Struct({
|
|
52
|
+
runId: Schema.String,
|
|
53
|
+
claimedAt: Schema.String,
|
|
54
|
+
branch: Schema.String,
|
|
55
|
+
withPerson: Schema.optionalKey(Schema.Boolean),
|
|
56
|
+
});
|
|
30
57
|
|
|
31
58
|
export type ClaimMarker = typeof ClaimMarker.Type;
|
|
32
59
|
|
|
33
60
|
// A run that owns a pull request, or let it go: the standalone review and fix
|
|
34
61
|
// flows leave a pull request alone while a run of another flow owns it.
|
|
35
|
-
export const OwnerMarker = Schema.Struct({
|
|
62
|
+
export const OwnerMarker = Schema.Struct({
|
|
63
|
+
flow: Schema.String,
|
|
64
|
+
runId: Schema.String,
|
|
65
|
+
released: Schema.Boolean,
|
|
66
|
+
});
|
|
36
67
|
|
|
37
68
|
export type OwnerMarker = typeof OwnerMarker.Type;
|
|
38
69
|
|
|
39
|
-
export const claimMarker = marker(
|
|
40
|
-
export const sessionMarker = marker(
|
|
41
|
-
export const reviewMarker = marker(
|
|
42
|
-
export const ownerMarker = marker(
|
|
70
|
+
export const claimMarker = marker("claim", ClaimMarker);
|
|
71
|
+
export const sessionMarker = marker("session", SessionMarker);
|
|
72
|
+
export const reviewMarker = marker("review", ReviewMarker);
|
|
73
|
+
export const ownerMarker = marker("owner", OwnerMarker);
|
|
43
74
|
|
|
44
75
|
// The newest marker among comment bodies, oldest first.
|
|
45
|
-
export const latestMarker = <A>(
|
|
46
|
-
|
|
76
|
+
export const latestMarker = <A>(
|
|
77
|
+
read: (body: string) => Option.Option<A>,
|
|
78
|
+
bodies: ReadonlyArray<string>,
|
|
79
|
+
): Option.Option<A> => Arr.last(bodies.flatMap((body) => Option.toArray(read(body))));
|
package/src/paths.ts
CHANGED
|
@@ -1,32 +1,40 @@
|
|
|
1
1
|
// Path rules such as humanOnlyPaths: `.github/**`, `**/package.json`, `RELEASE.md`.
|
|
2
2
|
|
|
3
|
-
const escape = (text: string) => text.replace(/[.+^${}()|[\]\\]/g,
|
|
3
|
+
const escape = (text: string) => text.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
4
4
|
|
|
5
5
|
const doubleStar = (glob: string, index: number) => {
|
|
6
|
-
if (glob[index + 2] ===
|
|
6
|
+
if (glob[index + 2] === "/") {
|
|
7
|
+
return { pattern: "(?:.*/)?", skip: 2 };
|
|
8
|
+
}
|
|
7
9
|
|
|
8
|
-
return { pattern:
|
|
10
|
+
return { pattern: ".*", skip: 1 };
|
|
9
11
|
};
|
|
10
12
|
|
|
11
13
|
export const globToRegExp = (glob: string) => {
|
|
12
|
-
let pattern =
|
|
14
|
+
let pattern = "";
|
|
13
15
|
|
|
14
16
|
for (let index = 0; index < glob.length; index++) {
|
|
15
17
|
const char = glob[index];
|
|
16
18
|
|
|
17
|
-
if (char ===
|
|
19
|
+
if (char === "*" && glob[index + 1] === "*") {
|
|
18
20
|
const star = doubleStar(glob, index);
|
|
19
21
|
pattern += star.pattern;
|
|
20
22
|
index += star.skip;
|
|
21
|
-
} else if (char ===
|
|
22
|
-
|
|
23
|
-
else
|
|
23
|
+
} else if (char === "*") {
|
|
24
|
+
pattern += "[^/]*";
|
|
25
|
+
} else if (char === "?") {
|
|
26
|
+
pattern += "[^/]";
|
|
27
|
+
} else {
|
|
28
|
+
pattern += escape(char);
|
|
29
|
+
}
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
// A directory rule covers everything under it.
|
|
27
|
-
let rest =
|
|
33
|
+
let rest = "(?:/.*)?";
|
|
28
34
|
|
|
29
|
-
if (glob.endsWith(
|
|
35
|
+
if (glob.endsWith("/")) {
|
|
36
|
+
rest = ".*";
|
|
37
|
+
}
|
|
30
38
|
|
|
31
39
|
return new RegExp(`^${pattern}${rest}$`);
|
|
32
40
|
};
|
package/src/prompt.ts
CHANGED
|
@@ -1,29 +1,38 @@
|
|
|
1
1
|
// Prompts are agent-neutral Markdown in .chernabog/prompts/, filled in with
|
|
2
2
|
// {{name}} placeholders. A list renders as one `- ` line per item.
|
|
3
3
|
|
|
4
|
-
import * as Effect from
|
|
5
|
-
import * as FileSystem from
|
|
6
|
-
import * as Option from
|
|
7
|
-
import * as Predicate from
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as FileSystem from "effect/FileSystem";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
7
|
+
import * as Predicate from "effect/Predicate";
|
|
8
8
|
|
|
9
|
-
import { StepFailed } from
|
|
10
|
-
import { Repo } from
|
|
9
|
+
import { StepFailed } from "./errors.ts";
|
|
10
|
+
import { Repo } from "./services.ts";
|
|
11
11
|
|
|
12
12
|
export type PromptValue = string | number | ReadonlyArray<string> | Option.Option<string>;
|
|
13
13
|
|
|
14
14
|
const render = (value: PromptValue): string => {
|
|
15
|
-
if (Option.isOption(value))
|
|
15
|
+
if (Option.isOption(value)) {
|
|
16
|
+
return Option.getOrElse(value, () => "");
|
|
17
|
+
}
|
|
16
18
|
|
|
17
|
-
if (Predicate.isString(value) || Predicate.isNumber(value))
|
|
19
|
+
if (Predicate.isString(value) || Predicate.isNumber(value)) {
|
|
20
|
+
return String(value);
|
|
21
|
+
}
|
|
18
22
|
|
|
19
|
-
if (value.length === 0)
|
|
23
|
+
if (value.length === 0) {
|
|
24
|
+
return "- none";
|
|
25
|
+
}
|
|
20
26
|
|
|
21
|
-
return value.map((item) => `- ${item}`).join(
|
|
27
|
+
return value.map((item) => `- ${item}`).join("\n");
|
|
22
28
|
};
|
|
23
29
|
|
|
24
30
|
export const fillTemplate = (template: string, values: Readonly<Record<string, PromptValue>>) =>
|
|
25
31
|
template.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (placeholder, key: string) =>
|
|
26
|
-
Option.match(Option.fromUndefinedOr(values[key]), {
|
|
32
|
+
Option.match(Option.fromUndefinedOr(values[key]), {
|
|
33
|
+
onNone: () => placeholder,
|
|
34
|
+
onSome: render,
|
|
35
|
+
}),
|
|
27
36
|
);
|
|
28
37
|
|
|
29
38
|
export const renderPrompt = (name: string, values: Readonly<Record<string, PromptValue>>) =>
|
|
@@ -31,20 +40,33 @@ export const renderPrompt = (name: string, values: Readonly<Record<string, Promp
|
|
|
31
40
|
const repo = yield* Repo;
|
|
32
41
|
const fs = yield* FileSystem.FileSystem;
|
|
33
42
|
const path = `${repo.configDir}/prompts/${name}.md`;
|
|
34
|
-
|
|
43
|
+
|
|
44
|
+
const template = yield* fs
|
|
45
|
+
.readFileString(path)
|
|
46
|
+
.pipe(
|
|
47
|
+
Effect.mapError(
|
|
48
|
+
() => new StepFailed({ step: `prompt ${name}`, message: `no prompt at ${path}` }),
|
|
49
|
+
),
|
|
50
|
+
);
|
|
35
51
|
|
|
36
52
|
return fillTemplate(template, values);
|
|
37
53
|
});
|
|
38
54
|
|
|
39
55
|
// Like renderPrompt, from the default the SDK carries when the repo has no
|
|
40
56
|
// .chernabog/prompts/<name>.md of its own.
|
|
41
|
-
export const renderPromptOr = (
|
|
57
|
+
export const renderPromptOr = (
|
|
58
|
+
name: string,
|
|
59
|
+
fallback: string,
|
|
60
|
+
values: Readonly<Record<string, PromptValue>>,
|
|
61
|
+
) =>
|
|
42
62
|
Effect.gen(function* () {
|
|
43
63
|
const repo = yield* Repo;
|
|
44
64
|
const fs = yield* FileSystem.FileSystem;
|
|
45
65
|
const path = `${repo.configDir}/prompts/${name}.md`;
|
|
46
66
|
|
|
47
|
-
if (!(yield* fs.exists(path).pipe(Effect.orElseSucceed(() => false))))
|
|
67
|
+
if (!(yield* fs.exists(path).pipe(Effect.orElseSucceed(() => false)))) {
|
|
68
|
+
return fillTemplate(fallback, values);
|
|
69
|
+
}
|
|
48
70
|
|
|
49
71
|
return yield* renderPrompt(name, values);
|
|
50
72
|
});
|
package/src/pull-request.ts
CHANGED
|
@@ -1,19 +1,29 @@
|
|
|
1
|
-
// Steps on a pull request that any flow can use, whichever flow opened it
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import * as
|
|
7
|
-
import * as
|
|
8
|
-
import * as
|
|
9
|
-
import * as
|
|
10
|
-
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
1
|
+
// Steps on a pull request that any flow can use, whichever flow opened it:
|
|
2
|
+
// asking the team's reviewer pool for a review, merging on the automated path,
|
|
3
|
+
// and handing it to a person. A flow that works on a pull request itself can
|
|
4
|
+
// own it while it does, so the standalone flows leave it alone.
|
|
5
|
+
|
|
6
|
+
import * as Cause from "effect/Cause";
|
|
7
|
+
import * as Effect from "effect/Effect";
|
|
8
|
+
import * as Exit from "effect/Exit";
|
|
9
|
+
import * as Option from "effect/Option";
|
|
10
|
+
import * as Random from "effect/Random";
|
|
11
|
+
import * as Schema from "effect/Schema";
|
|
12
|
+
|
|
13
|
+
import { commentStep } from "./comment.ts";
|
|
14
|
+
import { MergeRefused, NeedsHuman, PullRequestClosed } from "./errors.ts";
|
|
15
|
+
import { activity } from "./flow.ts";
|
|
16
|
+
import { latestMarker, type OwnerMarker, ownerMarker, reviewMarker } from "./markers.ts";
|
|
17
|
+
import { pathsMatching } from "./paths.ts";
|
|
18
|
+
import {
|
|
19
|
+
GitHub,
|
|
20
|
+
type Labels,
|
|
21
|
+
type MergeMethod,
|
|
22
|
+
type PullRequest,
|
|
23
|
+
Repo,
|
|
24
|
+
Run,
|
|
25
|
+
Runs,
|
|
26
|
+
} from "./services.ts";
|
|
17
27
|
|
|
18
28
|
const writeOwner = (name: string, pullRequest: number, released: boolean) =>
|
|
19
29
|
activity(
|
|
@@ -22,19 +32,29 @@ const writeOwner = (name: string, pullRequest: number, released: boolean) =>
|
|
|
22
32
|
const run = yield* Run;
|
|
23
33
|
const marker = ownerMarker.write({ flow: run.flow, runId: run.runId, released });
|
|
24
34
|
|
|
25
|
-
if (released)
|
|
26
|
-
|
|
35
|
+
if (released) {
|
|
36
|
+
yield* (yield* GitHub).comment(
|
|
37
|
+
pullRequest,
|
|
38
|
+
`The \`${run.flow}\` run is done with this pull request. ${marker}`,
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
yield* (yield* GitHub).comment(
|
|
42
|
+
pullRequest,
|
|
43
|
+
`The \`${run.flow}\` run owns this pull request until it ends. ${marker}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
27
46
|
}),
|
|
28
47
|
);
|
|
29
48
|
|
|
30
49
|
// Comments on the pull request, and does nothing else.
|
|
31
|
-
export const comment = (pullRequest: number, body: string) => commentStep(
|
|
50
|
+
export const comment = (pullRequest: number, body: string) => commentStep("pr", pullRequest, body);
|
|
32
51
|
|
|
33
52
|
// Marks the pull request as this run's.
|
|
34
53
|
export const own = (pullRequest: number) => writeOwner(`own-pr-${pullRequest}`, pullRequest, false);
|
|
35
54
|
|
|
36
55
|
// Lets the pull request go, so the standalone flows pick it up again.
|
|
37
|
-
export const release = (pullRequest: number) =>
|
|
56
|
+
export const release = (pullRequest: number) =>
|
|
57
|
+
writeOwner(`release-pr-${pullRequest}`, pullRequest, true);
|
|
38
58
|
|
|
39
59
|
// Owns the pull request for as long as `effect` runs, and releases it however
|
|
40
60
|
// that ends. A run waiting on a signal keeps it.
|
|
@@ -45,7 +65,9 @@ export const owning =
|
|
|
45
65
|
Effect.andThen(effect),
|
|
46
66
|
// A wait suspends the run by interrupting it; that is not an ending.
|
|
47
67
|
Effect.onExit((exit) => {
|
|
48
|
-
if (Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause))
|
|
68
|
+
if (Exit.isFailure(exit) && Cause.hasInterrupts(exit.cause)) {
|
|
69
|
+
return Effect.void;
|
|
70
|
+
}
|
|
49
71
|
|
|
50
72
|
return release(pullRequest).pipe(Effect.ignore);
|
|
51
73
|
}),
|
|
@@ -55,27 +77,38 @@ export const owning =
|
|
|
55
77
|
// or when this machine knows the run has ended.
|
|
56
78
|
export const ownerOf = (bodies: ReadonlyArray<string>) =>
|
|
57
79
|
Effect.gen(function* () {
|
|
58
|
-
const latest = Option.filter(
|
|
80
|
+
const latest = Option.filter(
|
|
81
|
+
latestMarker(ownerMarker.read, bodies),
|
|
82
|
+
(marker) => !marker.released,
|
|
83
|
+
);
|
|
59
84
|
|
|
60
|
-
if (Option.isNone(latest))
|
|
85
|
+
if (Option.isNone(latest)) {
|
|
86
|
+
return Option.none<OwnerMarker>();
|
|
87
|
+
}
|
|
61
88
|
|
|
62
89
|
const runs = yield* Effect.serviceOption(Runs);
|
|
63
|
-
const active = yield* Option.match(runs, { onNone: () => Effect.succeedNone, onSome: (known) => known.isActive(latest.value.runId) });
|
|
64
90
|
|
|
65
|
-
|
|
91
|
+
const active = yield* Option.match(runs, {
|
|
92
|
+
onNone: () => Effect.succeedNone,
|
|
93
|
+
onSome: (known) => known.isActive(latest.value.runId),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
if (Option.contains(active, false)) {
|
|
97
|
+
return Option.none<OwnerMarker>();
|
|
98
|
+
}
|
|
66
99
|
|
|
67
100
|
return latest;
|
|
68
101
|
});
|
|
69
102
|
|
|
70
103
|
export type MergeOptions = { readonly method: MergeMethod };
|
|
71
104
|
|
|
72
|
-
const refuse = (pullRequest: number, reason: string) => new
|
|
105
|
+
const refuse = (pullRequest: number, reason: string) => new MergeRefused({ pullRequest, reason });
|
|
73
106
|
|
|
74
107
|
// Merges the pull request, only when that is safe: it is open, its required
|
|
75
108
|
// checks are green on the current head, the latest review approved that very
|
|
76
109
|
// head, it touches no humanOnlyPaths, and GitHub's rules for the repo allow it.
|
|
77
|
-
// Otherwise it fails with
|
|
78
|
-
// calls this.
|
|
110
|
+
// Otherwise it fails with MergeRefused, saying why, or PullRequestClosed for
|
|
111
|
+
// one closed without merging. Nothing merges unless a flow calls this.
|
|
79
112
|
export const merge = (pullRequest: number, options: MergeOptions) =>
|
|
80
113
|
activity(
|
|
81
114
|
`merge-pr-${pullRequest}`,
|
|
@@ -85,36 +118,139 @@ export const merge = (pullRequest: number, options: MergeOptions) =>
|
|
|
85
118
|
const pull = yield* github.pullRequest(pullRequest);
|
|
86
119
|
|
|
87
120
|
// A rerun after a crash finds its own merge done.
|
|
88
|
-
if (pull.state ===
|
|
121
|
+
if (pull.state === "merged") {
|
|
122
|
+
return `#${pullRequest} merged`;
|
|
123
|
+
}
|
|
89
124
|
|
|
90
|
-
if (pull.state
|
|
125
|
+
if (pull.state === "closed") {
|
|
126
|
+
return yield* new PullRequestClosed({ pullRequest, state: "closed" });
|
|
127
|
+
}
|
|
91
128
|
|
|
92
129
|
const ci = yield* github.ciState(pullRequest);
|
|
93
130
|
|
|
94
|
-
if (ci.state ===
|
|
131
|
+
if (ci.state === "red") {
|
|
132
|
+
return yield* refuse(pullRequest, `checks failed: ${ci.failedChecks.join(", ")}`);
|
|
133
|
+
}
|
|
95
134
|
|
|
96
|
-
if (ci.state ===
|
|
135
|
+
if (ci.state === "pending") {
|
|
136
|
+
return yield* refuse(pullRequest, "checks are still pending");
|
|
137
|
+
}
|
|
97
138
|
|
|
98
139
|
const reviewed = latestMarker(
|
|
99
140
|
reviewMarker.read,
|
|
100
141
|
(yield* github.comments(pullRequest)).map((posted) => posted.body),
|
|
101
142
|
);
|
|
102
143
|
|
|
103
|
-
if (Option.isNone(reviewed))
|
|
104
|
-
|
|
105
|
-
|
|
144
|
+
if (Option.isNone(reviewed)) {
|
|
145
|
+
return yield* refuse(pullRequest, "it has no review");
|
|
146
|
+
}
|
|
106
147
|
|
|
107
|
-
if (reviewed.value.
|
|
148
|
+
if (reviewed.value.sha !== pull.headSha) {
|
|
149
|
+
return yield* refuse(
|
|
150
|
+
pullRequest,
|
|
151
|
+
`the head moved to \`${pull.headSha.slice(0, 7)}\` since review ${reviewed.value.round} saw \`${reviewed.value.sha.slice(0, 7)}\``,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
108
154
|
|
|
109
|
-
|
|
155
|
+
if (reviewed.value.verdict !== "approve") {
|
|
156
|
+
return yield* refuse(pullRequest, `review ${reviewed.value.round} requested changes`);
|
|
157
|
+
}
|
|
110
158
|
|
|
111
|
-
|
|
159
|
+
const touched = pathsMatching(
|
|
160
|
+
yield* github.pullRequestFiles(pullRequest),
|
|
161
|
+
repo.humanOnlyPaths,
|
|
162
|
+
);
|
|
112
163
|
|
|
113
|
-
|
|
164
|
+
if (touched.length > 0) {
|
|
165
|
+
return yield* refuse(
|
|
166
|
+
pullRequest,
|
|
167
|
+
`it touches paths a person must handle: ${touched.map((path) => `\`${path}\``).join(", ")}`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
114
170
|
|
|
115
|
-
|
|
171
|
+
const merged = yield* github.merge(pullRequest, {
|
|
172
|
+
method: options.method,
|
|
173
|
+
sha: pull.headSha,
|
|
174
|
+
});
|
|
116
175
|
|
|
117
176
|
return `#${pullRequest} merged (${options.method}) as \`${merged.sha.slice(0, 7)}\``;
|
|
118
177
|
}),
|
|
119
178
|
Schema.String,
|
|
120
179
|
);
|
|
180
|
+
|
|
181
|
+
const sameLogin = (left: string, right: string) => left.toLowerCase() === right.toLowerCase();
|
|
182
|
+
|
|
183
|
+
// Asks one reviewer from the team's pool (reviewers: in config) to review the
|
|
184
|
+
// pull request, never its author. The choice is made once, at random, in an
|
|
185
|
+
// activity, so a replay asks the same person. Without a pool, or with no one
|
|
186
|
+
// in it but the author, it labels the pull request needs-review for a person.
|
|
187
|
+
export const requestReview = (pullRequest: number) =>
|
|
188
|
+
activity(
|
|
189
|
+
`request-review-${pullRequest}`,
|
|
190
|
+
Effect.gen(function* () {
|
|
191
|
+
const github = yield* GitHub;
|
|
192
|
+
const repo = yield* Repo;
|
|
193
|
+
const pull = yield* github.pullRequest(pullRequest);
|
|
194
|
+
const pool = (repo.reviewers ?? []).filter((login) => !sameLogin(login, pull.author));
|
|
195
|
+
|
|
196
|
+
if (pool.length === 0) {
|
|
197
|
+
yield* github.addLabel(pullRequest, repo.labels.needsReview);
|
|
198
|
+
|
|
199
|
+
return `#${pullRequest}: no reviewer in the pool but its author; labelled ${repo.labels.needsReview} for a person`;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const reviewer = pool[yield* Random.nextIntBetween(0, pool.length, { halfOpen: true })];
|
|
203
|
+
yield* github.requestReviewers(pullRequest, [reviewer]);
|
|
204
|
+
|
|
205
|
+
return `#${pullRequest}: review requested of ${reviewer}`;
|
|
206
|
+
}),
|
|
207
|
+
Schema.String,
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
// Whether chernabog may merge the pull request: only on the automated path,
|
|
211
|
+
// one labelled ship (auto:ship). Anything else, a missing or misspelt label
|
|
212
|
+
// included, is left for a person to merge. The one place that decides it.
|
|
213
|
+
export const ships = (pull: PullRequest, labels: Labels) => pull.labels.includes(labels.ship);
|
|
214
|
+
|
|
215
|
+
// Labels the pull request needs-human and says why; no flow picks it up again
|
|
216
|
+
// until a person removes the label.
|
|
217
|
+
export const handToPerson = (pullRequest: number, reason: string) =>
|
|
218
|
+
activity(
|
|
219
|
+
`needs-human-pr-${pullRequest}`,
|
|
220
|
+
Effect.gen(function* () {
|
|
221
|
+
const github = yield* GitHub;
|
|
222
|
+
const repo = yield* Repo;
|
|
223
|
+
yield* github.addLabel(pullRequest, repo.labels.needsHuman);
|
|
224
|
+
yield* github.comment(pullRequest, `Chernabog stopped and needs a person: ${reason}`);
|
|
225
|
+
}),
|
|
226
|
+
).pipe(Effect.andThen(Effect.fail(new NeedsHuman({ reason: `#${pullRequest}: ${reason}` }))));
|
|
227
|
+
|
|
228
|
+
// Whether chernabog may merge the pull request, as `ships` decides it,
|
|
229
|
+
// read once in an activity so a replay decides the same.
|
|
230
|
+
export const shipping = (pullRequest: number) =>
|
|
231
|
+
activity(
|
|
232
|
+
`ships-pr-${pullRequest}`,
|
|
233
|
+
Effect.gen(function* () {
|
|
234
|
+
const repo = yield* Repo;
|
|
235
|
+
|
|
236
|
+
return ships(yield* (yield* GitHub).pullRequest(pullRequest), repo.labels);
|
|
237
|
+
}),
|
|
238
|
+
Schema.Boolean,
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
// After an approval: on the automated path, merges the pull request when that
|
|
242
|
+
// is safe, and otherwise hands it to a person; any other pull request is left
|
|
243
|
+
// for a person to merge, and one closed meanwhile is left alone.
|
|
244
|
+
export const mergeOrLeave = (pullRequest: number) =>
|
|
245
|
+
Effect.gen(function* () {
|
|
246
|
+
if (!(yield* shipping(pullRequest))) {
|
|
247
|
+
return `#${pullRequest} approved; a person merges`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return yield* merge(pullRequest, { method: "squash" }).pipe(
|
|
251
|
+
Effect.catchTags({
|
|
252
|
+
MergeRefused: (refused) => handToPerson(pullRequest, refused.message),
|
|
253
|
+
PullRequestClosed: (closed) => Effect.succeed(`${closed.message}; nothing to merge`),
|
|
254
|
+
}),
|
|
255
|
+
);
|
|
256
|
+
});
|