-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync-to-issue.js
108 lines (94 loc) · 2.61 KB
/
sync-to-issue.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
const fs = require('fs');
const https = require('https');
const assert = require('assert');
const config = {
urls: {
updateIssue: '/repos/yidinghan/blog/issues/',
},
articleLinkPrefix: 'https://github.com/yidinghan/blog/blob/master/',
filename: '',
filepath: '',
issue: {
id: 0,
title: '',
body: '',
},
httpsOptions: {
protocol: 'https:',
host: 'api.github.com',
port: '443',
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Ding Blog',
},
},
user: {
un: '',
pw: '',
},
};
const parseArgv = () => {
const filename = process.argv[2];
assert(filename, 'filename is empty');
const filepath = `${__dirname}/${filename}`;
const isFileExists = fs.existsSync(filepath);
assert(isFileExists, `${filename} not exists`);
const issueID = parseInt(process.argv[3]);
assert(issueID, 'issueID is empty');
const un = process.env['un'];
assert(un, 'un is empty');
const pw = process.env['pw'];
assert(pw, 'pw is empty');
config.filename = filename;
config.filepath = filepath;
config.issue.id = issueID;
config.issue.title = filename.split('.')[0].replace(/-/g, ' ');
config.user = { un, pw };
};
const parseIssueBody = () => {
const fileContent = fs.readFileSync(config.filepath).toString();
const articleLink = config.articleLinkPrefix + config.filename;
const refContent = `Article Reference [Link](${articleLink})`;
config.issue.body = [refContent, fileContent].join('\n\n');
};
const parseHttpOptions = () => {
config.httpsOptions = Object.assign(
{
path: config.urls.updateIssue + String(config.issue.id),
auth: `${config.user.un}:${config.user.pw}`,
},
config.httpsOptions
);
};
const makeRequest = callback => {
const req = https.request(config.httpsOptions, res => {
let body = '';
res.on('data', chuck => {
body += chuck;
});
res.on('end', () => {
console.log({ body, code: res.statusCode });
callback(0);
});
res.on('err', err => {
console.log({ err });
callback(1);
});
});
const body = {
title: config.issue.title,
body: config.issue.body,
};
req.write(JSON.stringify(body));
req.end();
};
(() => {
parseArgv();
parseIssueBody();
parseHttpOptions();
console.log(config.httpsOptions);
makeRequest((code = 0) => {
process.exit(code);
});
})();