This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathgenerate-ci-comment.js
89 lines (83 loc) · 2.87 KB
/
generate-ci-comment.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
const fs = require('fs')
const { execSync } = require('child_process')
const indent = require('indent-string')
const escapeHtml = require('escape-html')
console.error(
'# Please pipe the output of this command to "tmp/ci-comment.txt"'
)
console.error(`# so that the "post-ci-comment.js" works!`)
const buildUrl = `https://circleci.com/gh/ThaiProgrammer/tech-events-calendar/${
process.env.CIRCLE_BUILD_NUM
}`
const checkBuildLog = `[โปรดตรวจสอบข้อมูล Log ใน CircleCI (Please check the logs in CircleCI).](${buildUrl})`
if (!fs.existsSync('public/calendar.json')) {
console.log(
':rotating_light: **ไม่สามารถประมวลผลไฟล์ข้อมูลได้ (Cannot process data file.)**'
)
console.log(checkBuildLog)
console.log()
} else if (!fs.existsSync('public/calendar.ics')) {
console.log(
':rotating_light: **ไม่สามารถสร้างไฟล์ปฏิทินได้ (Cannot generate ICS file.)**'
)
console.log(checkBuildLog)
console.log()
}
if (fs.existsSync('tmp/readme-parse-diagnostic.json')) {
const diagnostics = JSON.parse(
fs.readFileSync('tmp/readme-parse-diagnostic.json', 'utf8')
)
if (diagnostics.errors && diagnostics.errors.length) {
console.log(':x: **ข้อผิดพลาดในการประมวลผลไฟล์ (Processing errors):**')
console.log()
for (const error of diagnostics.errors) {
console.log(
` - **${error.location.filename}**\n\n${escapeHtml(
indent(error.message, 4)
)}`
)
console.log()
}
console.log()
}
}
if (fs.existsSync('public/calendar.json')) {
console.log(
':green_heart: **ประมวลผลข้อมูลสำเร็จ (Calendar data processed successfully!)**'
)
console.log()
try {
console.error('# Now diffing calendar.json file.')
execSync(
'curl https://thaiprogrammer-tech-events-calendar.spacet.me/calendar.json > /tmp/master-calendar.json',
{ timeout: 10000 }
)
const diffResult = execSync(
'diff -u /tmp/master-calendar.json public/calendar.json || true',
{ timeout: 2000 }
).toString()
if (diffResult.trim()) {
console.log(
':bulb: **ข้อมูลปฏิทินมีการเปลี่ยนแปลง (Calendar data has been changed):**'
)
console.log('```diff')
console.log(diffResult)
console.log('```')
} else {
console.log(
':bulb: **ข้อมูลปฏิทินเหมือนเดิม (Calendar data unchanged.)**'
)
}
console.log()
} catch (e) {
logError('Failed to generate JSON diff.', e)
}
}
function logError(m, e) {
console.log(`:x: **${m}**`)
console.log('Error:')
console.log('```')
console.log(e.stack)
console.log('```')
console.log()
}