This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gist.js
64 lines (53 loc) · 1.4 KB
/
gist.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
let gistURL = process.env.INPUT_GISTURL;
let token = process.env.INPUT_TOKEN;
let file = process.env.INPUT_FILE;
let suffix = process.env.INPUT_SUFFIX;
let fs = require('fs');
let https = require('https');
console.log('Gist URL:', gistURL);
let gistID = gistURL.split('/')[4]
console.log('Gist ID is:', gistID);
let fileData = fs.readFileSync(file, 'utf-8');
let fileSplit = file.split('/');
let fileName = fileSplit[fileSplit.length-1] + (suffix || '');
let options = {
'host': 'api.github.com',
'port': '443',
'path': `/gists/${gistID}`,
'method': 'PATCH',
'headers': {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `token ${token}`,
'Content-Type': 'application/json',
'User-Agent': 'actions/get-gist-action'
}
}
if (!fileData) {
console.log('No data in the file, aborting.')
process.exit(1)
}
let data = {
"files": {
[fileName]: {
"content": fileData,
"filename": fileName
}
}
}
let req = https.request(options, (resp) => {
if (resp.statusCode !== 200) {
console.log(`Got an error: ${resp.statusCode}`);
process.exit(1)
}
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
console.log(`Gist updated, written: ${fileName}`);
});
}).on("error", (err) => {
console.log(`Error getting gist: ${err.message}`);
});
req.write(JSON.stringify(data));
req.end();