Repository navigation
Expand file tree
/
Copy pathvalidate-coderabbit-yml.cjs
More file actions
118 lines (107 loc) · 3.43 KB
/
Copy pathvalidate-coderabbit-yml.cjs
File metadata and controls
118 lines (107 loc) · 3.43 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
#!/usr/bin/env node
/**
* Validates `.coderabbit.yml` against the canonical CodeRabbit schema and structure.
* @module scripts/validation/validate-coderabbit-yml
* @see .coderabbit.yml
*/
const fs = require("fs");
const path = require("path");
const yaml = require("js-yaml");
const https = require("https");
// Accept file path as argument for testability
const filePath = process.argv[2]
? path.resolve(process.argv[2])
: path.resolve(__dirname, "../../.coderabbit.yml");
const schemaUrl =
"https://coderabbit.ai/integrations/coderabbit-overrides.v2.json";
const localSchemaPath = path.resolve(
__dirname,
"../../.schemas/coderabbit-overrides.v2.json",
);
const logsDir = path.resolve(__dirname, "../../logs");
const logFile = path.join(logsDir, "validate-coderabbit-yml.log");
function logToFile(message) {
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
fs.appendFileSync(logFile, `[${new Date().toISOString()}] ${message}\n`);
}
function error(msg) {
const errMsg = `Error: ${msg}`;
console.error(`\x1b[31m${errMsg}\x1b[0m`);
logToFile(errMsg);
// Print a recognizable string for test matching
console.log("Invalid .coderabbit.yml");
process.exit(1);
}
function fetchRemoteSchema(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
if (!data) return reject(new Error("Empty response from schema URL"));
try {
const json = JSON.parse(data);
resolve(json);
} catch (e) {
reject(new Error("Invalid JSON from schema URL"));
}
});
})
.on("error", (err) => {
reject(err);
});
});
}
async function main() {
if (!fs.existsSync(filePath)) {
error(`File not found: ${filePath}`);
}
const content = fs.readFileSync(filePath, "utf8");
const data = yaml.load(content);
// Always try to fetch the latest schema, fallback to local if needed
let schema = null;
let usedRemote = false;
try {
schema = await fetchRemoteSchema(schemaUrl);
// Save the latest schema locally for future runs
fs.writeFileSync(localSchemaPath, JSON.stringify(schema, null, 2));
usedRemote = true;
logToFile("Fetched latest schema from remote.");
} catch (e) {
if (fs.existsSync(localSchemaPath)) {
schema = JSON.parse(fs.readFileSync(localSchemaPath, "utf8"));
const warnMsg = `Warning: Using local schema due to fetch error: ${e.message}`;
console.warn(warnMsg);
logToFile(warnMsg);
} else {
error(`Failed to fetch schema and no local schema found: ${e.message}`);
}
}
// Basic required fields (from schema)
if (schema && Array.isArray(schema.required)) {
for (const key of schema.required) {
if (!(key in data)) {
error(`Missing required top-level field: ${key}`);
}
}
}
// Optionally, check for common subfields
if (data.reviews) {
if (!data.reviews.path_filters) {
console.warn("Warning: reviews.path_filters is missing.");
}
if (!data.reviews.auto_review) {
console.warn("Warning: reviews.auto_review is missing.");
}
}
const okMsg = `.coderabbit.yml is valid! (schema: ${usedRemote ? "remote" : "local"})`;
console.log(`\x1b[32m${okMsg}\x1b[0m`);
logToFile(okMsg);
process.exit(0);
}
main().catch((e) => error(e.message || e));