@chernabog/sdk 0.10.0 → 0.11.1
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/human-initiated-blocks.ts +274 -0
- package/src/hybrid.ts +2 -1
- package/src/implement.ts +103 -16
- 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/suits.ts +156 -0
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
|
+
};
|