This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
135 lines (120 loc) · 4.14 KB
/
index.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
var CircleCI = require('circleci');
var program = require('commander');
var https = require('https');
var fs = require('fs');
var mkdirp = require('mkdirp');
var path = require('path');
program
.version('0.0.11')
.option('-t, --token [value]', '(Required) CircleCI Authentication Token')
.option('-b, --buildnum <n>', 'Specify a build number, or we take latest', parseInt)
.option('-u, --user [value]', '(Required) Build git repository username')
.option('-p, --project [value]', '(Required) Build git repository project')
.option('--branch [value]', 'Branch to get builds from')
.option('--path [value]', 'Path of files to download')
.option('--outputdir [value]', 'path of local output directory output artifacts defaults to "./"')
.option('--config [value]', 'specify a json config')
.option('--print-paths', 'print file paths')
program.parse(process.argv);
//check for required params
if(program.config){
var config = require(path.resolve(process.cwd(), program.config));
console.log(config);
}
if(!program.user || !program.project || !program.token){
program.help();
}
var outputdir = program.outputdir || './';
var buildnum = program.buildnum;
const ci = new CircleCI({
auth: program.token
});
const params = {
user : program.user,
project : program.project,
token: program.token,
buildnum : program.buildnum,
branch: program.branch,
printPaths: program.printPaths
}
var downloadArtifacts = (filepath) => (artifacts) => {
var downloads = [];
if(!filepath){
filepath = "";
}
artifacts.map((artifact) => {
if(artifact.path.startsWith(filepath)){
downloads.push(downloadArtifact(artifact.url, `${outputdir}${artifact.path.substring(filepath.length)}`))
}
});
return Promise.all(downloads);
}
function downloadArtifact(artifactUrl, filepath){
return new Promise((resolve, reject) => {
mkdirp(path.dirname(filepath), function (err) {
if (err){
return reject(`cannot create path ${filepath} ${err}`);
}
var file = fs.createWriteStream(filepath);
var url = `${artifactUrl}?circle-token=${params.token}`;
var request = https.get(url, function(response) {
response.pipe(file);
});
file.on('close', ()=>{
if(params.printPaths){
console.log(filepath);
}
return resolve(filepath);
})
});
})
}
function getBuildArtifacts(params) {
return (build) =>{
return ci.getBuildArtifacts({
username: params.user,
project: params.project,
build_num: build.build_num
})
}
}
function resolveBuildNumber(params){
if(typeof(params.buildnum) !== 'number'){
return findLatestBuild(params);
} else {
return ci.getBuild({username: params.user, project: params.project, build_num: params.buildnum})
}
}
function latestSuccess(builds){
return new Promise((resolve, reject) => {
var i = 0;
for(i =0; i < builds.length; i++){
if(builds[i].lifecycle === 'finished' && builds[i].outcome === 'success'){
return resolve(builds[i]);
}
}
return reject('No valid builds found');
})
}
function findLatestBuild(params){
var paramsReturn = params;
if(!params.branch){
return ci.getBuilds({username: params.user, project: params.project}).then(latestSuccess);
} else {
return ci.getBranchBuilds({username: params.user, project: params.project, branch: params.branch}).then(latestSuccess)
}
}
resolveBuildNumber(params)
.then((build) => {
console.log(`Download Build ${build.build_num} - ${build.vcs_url}/commit/${build.vcs_revision} queued ${build.queued_at}`)
return build;
})
.then(getBuildArtifacts(params))
.then(downloadArtifacts(program.path))
.then((files)=>{
console.log(`Finished wrote ${files.length} files`);
})
.catch((err)=>{
console.error('Error while attempting to download artifacts: ');
console.error(err);
});