co-maintainer 0.7.3 → 0.7.4
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/dist/package.json
CHANGED
|
@@ -18,8 +18,13 @@ export type FreshRule = {
|
|
|
18
18
|
* A fresh rule may keep an old id when it shares a source with that rule, or
|
|
19
19
|
* else has the same text. The ids are matched as a whole, not first come
|
|
20
20
|
* first served: a rule that could keep two ids gives way to a later rule that
|
|
21
|
-
* can keep only one of them, so as many ids as possible stay the same.
|
|
22
|
-
*
|
|
21
|
+
* can keep only one of them, so as many ids as possible stay the same.
|
|
22
|
+
*
|
|
23
|
+
* Among its options a rule tries the best first: each shared pull request or
|
|
24
|
+
* file line counts 1, a shared review comment link 1 more, and text
|
|
25
|
+
* similarity adds less than one source. So two rules that rest on the same
|
|
26
|
+
* pull request keep their own ids, by their comment links when both builds
|
|
27
|
+
* kept them, or else by their wording. */
|
|
23
28
|
export declare function stableIds(kind: "R" | "I", fresh: FreshRule[], previous: Record<string, RuleEntry> | undefined, lastId: number | undefined): {
|
|
24
29
|
ids: Map<string, string>;
|
|
25
30
|
lastId: number;
|
|
@@ -16,32 +16,66 @@ function highest(rules, kind) {
|
|
|
16
16
|
}
|
|
17
17
|
return max;
|
|
18
18
|
}
|
|
19
|
+
/** What a source can match on: its pull request or file line, and for a
|
|
20
|
+
* guide rule also the review comment it came from. Two rules on the same pull
|
|
21
|
+
* request share `#4` but not their comment links. */
|
|
22
|
+
function matchKeys(source) {
|
|
23
|
+
const url = "pr" in source ? source.url : undefined;
|
|
24
|
+
return url ? [sourceLabel(source), url] : [sourceLabel(source)];
|
|
25
|
+
}
|
|
26
|
+
function words(text) {
|
|
27
|
+
return new Set(text
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.split(/[^\p{L}\p{N}]+/u)
|
|
30
|
+
.filter((word) => word.length > 2));
|
|
31
|
+
}
|
|
32
|
+
/** Shared words over all words, from 0 to 1. */
|
|
33
|
+
function similarity(a, b) {
|
|
34
|
+
if (a.size === 0 && b.size === 0)
|
|
35
|
+
return 1;
|
|
36
|
+
let shared = 0;
|
|
37
|
+
for (const word of a)
|
|
38
|
+
if (b.has(word))
|
|
39
|
+
shared++;
|
|
40
|
+
return shared / (a.size + b.size - shared);
|
|
41
|
+
}
|
|
19
42
|
/** Maps each fresh id to its stable id, and returns the new highest number.
|
|
20
43
|
* A fresh rule may keep an old id when it shares a source with that rule, or
|
|
21
44
|
* else has the same text. The ids are matched as a whole, not first come
|
|
22
45
|
* first served: a rule that could keep two ids gives way to a later rule that
|
|
23
|
-
* can keep only one of them, so as many ids as possible stay the same.
|
|
24
|
-
*
|
|
46
|
+
* can keep only one of them, so as many ids as possible stay the same.
|
|
47
|
+
*
|
|
48
|
+
* Among its options a rule tries the best first: each shared pull request or
|
|
49
|
+
* file line counts 1, a shared review comment link 1 more, and text
|
|
50
|
+
* similarity adds less than one source. So two rules that rest on the same
|
|
51
|
+
* pull request keep their own ids, by their comment links when both builds
|
|
52
|
+
* kept them, or else by their wording. */
|
|
25
53
|
export function stableIds(kind, fresh, previous, lastId) {
|
|
26
54
|
const old = Object.entries(previous ?? {})
|
|
27
55
|
.filter(([id]) => id.startsWith(kind))
|
|
28
56
|
.map(([id, entry]) => ({
|
|
29
57
|
id,
|
|
30
58
|
text: entry.text,
|
|
31
|
-
|
|
59
|
+
words: words(entry.text),
|
|
60
|
+
keys: new Set(entry.sources.flatMap(matchKeys)),
|
|
32
61
|
}));
|
|
33
62
|
const rules = fresh.filter((rule, index) => fresh.findIndex((item) => item.id === rule.id) === index);
|
|
34
63
|
// Each rule's old ids it may keep, best first.
|
|
35
64
|
const options = rules.map((rule) => {
|
|
36
|
-
const
|
|
65
|
+
const keys = [...new Set(rule.sources.flatMap(matchKeys))];
|
|
66
|
+
const own = words(rule.text);
|
|
37
67
|
return old
|
|
38
68
|
.flatMap((item, order) => {
|
|
39
69
|
if (rule.avoid?.includes(item.id))
|
|
40
70
|
return [];
|
|
41
|
-
const shared =
|
|
71
|
+
const shared = keys.filter((key) => item.keys.has(key)).length;
|
|
42
72
|
// Same text with no shared source still counts, below any shared
|
|
43
73
|
// source: an instruction line that moved down a file is the same rule.
|
|
44
|
-
const score = shared > 0
|
|
74
|
+
const score = shared > 0
|
|
75
|
+
? shared + 0.5 * similarity(own, item.words)
|
|
76
|
+
: item.text === rule.text
|
|
77
|
+
? 0.5
|
|
78
|
+
: 0;
|
|
45
79
|
return score > 0 ? [{ id: item.id, score, order }] : [];
|
|
46
80
|
})
|
|
47
81
|
.sort((a, b) => b.score - a.score || a.order - b.order)
|
|
@@ -115,7 +115,10 @@ export async function stabilizeRuleIds(repo, documents, instructions) {
|
|
|
115
115
|
const guide = stableIds("R", (documents?.rules ?? []).map((rule) => ({
|
|
116
116
|
id: rule.id,
|
|
117
117
|
text: rule.text,
|
|
118
|
-
sources: rule.pullRequests.map((pr) =>
|
|
118
|
+
sources: rule.pullRequests.map((pr) => {
|
|
119
|
+
const url = rule.links?.find((link) => link.pr === pr)?.url;
|
|
120
|
+
return url ? { pr, url } : { pr };
|
|
121
|
+
}),
|
|
119
122
|
// A rule never takes the id of the rule it goes against.
|
|
120
123
|
avoid: rule.overturns ?? [],
|
|
121
124
|
})), previous?.rules, previous?.lastIds?.R);
|