| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  | const args = require('minimist')(process.argv.slice(2)) | 
					
						
							|  |  |  | const nugget = require('nugget') | 
					
						
							|  |  |  | const request = require('request') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function makeRequest (requestOptions, parseResponse) { | 
					
						
							|  |  |  |   return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |     request(requestOptions, (err, res, body) => { | 
					
						
							|  |  |  |       if (!err && res.statusCode >= 200 && res.statusCode < 300) { | 
					
						
							|  |  |  |         if (parseResponse) { | 
					
						
							|  |  |  |           const build = JSON.parse(body) | 
					
						
							|  |  |  |           resolve(build) | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           resolve(body) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |         if (args.verbose) { | 
					
						
							|  |  |  |           console.error('Error occurred while requesting:', requestOptions.url) | 
					
						
							|  |  |  |           if (parseResponse) { | 
					
						
							|  |  |  |             try { | 
					
						
							|  |  |  |               console.log('Error: ', `(status ${res.statusCode})`, err || JSON.parse(res.body), requestOptions) | 
					
						
							|  |  |  |             } catch (err) { | 
					
						
							|  |  |  |               console.log('Error: ', `(status ${res.statusCode})`, err || res.body, requestOptions) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  |             console.log('Error: ', `(status ${res.statusCode})`, err || res.body, requestOptions) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         reject(err) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function downloadArtifact (name, buildNum, dest) { | 
					
						
							|  |  |  |   const circleArtifactUrl = `https://circleci.com/api/v1.1/project/github/electron/electron/${args.buildNum}/artifacts?circle-token=${process.env.CIRCLE_TOKEN}` | 
					
						
							|  |  |  |   const artifacts = await makeRequest({ | 
					
						
							|  |  |  |     method: 'GET', | 
					
						
							|  |  |  |     url: circleArtifactUrl, | 
					
						
							|  |  |  |     headers: { | 
					
						
							|  |  |  |       'Content-Type': 'application/json', | 
					
						
							|  |  |  |       'Accept': 'application/json' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, true).catch(err => { | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |     if (args.verbose) { | 
					
						
							|  |  |  |       console.log('Error calling CircleCI:', err) | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       console.error('Error calling CircleCI to get artifact details') | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  |   }) | 
					
						
							|  |  |  |   const artifactToDownload = artifacts.find(artifact => { | 
					
						
							|  |  |  |     return (artifact.path === name) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   if (!artifactToDownload) { | 
					
						
							|  |  |  |     console.log(`Could not find artifact called ${name} to download for build #${buildNum}.`) | 
					
						
							|  |  |  |     process.exit(1) | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     console.log(`Downloading ${artifactToDownload.url}.`) | 
					
						
							| 
									
										
										
										
											2019-05-30 20:22:35 -04:00
										 |  |  |     let downloadError = false | 
					
						
							|  |  |  |     await downloadWithRetry(artifactToDownload.url, dest).catch(err => { | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |       if (args.verbose) { | 
					
						
							|  |  |  |         console.log(`${artifactToDownload.url} could not be successfully downloaded.  Error was:`, err) | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         console.log(`${artifactToDownload.url} could not be successfully downloaded.`) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-05-30 20:22:35 -04:00
										 |  |  |       downloadError = true | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     if (!downloadError) { | 
					
						
							|  |  |  |       console.log(`Successfully downloaded ${name}.`) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function downloadWithRetry (url, directory) { | 
					
						
							|  |  |  |   let lastError | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |   const downloadURL = `${url}?circle-token=${process.env.CIRCLE_TOKEN}` | 
					
						
							| 
									
										
										
										
											2019-05-30 20:22:35 -04:00
										 |  |  |   for (let i = 0; i < 5; i++) { | 
					
						
							|  |  |  |     console.log(`Attempting to download ${url} - attempt #${(i + 1)}`) | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |       return await downloadFile(downloadURL, directory) | 
					
						
							| 
									
										
										
										
											2019-05-30 20:22:35 -04:00
										 |  |  |     } catch (err) { | 
					
						
							|  |  |  |       lastError = err | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |       await new Promise((resolve, reject) => setTimeout(resolve, 30000)) | 
					
						
							| 
									
										
										
										
											2019-05-30 20:22:35 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-05-30 20:22:35 -04:00
										 |  |  |   throw lastError | 
					
						
							| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function downloadFile (url, directory) { | 
					
						
							|  |  |  |   return new Promise((resolve, reject) => { | 
					
						
							|  |  |  |     const nuggetOpts = { | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |       dir: directory, | 
					
						
							|  |  |  |       quiet: args.verbose | 
					
						
							| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     nugget(url, nuggetOpts, (err) => { | 
					
						
							|  |  |  |       if (err) { | 
					
						
							|  |  |  |         reject(err) | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         resolve() | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (!args.name || !args.buildNum || !args.dest) { | 
					
						
							|  |  |  |   console.log(`Download CircleCI artifacts.
 | 
					
						
							| 
									
										
										
										
											2019-06-05 12:21:57 -04:00
										 |  |  |     Usage: download-circleci-artifacts.js [--buildNum=CIRCLE_BUILD_NUMBER] [--name=artifactName] [--dest] [--verbose]`)
 | 
					
						
							| 
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 |  |  |   process.exit(0) | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |   downloadArtifact(args.name, args.buildNum, args.dest) | 
					
						
							|  |  |  | } |