-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.js
164 lines (130 loc) · 5.07 KB
/
action.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
require('child_process')
.execSync(
'npm install @actions/core @actions/github conventional-changelog-cli [email protected]',
{ cwd: __dirname }
);
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const core = require('@actions/core');
const github = require('@actions/github');
const http = require('https');
const { parse } = require('url');
const { basename } = require('path');
const { callbackify } = require('util');
(async () => {
try {
// const api = github.getOctokit(core.getInput('token'));
const url = core.getInput('url');
// Create an array of the initial URL, which will be populated with paging URLs after initial request.
let urls = [url];
const verbose = core.getInput('verbose') == 'true'; // input is always string, not boolean.
const folder = core.getInput('folder');
var timeout = 10000;
// Options for HTTP requests against GitHub APIs. User-Agent is required.
const options = {
headers: { 'User-Agent': 'sondreb/action-release-download' }
};
info(`Url input: ${url}`);
info(`Folder input: ${folder}`);
if (!fs.existsSync(folder)) {
info(`Creating the destination folder: "${folder}".`);
fs.mkdirSync(folder);
}
function info(text, ...params) {
log(text, true, ...params);
}
function debug(text, ...params) {
log(text, verbose, ...params);
}
function log(text, enabled, ...params) {
if (enabled) {
console.log(text, ...params);
}
}
var timeout_wrapper = function (req) {
return function () {
console.log('abort');
req.abort();
// callback(true, { size: 0, downloaded: 0, progress: 0, status: 'Timeout' }, "File transfer timeout!");
};
};
function completed() {
info('All is norminal 🚀. Execution has ended.');
}
function get(url, file, callback) {
const options = {
headers: { 'User-Agent': 'sondreb/action-release-download' }
};
http.get(url, options, (res) => {
if (res.statusCode === 301 || res.statusCode === 302) {
console.log('Forwarding to: ' + res.headers.location);
return get(res.headers.location, file, callback);
}
debug(`Download completed: ${url}`);
res.pipe(file);
callback();
});
}
function downloadFiles(urls) {
var url = urls.pop();
if (!url) {
completed();
return;
}
const uri = parse(url);
const fileName = basename(uri.path);
const filePath = path.join(folder, fileName);
var file = fs.createWriteStream(filePath);
// Get the file then continue on the next.
get(url, file, () => {
downloadFiles(urls);
});
}
function getFiles(urls, files, processLinks) {
const url = urls.pop();
// When all URLs has been processes, start downloading the files.
if (!url) {
downloadFiles(files);
return;
}
http.get(url, options, res => {
let data = [];
const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';
console.log('Status Code:', res.statusCode);
console.log('Date in Response header:', headerDate);
if (processLinks) {
// Process all paging links, but only on first request.
if (res.headers.link) {
var links = res.headers.link.split(', ');
for (i = 0; i < links.length; i++) {
let link = links[i];
link = link.substring(1, link.indexOf('>'));
console.log('link:' + link);
urls.push(link);
}
}
}
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
console.log('Response ended: ');
const assets = JSON.parse(Buffer.concat(data).toString());
for (asset of assets) {
console.log(`Download: ${asset.browser_download_url}`);
files.push(asset.browser_download_url);
}
getFiles(urls, files, false);
});
}).on('error', err => {
console.log('Error: ', err.message);
});
}
const files = [];
getFiles(urls, files, true);
} catch (error) {
console.error(error);
core.setFailed(error.message);
}
})();