@chernabog/sdk 0.9.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.
- package/package.json +1 -1
- package/src/address-review-blocks.ts +510 -0
- package/src/block.ts +466 -0
- package/src/blocks.ts +1051 -0
- package/src/compose.ts +2666 -0
- package/src/epic-blocks.ts +1217 -0
- package/src/epic.ts +24 -9
- package/src/human-initiated-blocks.ts +274 -0
- package/src/implement.ts +65 -10
- package/src/index.ts +3 -0
- package/src/plan-blocks.ts +312 -0
- package/src/post-merge-blocks.ts +826 -0
- package/src/pull-request.ts +17 -0
- package/src/re-review-blocks.ts +234 -0
- package/src/resolve-conflicts-blocks.ts +567 -0
- package/src/review.ts +3 -1
- package/src/settings.ts +22 -2
- package/src/stack.ts +85 -0
- package/src/suits.ts +156 -0
package/src/stack.ts
CHANGED
|
@@ -103,6 +103,13 @@ const contains = (worktree: string, ancestor: string) =>
|
|
|
103
103
|
Effect.catchTag("GitError", () => Effect.succeed(false)),
|
|
104
104
|
);
|
|
105
105
|
|
|
106
|
+
// Whether `descendant` holds `ancestor` in its history.
|
|
107
|
+
const holds = (worktree: string, descendant: string, ancestor: string) =>
|
|
108
|
+
Git.use((git) => git.run(["merge-base", "--is-ancestor", ancestor, descendant], worktree)).pipe(
|
|
109
|
+
Effect.as(true),
|
|
110
|
+
Effect.catchTag("GitError", () => Effect.succeed(false)),
|
|
111
|
+
);
|
|
112
|
+
|
|
106
113
|
// Merges `ref` into HEAD. One that conflicts is left in progress, and its
|
|
107
114
|
// conflicted paths returned; any other failure stays a failure.
|
|
108
115
|
const merge = (worktree: string, args: ReadonlyArray<string>) =>
|
|
@@ -235,6 +242,84 @@ export const updateFromBase = (worktree: string, options: UpdateOptions = {}) =>
|
|
|
235
242
|
),
|
|
236
243
|
);
|
|
237
244
|
|
|
245
|
+
const ClosedPull = Schema.Struct({
|
|
246
|
+
number: Schema.Finite,
|
|
247
|
+
merged_at: Schema.OptionFromNullOr(Schema.String),
|
|
248
|
+
merge_commit_sha: Schema.OptionFromNullOr(Schema.String),
|
|
249
|
+
head: Schema.Struct({ sha: Schema.String }),
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// The pull requests closed lately into `base`, newest first, one page.
|
|
253
|
+
const closedInto = (base: string) =>
|
|
254
|
+
Effect.gen(function* () {
|
|
255
|
+
const repo = yield* Repo;
|
|
256
|
+
const query = `state=closed&base=${encodeURIComponent(base)}&sort=updated&direction=desc&per_page=30`;
|
|
257
|
+
|
|
258
|
+
return yield* (yield* GitHub)
|
|
259
|
+
.request("GET", `/repos/${repo.slug}/pulls?${query}`, Option.none())
|
|
260
|
+
.pipe(
|
|
261
|
+
Effect.flatMap((json: Json) => Schema.decodeUnknownEffect(Schema.Array(ClosedPull))(json)),
|
|
262
|
+
Effect.mapError(
|
|
263
|
+
(error) => new StepFailed({ step: "merged-below", message: error.message }),
|
|
264
|
+
),
|
|
265
|
+
);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// The layer below the one checked out in `worktree` that squash- or
|
|
269
|
+
// rebase-merged into `base` without this layer recording it: a pull request
|
|
270
|
+
// merged lately into `base` whose head is in this layer's history and whose
|
|
271
|
+
// merge commit isn't. Its number, for updateFromBase's `lower`; none when
|
|
272
|
+
// there is none. It is for a layer whose pull request already moved onto
|
|
273
|
+
// `base`, as GitHub moves one when the branch below it is deleted, so its
|
|
274
|
+
// layer below is no longer its base: merging `base` plainly would see this
|
|
275
|
+
// layer's edits of lines the layer below added as conflicts, and bring back
|
|
276
|
+
// files `base` has since moved or deleted (#311). With two such layers below,
|
|
277
|
+
// it is the higher one, whose head holds the other's.
|
|
278
|
+
export const mergedBelow = (worktree: string, base: string) =>
|
|
279
|
+
Effect.gen(function* () {
|
|
280
|
+
const git = yield* Git;
|
|
281
|
+
yield* git.run(["fetch", "--quiet", "origin", base], worktree);
|
|
282
|
+
|
|
283
|
+
const found: Array<{ number: number; head: string }> = [];
|
|
284
|
+
|
|
285
|
+
for (const pull of yield* closedInto(base)) {
|
|
286
|
+
if (Option.isNone(pull.merged_at) || Option.isNone(pull.merge_commit_sha)) {
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (
|
|
291
|
+
(yield* contains(worktree, pull.head.sha)) &&
|
|
292
|
+
!(yield* contains(worktree, pull.merge_commit_sha.value))
|
|
293
|
+
) {
|
|
294
|
+
found.push({ number: pull.number, head: pull.head.sha });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// The one no other found head is built on.
|
|
299
|
+
for (const candidate of found) {
|
|
300
|
+
let highest = true;
|
|
301
|
+
|
|
302
|
+
for (const other of found) {
|
|
303
|
+
if (
|
|
304
|
+
other.number !== candidate.number &&
|
|
305
|
+
(yield* holds(worktree, other.head, candidate.head))
|
|
306
|
+
) {
|
|
307
|
+
highest = false;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (highest) {
|
|
312
|
+
return Option.some(candidate.number);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return Option.none<number>();
|
|
317
|
+
}).pipe(
|
|
318
|
+
Effect.catchTag("GitError", (error) =>
|
|
319
|
+
Effect.fail(new StepFailed({ step: "merged-below", message: error.message })),
|
|
320
|
+
),
|
|
321
|
+
);
|
|
322
|
+
|
|
238
323
|
// updateFromBase as a durable step: the merge is made once, and a replay
|
|
239
324
|
// returns what it did.
|
|
240
325
|
export const syncBase = (worktree: string, options: UpdateOptions = {}) =>
|
package/src/suits.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Which subjects each block suits, found from what it needs rather than kept
|
|
2
|
+
// as a list beside it that could drift. A block suits a subject when a flow on
|
|
3
|
+
// it can give each input the block must have:
|
|
4
|
+
// - an input said to come from another block (`from author.address`) is
|
|
5
|
+
// there wherever that block is;
|
|
6
|
+
// - one said to come from the trigger (`trigger.branch`) is there where the
|
|
7
|
+
// subject's trigger gives that field, of the same type;
|
|
8
|
+
// - one of a type (`PullRequestNumber`, `Option<BaseBranch>`) is there where
|
|
9
|
+
// the subject's trigger gives one, or a block that suits it does: an
|
|
10
|
+
// issue's flow has a pull request's number once pr.open gives it.
|
|
11
|
+
// An input of no type it names, such as a round, is there on any subject. A
|
|
12
|
+
// block that declares the subjects it suits suits none of the others.
|
|
13
|
+
|
|
14
|
+
import * as Option from "effect/Option";
|
|
15
|
+
import * as Predicate from "effect/Predicate";
|
|
16
|
+
|
|
17
|
+
type Kinds = string | { readonly [field: string]: string };
|
|
18
|
+
|
|
19
|
+
export type Suitable = {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly inputs: { readonly [input: string]: "required" | "optional" };
|
|
22
|
+
readonly inputKinds: { readonly [input: string]: string };
|
|
23
|
+
readonly outputKinds: Kinds;
|
|
24
|
+
readonly outcomeKinds: { readonly [outcome: string]: string };
|
|
25
|
+
readonly suits: Option.Option<ReadonlyArray<string>>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// What each subject's trigger gives, a kind for each field.
|
|
29
|
+
export type TriggerKinds = { readonly [subject: string]: { readonly [field: string]: string } };
|
|
30
|
+
|
|
31
|
+
// A type a kind names: a capitalised name, maybe as an Option of one, but not
|
|
32
|
+
// an Option of something it doesn't name, `Option<the earlier review>`.
|
|
33
|
+
const TYPE = /(Option<)?\b([A-Z][A-Za-z]*)\b(?!<)/g;
|
|
34
|
+
|
|
35
|
+
const LEADING_TYPE = /^(Option<)?([A-Z][A-Za-z]*)\b(?!<)/;
|
|
36
|
+
|
|
37
|
+
const BLOCK_NAME = /\b[a-z][A-Za-z]*\.[A-Za-z]+\b/g;
|
|
38
|
+
|
|
39
|
+
const TRIGGER_FIELD = /\btrigger\.(\w+)/g;
|
|
40
|
+
|
|
41
|
+
// Its groups, `Option<` when it is one and the name; join leaves out the first
|
|
42
|
+
// when it isn't.
|
|
43
|
+
const typeOf = (match: RegExpMatchArray) => match.slice(1, 3).join("");
|
|
44
|
+
|
|
45
|
+
// The type a kind starts with, which is the type of what it is.
|
|
46
|
+
const leadingType = (kind: string) =>
|
|
47
|
+
Option.map(Option.fromNullishOr(LEADING_TYPE.exec(kind)), typeOf);
|
|
48
|
+
|
|
49
|
+
const typesIn = (kind: string) => [...kind.matchAll(TYPE)].map(typeOf);
|
|
50
|
+
|
|
51
|
+
const kindsOf = (kinds: Kinds) => {
|
|
52
|
+
if (Predicate.isString(kinds)) {
|
|
53
|
+
return [kinds];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return Object.values(kinds);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// The kinds of everything a block gives: its output and what its outcomes bind.
|
|
60
|
+
const givenBy = (block: Suitable) => [
|
|
61
|
+
...kindsOf(block.outputKinds),
|
|
62
|
+
...Object.values(block.outcomeKinds),
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
export const suited = (
|
|
66
|
+
blocks: ReadonlyArray<Suitable>,
|
|
67
|
+
triggers: TriggerKinds,
|
|
68
|
+
): { readonly [block: string]: ReadonlyArray<string> } => {
|
|
69
|
+
const names = new Set(blocks.map((block) => block.name));
|
|
70
|
+
const subjects = Object.keys(triggers);
|
|
71
|
+
|
|
72
|
+
const suits = new Map(blocks.map((block) => [block.name, new Set<string>()]));
|
|
73
|
+
|
|
74
|
+
const has = new Map(
|
|
75
|
+
subjects.map((subject) => [
|
|
76
|
+
subject,
|
|
77
|
+
new Set(
|
|
78
|
+
Object.values(triggers[subject] ?? {}).flatMap((kind) => Option.toArray(leadingType(kind))),
|
|
79
|
+
),
|
|
80
|
+
]),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const suitsNow = (block: string, subject: string) => suits.get(block)?.has(subject) === true;
|
|
84
|
+
|
|
85
|
+
// Whether a flow on the subject can give an input of this kind.
|
|
86
|
+
const gives = (subject: string, kind: string) => {
|
|
87
|
+
const from = [...kind.matchAll(BLOCK_NAME)].flatMap((match) =>
|
|
88
|
+
Option.toArray(Option.liftPredicate(match[0], (name) => names.has(name))),
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
if (from.length > 0) {
|
|
92
|
+
return from.some((block) => suitsNow(block, subject));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const fields = [...kind.matchAll(TRIGGER_FIELD)].map((match) => match.slice(1, 2).join(""));
|
|
96
|
+
const type = leadingType(kind);
|
|
97
|
+
const trigger = triggers[subject] ?? {};
|
|
98
|
+
|
|
99
|
+
if (fields.length > 0) {
|
|
100
|
+
return fields.every((field) => {
|
|
101
|
+
const given = Option.fromUndefinedOr(trigger[field]);
|
|
102
|
+
|
|
103
|
+
// Of the same type, or of none on either side.
|
|
104
|
+
return (
|
|
105
|
+
Option.isSome(given) &&
|
|
106
|
+
Option.getOrElse(leadingType(given.value), () => "") === Option.getOrElse(type, () => "")
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return Option.match(type, {
|
|
112
|
+
onNone: () => true,
|
|
113
|
+
onSome: (named) => has.get(subject)?.has(named) === true,
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const declared = (block: Suitable, subject: string) =>
|
|
118
|
+
Option.match(block.suits, {
|
|
119
|
+
onNone: () => true,
|
|
120
|
+
onSome: (only) => only.includes(subject),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const fits = (block: Suitable, subject: string) =>
|
|
124
|
+
declared(block, subject) &&
|
|
125
|
+
Object.entries(block.inputs)
|
|
126
|
+
.filter(([, need]) => need === "required")
|
|
127
|
+
.every(([input]) => gives(subject, block.inputKinds[input] ?? ""));
|
|
128
|
+
|
|
129
|
+
// What a block gives, a flow it suits has, which may suit more blocks to it:
|
|
130
|
+
// go round until nothing more suits.
|
|
131
|
+
let more = true;
|
|
132
|
+
|
|
133
|
+
while (more) {
|
|
134
|
+
more = false;
|
|
135
|
+
|
|
136
|
+
for (const block of blocks) {
|
|
137
|
+
for (const subject of subjects) {
|
|
138
|
+
if (!suitsNow(block.name, subject) && fits(block, subject)) {
|
|
139
|
+
suits.get(block.name)?.add(subject);
|
|
140
|
+
more = true;
|
|
141
|
+
|
|
142
|
+
for (const type of givenBy(block).flatMap(typesIn)) {
|
|
143
|
+
has.get(subject)?.add(type);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return Object.fromEntries(
|
|
151
|
+
blocks.map((block) => [
|
|
152
|
+
block.name,
|
|
153
|
+
subjects.filter((subject) => suitsNow(block.name, subject)),
|
|
154
|
+
]),
|
|
155
|
+
);
|
|
156
|
+
};
|