-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluator.js
53 lines (42 loc) · 1.4 KB
/
evaluator.js
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
const fs = require('fs')
const CORRECT_ANSWER_GRADE = 3;
const WRONG_ANSWER_GRADE = 1;
const githubUsername = process.env.INPUT_PR_AUTHOR_USERNAME || 'no_actor';
const githubRepositoryName = process.env.GITHUB_REPOSITORY || 'no_repository';
// https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/
const evaluationFileContent = fs.readFileSync(process.argv[2]);
const testData = JSON.parse(evaluationFileContent);
const leafSuites = (suites, leaves = []) => {
suites.forEach((suite) => {
if (suite.suites.length === 0) {
leaves.push(suite);
return;
}
leafSuites(suite.suites, leaves);
});
return leaves.flat();
};
const evaluationsByRequirements =
leafSuites(testData.results).reduce(
(acc, { title, tests, passes }) => {
const allUnitTestsPassed = tests.length === passes.length;
acc[title] = allUnitTestsPassed;
return acc;
},
{}
);
const requirementsFile = fs.readFileSync(process.argv[3]);
const { requirements } = JSON.parse(requirementsFile);
const evaluations =
requirements.map(({ description }) => (
{
description,
grade: evaluationsByRequirements[description] ? CORRECT_ANSWER_GRADE : WRONG_ANSWER_GRADE
}
));
fs.writeFileSync(process.argv[4], JSON.stringify({
github_username: githubUsername,
github_repository_name: githubRepositoryName,
evaluations: [...evaluations]
}));
process.exit();