-
Notifications
You must be signed in to change notification settings - Fork 114
/
verify.js
164 lines (135 loc) Β· 5.96 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
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
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'),
path = require('path');
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) ? ev.pull_request.number || true : false
}
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 weeklyInfoFiles = glob.sync(`${quarterly}/+(0|1|2|3|4|5|6|7|8|9)/info.yml`);
if (weeklyInfoFiles.length < 13 && !fs.pathExistsSync(`${quarterly}/pdf.yml`)) {
fail(`One or more weekly info.yml files is missing for ${quarterly}`);
}
for (let weeklyInfoFile of weeklyInfoFiles) {
try {
doc = yamljs.load(fs.readFileSync(`${weeklyInfoFile}`));
} catch (e) {
e = e.toString().replace(/\n/g, '<br>');
fail(`Critical error. Can not parse the weekly info: \`${weeklyInfoFile}\`. 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 < 84) {
if (!fs.pathExistsSync(`${quarterly}/pdf.yml`)) {
fail(`Incomplete quarterly \`${quarterly}\`. Expecting markdown for all 7 days for each 13 weeks`);
}
}
if (fs.pathExistsSync(`${quarterly}/video.yml`)) {
try {
doc = yamljs.load(fs.readFileSync(`${quarterly}/video.yml`));
} catch (e) {
e = e.toString().replace(/\n/g, '<br>');
fail(`Critical error. Can not parse the quarterly video file: \`${quarterly}\`/video.yml. Error: \`${e}\``);
break
}
}
if (fs.pathExistsSync(`${quarterly}/pdf.yml`)) {
try {
doc = yamljs.load(fs.readFileSync(`${quarterly}/pdf.yml`));
} catch (e) {
e = e.toString().replace(/\n/g, '<br>');
fail(`Critical error. Can not parse the quarterly pdf file: \`${quarterly}\`/pdf.yml. Error: \`${e}\``);
break
}
}
if (fs.pathExistsSync(`${quarterly}/audio.yml`)) {
try {
doc = yamljs.load(fs.readFileSync(`${quarterly}/audio.yml`));
} catch (e) {
e = e.toString().replace(/\n/g, '<br>');
fail(`Critical error. Can not parse the quarterly audio file: \`${quarterly}\`/audio.yml. Error: \`${e}\``);
break
}
}
let prevMarkdownFile = null
for (let markdownFile of markdownFiles) {
try {
if (/\d{2}\.md$/.test(markdownFile)) {
validDate.add(prevMarkdownFile !== path.basename(markdownFile) ? 1 : 7, 'd')
prevMarkdownFile = path.basename(markdownFile)
}
let c = fs.readFileSync(markdownFile, "utf-8")
if (c.length > 40000) {
fail(`Potentially very long document over 40k characters: \`${markdownFile}\`.`)
}
let content = metaMarked(c)
if (!content.meta.title) {
fail(`Error in the title field: \`${markdownFile}\`. No title provided`)
}
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");
}
})