MCPcopy
hub / github.com/ampproject/amphtml / checkFile

Function checkFile

build-system/tasks/check-owners.js:39–94  ·  view source on GitHub ↗

* Checks a single OWNERS file using the owners bot API. * @param {string} file * @return {Promise }

(file)

Source from the content-addressed store, hash-verified

37 * @return {Promise<void>}
38 */
39async function checkFile(file) {
40 if (!file.endsWith('OWNERS')) {
41 log(red('ERROR:'), cyan(file), 'is not an', cyan('OWNERS'), 'file.');
42 process.exitCode = 1;
43 return;
44 }
45
46 const contents = fs.readFileSync(file, 'utf8').toString();
47 try {
48 JSON5.parse(contents);
49 logLocalDev(green('SUCCESS:'), 'No errors in', cyan(file));
50 } catch {
51 log(red('FAILURE:'), 'Found errors in', cyan(file));
52 process.exitCode = 1;
53 }
54
55 try {
56 const response = await fetch(OWNERS_SYNTAX_CHECK_URI, {
57 method: 'POST',
58 headers: {
59 'Accept': 'application/json',
60 'Content-Type': 'application/json',
61 },
62 body: JSON.stringify({path: file, contents}),
63 });
64
65 if (!response.ok) {
66 log(red('ERROR:'), 'Could not reach the owners syntax check API');
67 throw new Error(
68 `${response.status} ${response.statusText}: ${await response.text()}`
69 );
70 }
71
72 const {fileErrors, requestErrors, rules} = await response.json();
73
74 if (requestErrors) {
75 requestErrors.forEach((err) => log(red(err)));
76 throw new Error('Could not reach the owners syntax check API');
77 } else if (fileErrors && fileErrors.length) {
78 fileErrors.forEach((err) => log(red(err)));
79 throw new Error(`Errors encountered parsing "${file}"`);
80 }
81
82 log(
83 green('SUCCESS:'),
84 'Parsed',
85 cyan(file),
86 'successfully; produced',
87 cyan(rules.length),
88 'rule(s).'
89 );
90 } catch (error) {
91 log(red('FAILURE:'), error);
92 process.exitCode = 1;
93 }
94}
95
96module.exports = {

Callers 1

checkOwnersFunction · 0.85

Calls 12

redFunction · 0.85
cyanFunction · 0.85
logLocalDevFunction · 0.85
greenFunction · 0.85
fetchFunction · 0.85
stringifyMethod · 0.80
logFunction · 0.50
toStringMethod · 0.45
parseMethod · 0.45
textMethod · 0.45
jsonMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected