forked from Adventech/sabbath-school-lessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.js
102 lines (85 loc) · 3.58 KB
/
verify.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
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
const DATE_FORMAT = "DD/MM/YYYY"
const { getCompilationQuarterValue } = require('./deploy-helper')
let metaMarked = require("meta-marked"),
fs = require('fs-extra'),
yamljs = require("js-yaml"),
glob = require("glob"),
moment = require('moment'),
axios = require('axios'),
core = require('@actions/core');
let prNum = false
if (process && process.env && process.env.GITHUB_EVENT_PATH && fs.pathExistsSync(process.env.GITHUB_EVENT_PATH)) {
let ev = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'));
prNum = ev.pull_request.number || true
}
let failMessages = []
let fail = function (message) {
failMessages.push(message);
}
/**
* Validate quarterly content to be complete, contain proper dates
* @returns {Promise<void>}
*/
let validateContent = async function () {
let quarterliesList = glob.sync("src/**/"+getCompilationQuarterValue()+"?(-cq|-er)")
for (let quarterly of quarterliesList) {
let validDate = null
let doc = null
try {
doc = yamljs.load(fs.readFileSync(`${quarterly}/info.yml`));
} catch (e) {
e = e.toString().replace(/\n/g, '<br>');
fail(`Critical error. Can not parse the quarterly info: \`${quarterly}\`/info.yml. Error: \`${e}\``);
break
}
try {
validDate = moment(doc["start_date"], DATE_FORMAT).add(-1, 'd');
} catch (e) {
fail(`Critical error. Can not obtain start date for the quarterly: \`${quarterly}\`/info.yml. Error: \`${e}\``);
break
}
let markdownFiles = glob.sync(`${quarterly}/+(0|1|2|3|4|5|6|7|8|9)/*.md`);
if (markdownFiles.filter((f) => { return /\d{2}\.md$/img.test(f) }).length < 91) {
if (!fs.pathExistsSync(`${quarterly}/pdf.yml`)) {
fail(`Incomplete quarterly \`${quarterly}\`. Expecting markdown for all 7 days for each 13 weeks`);
}
}
for (let markdownFile of markdownFiles) {
try {
if (/\d{2}\.md$/.test(markdownFile)) {
validDate.add(1, 'd')
}
let content = metaMarked(fs.readFileSync(markdownFile, "utf-8"))
let contentDate = moment(content.meta.date, DATE_FORMAT)
if (contentDate.format(DATE_FORMAT) !== validDate.format(DATE_FORMAT)) {
fail(`Potential error in the date field: \`${markdownFile}\`. Expected: \`${validDate.format(DATE_FORMAT)}\`, got: \`${contentDate.format(DATE_FORMAT)}\``)
}
} catch (e) {
e = e.toString().replace(/\n/g, '<br>');
fail(`Error parsing: \`${markdownFile}\`. Error: ${e}`);
}
}
}
}
validateContent().then(async function (){
if (failMessages.length) {
let pullRequestComment = "Ooops! Looks like you have to fix some issues before I can merge this PR\n"
pullRequestComment += "||Error description |\n| ----------- | ----------- |"
for (let message of failMessages) {
pullRequestComment += `\n|🛑| ${message}|`
}
if (!prNum) {
console.error(pullRequestComment)
return
}
await axios({
method: 'post',
url: 'https://fh83o4b1ph.execute-api.us-east-1.amazonaws.com/default/SabbathSchoolPullRequestPost',
data: {
pullRequestID: prNum,
pullRequestComment: pullRequestComment
}
});
core.setFailed("Ooops! Looks like you have to fix some issues before I can merge this PR");
}
})