Repository navigation
Expand file tree
/
Copy pathvalidate-plugin.mjs
More file actions
250 lines (210 loc) · 7.74 KB
/
Copy pathvalidate-plugin.mjs
File metadata and controls
250 lines (210 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import path from "node:path";
import process from "node:process";
const root = process.cwd();
const failures = [];
const expectedPluginDescription =
"Run CodeRabbit reviews for code, PR, security, and quality checks, plus guarded autofix for unresolved GitHub PR feedback in Cursor.";
function fail(message) {
failures.push(message);
}
function readJson(relativePath) {
const absolutePath = path.join(root, relativePath);
try {
return JSON.parse(readFileSync(absolutePath, "utf8"));
} catch (error) {
fail(`${relativePath}: ${error.message}`);
return null;
}
}
function isSafeRelative(value) {
return typeof value === "string" && value.length > 0 && !path.isAbsolute(value) && !value.split(/[\\/]/).includes("..");
}
function assertPathExists(owner, value) {
if (!isSafeRelative(value)) {
fail(`${owner}: path must be relative and must not contain parent traversal: ${value}`);
return;
}
const clean = value.replace(/^\.\//, "");
if (!existsSync(path.join(root, clean))) {
fail(`${owner}: path does not exist: ${value}`);
}
}
function parseFrontmatter(relativePath) {
const text = readFileSync(path.join(root, relativePath), "utf8");
if (!text.startsWith("---\n")) {
fail(`${relativePath}: missing frontmatter`);
return {};
}
const end = text.indexOf("\n---", 4);
if (end === -1) {
fail(`${relativePath}: frontmatter is not closed`);
return {};
}
const body = text.slice(4, end).split("\n");
const data = {};
for (const line of body) {
const match = line.match(/^([A-Za-z0-9_-]+):\s*(.*)$/);
if (match) {
data[match[1]] = match[2].replace(/^["']|["']$/g, "");
}
}
return data;
}
function walk(relativeDir) {
const absoluteDir = path.join(root, relativeDir);
if (!existsSync(absoluteDir)) {
return [];
}
const files = [];
for (const entry of readdirSync(absoluteDir)) {
const relativePath = path.join(relativeDir, entry);
const absolutePath = path.join(root, relativePath);
if (statSync(absolutePath).isDirectory()) {
files.push(...walk(relativePath));
} else {
files.push(relativePath);
}
}
return files;
}
function requireFrontmatterFields(relativePath, fields) {
const data = parseFrontmatter(relativePath);
for (const field of fields) {
if (!Object.prototype.hasOwnProperty.call(data, field) || data[field] === "") {
fail(`${relativePath}: missing frontmatter field ${field}`);
}
}
}
function checkNoEmDashes() {
const textExtensions = new Set([".json", ".md", ".mdc", ".mjs", ".yml", ".yaml", ".txt"]);
for (const relativePath of walk(".")) {
if (relativePath.includes("node_modules")) {
continue;
}
if (!textExtensions.has(path.extname(relativePath))) {
continue;
}
const text = readFileSync(path.join(root, relativePath), "utf8");
if (text.includes("\u2014")) {
fail(`${relativePath}: contains an em dash`);
}
}
}
const plugin = readJson(".cursor-plugin/plugin.json");
if (plugin) {
for (const field of ["name", "displayName", "version", "description", "author", "publisher", "license"]) {
if (!plugin[field]) {
fail(`.cursor-plugin/plugin.json: missing ${field}`);
}
}
if (!/^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$/.test(plugin.name || "")) {
fail(".cursor-plugin/plugin.json: name must be lowercase and plugin-safe");
}
if (plugin.displayName !== "CodeRabbit") {
fail('.cursor-plugin/plugin.json: displayName must be "CodeRabbit"');
}
if (plugin.author?.name !== "CodeRabbit") {
fail('.cursor-plugin/plugin.json: author.name must be "CodeRabbit"');
}
if (plugin.publisher !== "CodeRabbit") {
fail('.cursor-plugin/plugin.json: publisher must be "CodeRabbit"');
}
if (plugin.description !== expectedPluginDescription) {
fail(`.cursor-plugin/plugin.json: description must be "${expectedPluginDescription}"`);
}
for (const field of ["logo", "skills", "agents", "commands", "rules", "hooks"]) {
const value = plugin[field];
if (Array.isArray(value)) {
value.forEach((item) => assertPathExists(`.cursor-plugin/plugin.json ${field}`, item));
} else if (typeof value === "string" && !/^https?:\/\//.test(value)) {
assertPathExists(`.cursor-plugin/plugin.json ${field}`, value);
}
}
}
const marketplace = readJson(".cursor-plugin/marketplace.json");
if (marketplace) {
if (!marketplace.name) {
fail(".cursor-plugin/marketplace.json: missing name");
}
if (!Array.isArray(marketplace.plugins) || marketplace.plugins.length === 0) {
fail(".cursor-plugin/marketplace.json: plugins must contain at least one plugin");
} else {
if (marketplace.owner?.name !== "CodeRabbit") {
fail('.cursor-plugin/marketplace.json: owner.name must be "CodeRabbit"');
}
if (marketplace.metadata?.description !== expectedPluginDescription) {
fail(`.cursor-plugin/marketplace.json: metadata.description must be "${expectedPluginDescription}"`);
}
for (const entry of marketplace.plugins) {
if (!entry.name || !entry.source) {
fail(".cursor-plugin/marketplace.json: each plugin entry needs name and source");
}
if (plugin && entry.name !== plugin.name) {
fail(`.cursor-plugin/marketplace.json: plugin entry ${entry.name} does not match ${plugin.name}`);
}
if (entry.description !== expectedPluginDescription) {
fail(`.cursor-plugin/marketplace.json: plugin entry description must be "${expectedPluginDescription}"`);
}
}
}
}
for (const file of walk("skills").filter((item) => item.endsWith("SKILL.md"))) {
requireFrontmatterFields(file, ["name", "description"]);
}
const routingRequirements = [
{ file: "skills/code-review/SKILL.md", phrases: ["any code review request", "does not mention coderabbit"] },
{ file: "agents/code-reviewer.md", phrases: ["any code review request", "does not mention coderabbit"] },
];
for (const { file, phrases } of routingRequirements) {
if (!existsSync(path.join(root, file))) {
fail(`${file}: file is required for default review routing`);
continue;
}
const description = (parseFrontmatter(file).description || "").toLowerCase();
for (const phrase of phrases) {
if (!description.includes(phrase)) {
fail(`${file}: description must keep default review routing phrase "${phrase}"`);
}
}
}
for (const file of walk("agents").filter((item) => item.endsWith(".md"))) {
requireFrontmatterFields(file, ["name", "description"]);
}
for (const file of walk("commands").filter((item) => item.endsWith(".md") || item.endsWith(".txt"))) {
requireFrontmatterFields(file, ["name", "description"]);
}
for (const file of walk("rules").filter((item) => item.endsWith(".mdc"))) {
requireFrontmatterFields(file, ["description", "alwaysApply"]);
}
if (existsSync(path.join(root, "hooks/hooks.json"))) {
const hooksConfig = readJson("hooks/hooks.json");
if (hooksConfig) {
if (hooksConfig.version !== 1) {
fail("hooks/hooks.json: version must be 1");
}
const hookEntries = Object.values(hooksConfig.hooks ?? {}).flat();
if (hookEntries.length === 0) {
fail("hooks/hooks.json: hooks must contain at least one entry");
}
for (const entry of hookEntries) {
if (!entry.command) {
fail("hooks/hooks.json: each hook entry needs a command");
continue;
}
const scriptPath = entry.command.split(/\s+/).find((part) => /\.(mjs|cjs|js|sh|py)$/.test(part));
if (scriptPath) {
assertPathExists("hooks/hooks.json command", scriptPath);
}
}
}
}
checkNoEmDashes();
if (failures.length > 0) {
console.error("Validation failed:");
for (const failure of failures) {
console.error(`- ${failure}`);
}
process.exit(1);
}
console.log("Cursor plugin validation passed.");