@operad-io/review-grading 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +36 -2
- package/package.json +22 -4
- package/review.schema.json +44 -0
- package/src/index.ts +196 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yinghai Zhao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @operad-io/review-grading
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The reader for a repository's review grading, `.operad/review.json`. A plugin
|
|
4
|
+
whose flow reviews calls
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
readReviewGrading(repositoryPath, flowName): Grading
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
and puts the result in its review prompt. The package depends only on `@operad-io/engine`, for its
|
|
11
|
+
shared helpers. `review.schema.json` is the file's JSON Schema.
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"$schema": "../packages/review-grading/review.schema.json",
|
|
16
|
+
"default": { "fixLevel": "major" },
|
|
17
|
+
"flows": {
|
|
18
|
+
"dev": {
|
|
19
|
+
"levels": { "critical": "…", "major": "…", "minor": "…" },
|
|
20
|
+
"rules": ["…"],
|
|
21
|
+
"fixLevel": "major"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Every field is optional. A flow's entry falls back to `default` field by field
|
|
28
|
+
(`levels` per level; `rules` as a whole). A missing file gives an empty grading,
|
|
29
|
+
so the plugin adds no grading text and keeps its own fix level. An invalid file
|
|
30
|
+
throws a `ReviewGradingError` whose message names `review.json` and what is
|
|
31
|
+
wrong.
|
|
32
|
+
|
|
33
|
+
| field | meaning |
|
|
34
|
+
|---|---|
|
|
35
|
+
| `levels.critical`, `levels.major`, `levels.minor` | what each severity means, given to the reviewer verbatim |
|
|
36
|
+
| `rules` | rules for grading, given to the reviewer verbatim |
|
|
37
|
+
| `fixLevel` | the lowest severity that fails a review; wins over a plugin's own setting |
|
package/package.json
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/review-grading",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./src/index.ts"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@operad-io/engine": "0.0.1"
|
|
10
|
+
},
|
|
11
|
+
"description": "operad: review report grading (in development)",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/operad-io/operad.git",
|
|
15
|
+
"directory": "packages/review-grading"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"review.schema.json",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://operad.io/schemas/review.schema.json",
|
|
4
|
+
"title": "operad review grading",
|
|
5
|
+
"description": "The grading in <repository>/.operad/review.json: what critical, major and minor mean, extra rules, and the lowest level that fails a review. A flow's entry falls back field by field to \"default\".",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"$schema": { "type": "string" },
|
|
9
|
+
"default": { "$ref": "#/$defs/grading" },
|
|
10
|
+
"flows": {
|
|
11
|
+
"description": "Grading per flow name.",
|
|
12
|
+
"type": "object",
|
|
13
|
+
"additionalProperties": { "$ref": "#/$defs/grading" }
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"additionalProperties": false,
|
|
17
|
+
"$defs": {
|
|
18
|
+
"grading": {
|
|
19
|
+
"type": "object",
|
|
20
|
+
"properties": {
|
|
21
|
+
"levels": {
|
|
22
|
+
"description": "What each severity means, given to the reviewer verbatim.",
|
|
23
|
+
"type": "object",
|
|
24
|
+
"properties": {
|
|
25
|
+
"critical": { "type": "string" },
|
|
26
|
+
"major": { "type": "string" },
|
|
27
|
+
"minor": { "type": "string" }
|
|
28
|
+
},
|
|
29
|
+
"additionalProperties": false
|
|
30
|
+
},
|
|
31
|
+
"rules": {
|
|
32
|
+
"description": "Rules for grading, given to the reviewer verbatim.",
|
|
33
|
+
"type": "array",
|
|
34
|
+
"items": { "type": "string" }
|
|
35
|
+
},
|
|
36
|
+
"fixLevel": {
|
|
37
|
+
"description": "The lowest severity that fails a review. Wins over dev.json's reviewer.fixLevel.",
|
|
38
|
+
"enum": ["critical", "major", "minor"]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"additionalProperties": false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A repository's review grading: `<repository>/.operad/review.json`, checked
|
|
3
|
+
* against the shipped `review.schema.json`. A plugin whose flow reviews calls
|
|
4
|
+
* `readReviewGrading(repositoryPath, flowName)` and gets the merged grading.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { isRecord } from "@operad-io/engine";
|
|
10
|
+
|
|
11
|
+
/** Where a repository keeps its review grading. */
|
|
12
|
+
export const REVIEW_GRADING_FILE = ".operad/review.json";
|
|
13
|
+
|
|
14
|
+
/** The severities a finding may have. */
|
|
15
|
+
export type Level = "critical" | "major" | "minor";
|
|
16
|
+
|
|
17
|
+
const LEVELS: readonly Level[] = ["critical", "major", "minor"];
|
|
18
|
+
|
|
19
|
+
/** One grading; every field is optional. */
|
|
20
|
+
export interface Grading {
|
|
21
|
+
readonly levels?: Readonly<Partial<Record<Level, string>>>;
|
|
22
|
+
readonly rules?: readonly string[];
|
|
23
|
+
readonly fixLevel?: Level;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** An invalid or unreadable review.json. The message names the file. */
|
|
27
|
+
export class ReviewGradingError extends Error {
|
|
28
|
+
constructor(message: string) {
|
|
29
|
+
super(`review.json: ${message}`);
|
|
30
|
+
this.name = "ReviewGradingError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function noExtra(
|
|
35
|
+
value: Record<string, unknown>,
|
|
36
|
+
allowed: readonly string[],
|
|
37
|
+
field: string,
|
|
38
|
+
): void {
|
|
39
|
+
for (const key of Object.keys(value)) {
|
|
40
|
+
if (!allowed.includes(key)) {
|
|
41
|
+
throw new ReviewGradingError(`${field}.${key} is not a known field`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parseGrading(value: unknown, field: string): Grading {
|
|
47
|
+
if (!isRecord(value)) {
|
|
48
|
+
throw new ReviewGradingError(`${field} must be a JSON object`);
|
|
49
|
+
}
|
|
50
|
+
noExtra(value, ["levels", "rules", "fixLevel"], field);
|
|
51
|
+
let levels: Partial<Record<Level, string>> | undefined;
|
|
52
|
+
if (value.levels !== undefined) {
|
|
53
|
+
const raw = value.levels;
|
|
54
|
+
if (!isRecord(raw)) {
|
|
55
|
+
throw new ReviewGradingError(`${field}.levels must be a JSON object`);
|
|
56
|
+
}
|
|
57
|
+
noExtra(raw, LEVELS, `${field}.levels`);
|
|
58
|
+
levels = {};
|
|
59
|
+
for (const level of LEVELS) {
|
|
60
|
+
const text = raw[level];
|
|
61
|
+
if (text === undefined) continue;
|
|
62
|
+
if (typeof text !== "string") {
|
|
63
|
+
throw new ReviewGradingError(`${field}.levels.${level} must be a string`);
|
|
64
|
+
}
|
|
65
|
+
levels[level] = text;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
let rules: string[] | undefined;
|
|
69
|
+
if (value.rules !== undefined) {
|
|
70
|
+
const raw = value.rules;
|
|
71
|
+
if (!Array.isArray(raw)) {
|
|
72
|
+
throw new ReviewGradingError(`${field}.rules must be an array of strings`);
|
|
73
|
+
}
|
|
74
|
+
rules = raw.map((rule: unknown, index) => {
|
|
75
|
+
if (typeof rule !== "string") {
|
|
76
|
+
throw new ReviewGradingError(`${field}.rules[${index}] must be a string`);
|
|
77
|
+
}
|
|
78
|
+
return rule;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
let fixLevel: Level | undefined;
|
|
82
|
+
if (value.fixLevel !== undefined) {
|
|
83
|
+
const raw = value.fixLevel;
|
|
84
|
+
const found = LEVELS.find((level) => level === raw);
|
|
85
|
+
if (found === undefined) {
|
|
86
|
+
throw new ReviewGradingError(
|
|
87
|
+
`${field}.fixLevel must be "critical", "major" or "minor"`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
fixLevel = found;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
...(levels === undefined ? {} : { levels }),
|
|
94
|
+
...(rules === undefined ? {} : { rules }),
|
|
95
|
+
...(fixLevel === undefined ? {} : { fixLevel }),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Validates one parsed review.json and returns its default and flow entries. */
|
|
100
|
+
export function validateReviewGrading(value: unknown): {
|
|
101
|
+
readonly default: Grading;
|
|
102
|
+
readonly flows: Readonly<Record<string, Grading>>;
|
|
103
|
+
} {
|
|
104
|
+
if (!isRecord(value)) {
|
|
105
|
+
throw new ReviewGradingError("must be a JSON object");
|
|
106
|
+
}
|
|
107
|
+
noExtra(value, ["$schema", "default", "flows"], "$");
|
|
108
|
+
if (value.$schema !== undefined && typeof value.$schema !== "string") {
|
|
109
|
+
throw new ReviewGradingError("$schema must be a string");
|
|
110
|
+
}
|
|
111
|
+
const flows: Record<string, Grading> = {};
|
|
112
|
+
if (value.flows !== undefined) {
|
|
113
|
+
if (!isRecord(value.flows)) {
|
|
114
|
+
throw new ReviewGradingError("flows must be a JSON object");
|
|
115
|
+
}
|
|
116
|
+
for (const [name, entry] of Object.entries(value.flows)) {
|
|
117
|
+
flows[name] = parseGrading(entry, `flows.${name}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
default:
|
|
122
|
+
value.default === undefined ? {} : parseGrading(value.default, "default"),
|
|
123
|
+
flows,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A flow's entry over the default, field by field; levels merge per level. */
|
|
128
|
+
export function mergeGrading(base: Grading, over: Grading): Grading {
|
|
129
|
+
const levels = { ...base.levels, ...over.levels };
|
|
130
|
+
const rules = over.rules ?? base.rules;
|
|
131
|
+
const fixLevel = over.fixLevel ?? base.fixLevel;
|
|
132
|
+
return {
|
|
133
|
+
...(Object.keys(levels).length === 0 ? {} : { levels }),
|
|
134
|
+
...(rules === undefined ? {} : { rules }),
|
|
135
|
+
...(fixLevel === undefined ? {} : { fixLevel }),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Reads the repository's review.json and returns the grading of one flow. A
|
|
141
|
+
* missing file gives an empty grading; an invalid one throws a
|
|
142
|
+
* `ReviewGradingError` that names the file.
|
|
143
|
+
*/
|
|
144
|
+
export function readReviewGrading(
|
|
145
|
+
repositoryPath: string,
|
|
146
|
+
flowName: string,
|
|
147
|
+
): Grading {
|
|
148
|
+
const path = join(repositoryPath, REVIEW_GRADING_FILE);
|
|
149
|
+
if (!existsSync(path)) return {};
|
|
150
|
+
let text: string;
|
|
151
|
+
try {
|
|
152
|
+
text = readFileSync(path, "utf8");
|
|
153
|
+
} catch (error) {
|
|
154
|
+
throw new ReviewGradingError(`cannot read ${path}: ${String(error)}`);
|
|
155
|
+
}
|
|
156
|
+
let parsed: unknown;
|
|
157
|
+
try {
|
|
158
|
+
parsed = JSON.parse(text);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
throw new ReviewGradingError(`invalid JSON in ${path}: ${String(error)}`);
|
|
161
|
+
}
|
|
162
|
+
let file;
|
|
163
|
+
try {
|
|
164
|
+
file = validateReviewGrading(parsed);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (error instanceof ReviewGradingError) {
|
|
167
|
+
throw new ReviewGradingError(
|
|
168
|
+
`invalid ${path}: ${error.message.slice("review.json: ".length)}`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
const own = Object.hasOwn(file.flows, flowName) ? file.flows[flowName] : undefined;
|
|
174
|
+
return mergeGrading(file.default, own ?? {});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The strictest fix level the file sets anywhere: in `default` or in any
|
|
179
|
+
* flow's entry. A caller whose flow has no level of its own uses this one
|
|
180
|
+
* rather than a weaker level or none. Undefined when the file sets none.
|
|
181
|
+
*/
|
|
182
|
+
export function strictestFixLevel(repositoryPath: string): Level | undefined {
|
|
183
|
+
const path = join(repositoryPath, REVIEW_GRADING_FILE);
|
|
184
|
+
if (!existsSync(path)) return undefined;
|
|
185
|
+
let file;
|
|
186
|
+
try {
|
|
187
|
+
file = validateReviewGrading(JSON.parse(readFileSync(path, "utf8")));
|
|
188
|
+
} catch (error) {
|
|
189
|
+
throw new ReviewGradingError(`invalid ${path}: ${String(error)}`);
|
|
190
|
+
}
|
|
191
|
+
const set = [file.default, ...Object.values(file.flows)]
|
|
192
|
+
.map((grading) => grading.fixLevel)
|
|
193
|
+
.filter((level): level is Level => level !== undefined);
|
|
194
|
+
// LEVELS runs from critical to minor; the lowest threshold is the strictest.
|
|
195
|
+
return set.sort((a, b) => LEVELS.indexOf(b) - LEVELS.indexOf(a))[0];
|
|
196
|
+
}
|