-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
578 lines (527 loc) · 16.5 KB
/
index.js
File metadata and controls
578 lines (527 loc) · 16.5 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const path = require("path");
const fm = require('front-matter');
const yaml = require('js-yaml');
const validate = require('jsonschema').validate;
var Validator = require('jsonschema').Validator;
const { execPath } = require('process');
//Setup Variables
const pathStart=process.env.GITHUB_WORKSPACE;
const exemptPathFromHome=[".jekyll-cache","_site",".github"];
const contributorPath='contributors/contributors.json'
const QuizPath='quiz/questions'
const DataPath='_data'
//Schemas for the JSON ans YML files
var ContributorSchema={"type": "array",
"items":{
"type":"object",
"properties":{
"name":{"type":"string"},
"image":{"type":"string"},
"github":{"type":"string"},
"discord":{"type":"string"},
"nick":{"type":"string"}
},
"required":["name","image"],
"additionalProperties":{
"type":["string",
{
"type":"object",
"properties":{
"value":{"type":"string"},
"link":{"type":"string"}
}
}
]
}
}
}
var dataSchema={
"type":"object",
"properties":{
"code":{"type":"string"},
"description":{"type":"string"},
"Notes":{
"type":"array",
"items":{
"type":"object",
"properties":{
"name":{"type":"string"},
"description":{"type":"string"},
"order":{
"type":"array",
"items":{"type":"string"}
},
"link":{"type":"string"},
"authors":{
"type":"array",
"items":{"type":"string"}
},
"contributors":{
"type":"array",
"items":{"type":"string"}
}
},
"required":["name","description"]
}
},
"CribSheets":{
"type":"array",
"items":{
"type":"object",
"properties":{
"name":{"type":"string"},
"description":{"type":"string"},
"link":{"type":"string"},
"authors":{
"type":"array",
"items":{"type":"string"}
},
"contributors":{
"type":"array",
"items":{"type":"string"}
}
},
"required":["name","description"]
}
},
"Questions":{
"type":"object",
"properties":{
"QUIZAvailable":{"type":"boolean"},
"altQuestions":{
"type":"array",
"items":{
"type":"object",
"properties":{
"name":{"type":"string"},
"description":{"type":"string"},
"link":{"type":"string"},
"authors":{
"type":"array",
"items":{"type":"string"}
},
"contributors":{
"type":"array",
"items":{"type":"string"}
}
},
"required":["name","description"]
}
}
}
}
},
"required":["code","description"]
}
var questionSchema={
"id":"/QuestionStructure",
"type":"object",
"properties":{
"question":{"type":["string",
{"type":"array",
"items":{"type":"string"}
}
]},
"questionImg":{"type":"string"},
"answerImg":{"type":"string"},
"answer":{"type":["string",
{"type":"array",
"items":{"type":"string"}
}
]},
"marks":{"type":"integer"},
"topics":{
"type":"array",
"items":{"type":"string"}
},
"author":{
"type":"array",
"items":{"type":"string"}
},
"contributors":{
"type":"array",
"items":{"type":"string"}
}
},
"required":["question","answer","marks","topics"]
}
var quizSchema={
"type":"object",
"properties":{
"title":{"type":"string"},
"name":{"type":"string"},
"structure":{
"type":"array",
"items":{"$ref":"/CourseStructure"}
},
"extSites":{
"type":"array",
"items":{
"type":"object",
"properties":{
"name":{"type":"string"},
"description":{"type":"string"},
"link":{"type":"string"},
"answerType":{
"type":"string",
"pattern":"[NCW][NCW]?[NCW]?"
},
"topics":{
"type":"array",
"items":{"type":"string"}
}
},
"required":["name","description","link","answerType","topics"]
}
},
"recall":{
"type":"array",
"items":{"$ref":"/QuestionStructure"}
},
"understanding":{
"type":"array",
"items":{"$ref":"/QuestionStructure"}
}
},
"required":["title","name","structure"]
}
var structureSchema={
"id":"/CourseStructure",
"type":"Object",
"properties":{
"name":{"type":"string"},
"subTopics":{
"type":"array",
"items":{"$ref":"/CourseStructure"}
}
},
"required":["name"]
}
//Recursively read directories and return all files
// dir - directory to be searched
// except - path of any exempt directories
// result - function to call result(err,files) with a result
function readDirs(dir,exempt,result){
var results=[];
//using fs.readdir
fs.readdir(dir, function(err,files){
if (err){result(err);}
var count=files.length;
if (!count){result(null,results);}
for (var file of files){
var filePath=path.resolve(dir,file);
var funcCall=(filePath)=>{
fs.stat(filePath, (err,stat) => {
if (err){result(err);}
if (!exempt.includes(filePath)){
if (stat.isDirectory()){
readDirs(filePath,exempt, function(err,tempResults){
results=results.concat(tempResults);
if (!--count){result(null,results);}
});
}else{
results.push(filePath);
if (!--count){result(null,results);}
}
}else{
if (!--count){result(null,results);}
}
});
}
funcCall(filePath);
}
});
}
function getHeadingSize(line){
//counts the number of hashes until any other character
var count=0;
for (var letter of line){
if (letter=='#'){
count+=1;
}
else{
return count;
}
}
return count;
}
//check if the line is the start or end of code and should be excluded
function multiLineCode(line){
var count=0;
for (var letter of line){
if (letter=='`'){
count+=1;
if (count==3){
return true;
}
}
else{
return false;
}
}
return false;
}
//checks the MDFiles for heading size
function checkMDFile(file){
fs.readFile(file,'utf8',(err,data)=>{
if (err){
console.error(err);
return;
}
//check if part is true in the font matter
try{
var fontMatter=fm(data);
}catch(error){
console.error("error in file:"+file+" ; "+error);
}
var hasPart=false;
if (fontMatter.attributes.part){
hasPart=true;
}
lineCount=0;
//start with a large value
headingValue=10
var lines = data.split('\n');
//flags
var warned = false;
var inComment=false;
for (const line of lines){
lineCount+=1;
//only check a line if there is a comment
if (!inComment){
var currentHeadingSize = getHeadingSize(line);
// h1 is not permitted with part
if (hasPart && currentHeadingSize==1){
core.warning("Warning files with part:true should not have Heading 1 on line "+lineCount+" of "+ file + " Start a page on `h2` or `##` ");
warned=true;
}
// A jump up of more than 1 is not permitted
if (currentHeadingSize>headingValue+1){
core.warning("Warning incorrect heading indentation on line "+lineCount+" of "+ file + " went from a h"+headingValue+" to h"+currentHeadingSize);
headingValue=currentHeadingSize;
warned=true;
}
//any other valid heading size update the headingValue
else if (currentHeadingSize!=0){
headingValue=currentHeadingSize;
}
}
//toggle in and out of a multi line comment
if (multiLineCode(line)){
inComment= !inComment;
}
}
if (!warned){
core.info("\u001b[38;5;10m Checks passed: "+file);
}
});
}
//Check the json contributors is valid
function checkContributors(file){
//schema=JSON.parse(fs.readFileSync('contributorSchema.json' ,{encoding:'utf8', flag:'r'}));
schema=ContributorSchema;
//parse the json file
instance=JSON.parse(fs.readFileSync(file ,{encoding:'utf8', flag:'r'}));
//run the validator
var validation=validate(instance,schema);
//if valid
if (validation.valid){
// show a valid message
core.info("\u001b[38;5;10m Checks passed contributors JSON")
return instance;
}else{
//display the error
for( const JsonError of validation.errors){
console.log(JsonError.property);
console.log(JsonError.message);
}
}
}
//gets the quiz topics from the structure in teh quiz files
// function getQuizTopics(topics){
// var topicName=[]
// for (const topic of topics) {
// topicName.push(topic.name);
// //checks for sub topics and adds them too
// if (topic.subTopics){
// topicName=topicName.concat(getQuizTopics(topic.subTopics));
// }
// }
// return topicName;
// }
//for recall and understanding check that each of them have contributors that are defined in contributors.json
function checkValidContributors(checking,contributors,file,description){
var error=false;
if (checking){
for (let index = 0; index < checking.length; index++) {
if (checking[index].contributors){
for (const contributor of checking[index].contributors) {
if (!contributors.includes(contributor)){
core.error("Error in file:"+file+" in "+description+"["+index+"] '"+contributor+"' is not defined in contributors/contributors.json");
error=true;
}
}
}
if (checking[index].authors){
for (const contributor of checking[index].authors) {
if (!contributors.includes(contributor)){
core.error("Error in file:"+file+" in "+description+"["+index+"] '"+contributor+"' is not defined in contributors/contributors.json");
error=true;
}
}
}
}
}
return error;
}
// function checkQuizFile(file,contributors){
// //quizSchema=JSON.parse(fs.readFileSync('quizSchema.json' ,{encoding:'utf8', flag:'r'}));
// //questionSchema=JSON.parse(fs.readFileSync('questionSchema.json' ,{encoding:'utf8', flag:'r'}));
// //structureSchema=JSON.parse(fs.readFileSync('structureSchema.json' ,{encoding:'utf8', flag:'r'}));
// instance=JSON.parse(fs.readFileSync(file ,{encoding:'utf8', flag:'r'}));
// var v = new Validator();
// v.addSchema(questionSchema, '/QuestionStructure');
// v.addSchema(structureSchema, '/CourseStructure');
// validation=v.validate(instance, quizSchema);
// if (validation.valid){
// //check for topics
// var error=false;
// var topics=getQuizTopics(instance.structure);
// for (let index = 0; index < instance.extSites.length; index++) {
// for (const siteTopic of instance.extSites[index].topics) {
// if (!topics.includes(siteTopic)){
// core.error("Error in quiz JSON"+file+" in extSites["+index+"] '"+siteTopic+"' is not defined in structure");
// error=true;
// }
// }
// }
// for (let index = 0; index < instance.recall.length; index++) {
// for (const questionTopic of instance.recall[index].topics) {
// if (!topics.includes(questionTopic)){
// core.error("Error in quiz JSON"+file+" in recall["+index+"] '"+questionTopic+"' is not defined in structure");
// error=true;
// }
// }
// }
// for (let index = 0; index < instance.understanding.length; index++) {
// for (const questionTopic of instance.extSites[index].topics) {
// if (!topics.includes(questionTopic)){
// core.error("Error in quiz JSON"+file+" in understanding["+index+"] '"+questionTopic+"' is not defined in structure");
// error=true;
// }
// }
// }
// //check the contributors are defined
// error = error || checkValidContributors(instance.recall,contributors,file,"recall");
// error = error || checkValidContributors(instance.understanding,contributors,file,"understanding");
// if (!error){
// core.info("\u001b[38;5;10m Checks passed quiz JSON "+file);
// }
// return instance;
// }else{
// core.error("Error(s) in quiz JSON "+file);
// for( const JsonError of validation.errors){
// console.log(JsonError.property);
// console.log(JsonError.message);
// }
// }
// }
function checkDataFile(file,contributors){
//var dataSchema=JSON.parse(fs.readFileSync('dataSchema.json' ,{encoding:'utf8', flag:'r'}));
try{
var instance=yaml.load(fs.readFileSync(file ,{encoding:'utf8', flag:'r'}));
}catch(error){
console.error("error in file: "+file +":"+ error)
}
var v = new Validator();
var validation=v.validate(instance, dataSchema);
var error =false
if (validation.valid){
error = error || checkValidContributors(instance.Notes,contributors,file,"Notes");
error = error || checkValidContributors(instance.CribSheets,contributors,file,"CribSheets");
if (instance.Questions){
error = error || checkValidContributors(instance.Questions.altQuestions,contributors,file,"Questions");
}process.env.GITHUB_WORKSPACE
if (instance.Notes){
for (let index = 0; index < instance.Notes.length; index++) {
if (instance.Notes[index].order){
for (const part of instance.Notes[index].order) {
if (!part.endsWith(".html")){
core.error("Error in file:"+file+" in Notes["+index+"] '"+part+"' should end with .html ");
error=true;
}
}
}
}
}
if (!error){
core.info("\u001b[38;5;10m Checks passed data yml "+file);
}
}else{
core.error("Error(s) in quiz JSON "+file);
for( const JsonError of validation.errors){
console.log(JsonError.property);
console.log(JsonError.message);
}
}
}
var exemptPaths=[];
for (const exPath of exemptPathFromHome) {
exemptPaths.push(path.resolve(pathStart,exPath));
}
async function run(){
try {
//validate and check contributors
var contributorNames=[]
var contributors= checkContributors(path.resolve(pathStart,contributorPath));
//Check of duplicates
for (const con of contributors){
if (contributorNames.includes(con.name)){
core.setFailed("ERROR duplicate contributor name '"+con.name+"' Use a unique name, nick can be used to change display name");
}else{
contributorNames.push(con.name);
}
}
//validate and check quiz
// readDirs(path.resolve(pathStart,QuizPath),[],function(err,files){
// if (err){
// core.setFailed(err);
// }
// for(const file of files){
// if (file.endsWith(".json")){
// checkQuizFile(file,contributorNames);
// }
// }
// });
//validate and check data files
readDirs(path.resolve(pathStart,DataPath),[],function(err,files){
if (err){
core.setFailed(err);
}
for(const file of files){
if (file.endsWith(".yml")){
checkDataFile(file,contributorNames);
}
}
});
//validate and check headings for the markdown files
readDirs(pathStart,exemptPaths,function(err,files){
if (err){
core.setFailed(err);
}
for(const file of files){
if (file.endsWith(".md")){
checkMDFile(file);
}
}
});
}
catch (error) {
console.error(error);
core.setFailed(error.message);
}
}
run();