-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateBlog.js
More file actions
163 lines (150 loc) · 4.72 KB
/
updateBlog.js
File metadata and controls
163 lines (150 loc) · 4.72 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
var fs = require('fs');
const moment = require('moment');
const axios = require('axios').default;
const { Octokit } = require('octokit');
require('dotenv').config();
//Parse key and value seperated by ':'
function parseKeyValue(str) {
var arr = str.toString().split(':');
return {
key: arr[0].trim(),
value: arr[1].trim(),
};
}
//Convert object array which has key and value to object (This comment exist because co-pilot)
function convertToObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; i++) {
obj[arr[i].key] = arr[i].value;
}
return obj;
}
//Check object has [publisher, title, description, date] keys
function checkObject(obj) {
var keys = ['publisher', 'title', 'description', 'date', 'updated_at', 'updated_by'];
for (var i = 0; i < keys.length; i++) {
if (!obj.hasOwnProperty(keys[i])) {
return false;
}
}
return true;
}
function whichLineEnding(source) {
var temp = source.indexOf('\n');
if (source[temp - 1] === '\r') return '\r\n';
return '\n';
}
var blogJSON = require('./blog.json');
//Loop over files in fileDir
const octokit = new Octokit({
auth: process.env.API_KEY,
});
octokit.rest.users.getAuthenticated().then(loop);
async function createDiscussion(title, description) {
const disq_id = await octokit.graphql(
`mutation {
createDiscussion(input: {repositoryId: "R_kgDOGdv8Ow", categoryId: "DIC_kwDOGdv8O84CAHDn", body: "${description}", title: "${title}"}) {
discussion {
id
}
}
}`
);
return disq_id;
}
async function readFile(fileLink) {
var file = fs.readFileSync(fileLink, 'utf8');
var lineEnding = whichLineEnding(fileLink);
var lines = file.split(lineEnding);
if (lines[0] != '---' || lines[7] != '---') {
return {
err: true,
message: 'File ' + file + ' is not a valid blog file [config-error]',
};
} else {
var conf = lines.slice(1, 7);
var md = lines.slice(8);
var parsedConf = convertToObject(conf.map((x) => parseKeyValue(x)));
var correct = checkObject(parsedConf);
if (correct) {
if (moment(parsedConf.date, 'DD.MM.YYYY-hh.mm').isValid()) {
return {
err: false,
parsedConf,
md,
};
} else {
return {
err: true,
message: 'File ' + file + ' has invalid date',
};
}
} else {
return {
err: true,
message: 'File ' + file + ' has invalid config',
};
}
}
}
async function postWebhook(title, description, publisher, url) {
return axios.post(process.env.WEBHOOKURL, {
username: 'EllieBlog',
avatar_url: 'https://www.ellie-lang.org/img/EllieCharIcon.png',
embeds: [
{
title: title,
type: 'article',
description: description,
thumbnail: {
url: 'https://www.ellie-lang.org/img/EllieCharIcon.png',
},
color: '16181999',
author: {
name: publisher.replace('@', ''),
url: publisher.replace('@', 'https://github.com/'),
icon_url: publisher.replace('@', 'https://github.com/') + '.png',
},
url,
},
],
});
}
async function loop() {
var fileDir = fs.readdirSync('./blogs/');
let files = [];
for (var i = 0; i < fileDir.length; i++) {
var file = await readFile('./blogs/' + fileDir[i]);
if (file.err) {
console.log(file.message);
} else {
var id = moment(file.parsedConf.date, 'DD.MM.YYYY-hh.mm').format('x');
let title = `${file.parsedConf.title}@${id.toString(16)}`;
let disq_id;
if (!blogJSON.find((x) => x.id === id)) {
disq_id = await createDiscussion(title, file.parsedConf.description);
await postWebhook(
file.parsedConf.title,
file.parsedConf.description,
file.parsedConf.publisher,
'https://www.ellie-lang.org/blog/' + fileDir[i].replace('.md', '')
);
} else {
disq_id = blogJSON.find((x) => x.id === id).disq_id;
}
let blogData = {
id,
disq_id,
title,
publisher: file.parsedConf.publisher,
updated_at: file.parsedConf.updated_at,
updated_by: file.parsedConf.updated_by,
description: file.parsedConf.description,
date: moment(file.parsedConf.date, 'DD.MM.YYYY-hh.mm').format(),
file_name: fileDir[i],
};
files.push(blogData);
}
}
fs.writeFileSync('./blog.json', JSON.stringify(files));
}