-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
84 lines (71 loc) · 2.03 KB
/
main.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
const error = require('./error.js');
const core = require('@actions/core')
const {GitHub, context} = require('@actions/github')
const os = require('os');
const path = require('path');
const fs = require('fs');
const request = require('request');
function findAsset(assets, re){
list = []
var regex = RegExp(re);
for(var i in assets){
if(regex.test(assets[i].name)) {
list.push({
url: assets[i].url,
name: assets[i].name
})
}
}
if (list.length == 0) {
throw `${name} not found in assets`;
}
return list;
}
async function downloadAsset(asset, token, dir) {
var filePath = path.resolve(dir, asset.name);
const options = {
url: asset.url,
headers: {
'User-Agent': 'request',
'Accept': 'application/octet-stream',
'Authorization': `token ${token}`
}
};
console.log(`Downloading ${asset.name}`);
var stream = request(options).pipe(fs.createWriteStream(filePath))
return new Promise((resolve) => {
stream.on('finish', ()=>{ resolve(filePath) });
});
}
function createDir(dir) {
if (!dir) {
dir = os.tmpdir();
}
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
return dir
}
async function main() {
const regex = core.getInput('regex');
const token = core.getInput('token');
const downloadPath = createDir(core.getInput('dir'));
const octokit = new GitHub(token);
if (! context.payload.release) {
core.warning("Not running action as a release, skipping.");
return;
}
const release = context.payload.release;
//const octokit = new GitHub(process.env.GITHUB_TOKEN);
//var response = await octokit.repos.getReleaseByTag({ owner: "RadiusNetworks", repo: "iris-ios", tag: "app-v0.1" })
//var release = response.data
var assets = findAsset(release.assets, regex)
paths = [];
for (var i in assets) {
var newPath = await downloadAsset(assets[i], token, downloadPath)
paths.push(newPath);
}
core.setOutput('asset_paths',paths.join("\n"));
core.setOutput('dir',path.resolve(downloadPath));
}
main().catch(error.handle);