@@ -11,6 +11,10 @@ const logger = require('./logger').winstonLogger,
11
11
Constants = require ( "./constants" ) ,
12
12
config = require ( "./config" ) ;
13
13
14
+ const request = require ( 'request' ) ;
15
+ const { inspect } = require ( 'util' ) ;
16
+ const { reject } = require ( 'async' ) ;
17
+
14
18
15
19
let BUILD_ARTIFACTS_TOTAL_COUNT = 0 ;
16
20
let BUILD_ARTIFACTS_FAIL_COUNT = 0 ;
@@ -95,17 +99,14 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
95
99
let tmpFilePath = path . join ( filePath , fileName ) ;
96
100
const writer = fs . createWriteStream ( tmpFilePath ) ;
97
101
98
- return axios ( {
99
- method : 'get' ,
100
- url : url ,
101
- responseType : 'stream' ,
102
- } ) . then ( response => {
103
-
102
+ console . log ( `roshan1: url ${ url } ` )
103
+ console . log ( `roshan inside downloadAndUnzip` )
104
+ return request . get ( url ) . on ( 'response' , function ( response ) {
104
105
//ensure that the user can call `then()` only when the file has
105
106
//been downloaded entirely.
106
-
107
+ console . log ( `roshan1: response ${ inspect ( response ) } ` )
107
108
return new Promise ( async ( resolve , reject ) => {
108
- response . data . pipe ( writer ) ;
109
+ response . pipe ( writer ) ;
109
110
let error = null ;
110
111
writer . on ( 'error' , err => {
111
112
error = err ;
@@ -135,7 +136,7 @@ const unzipFile = async (filePath, fileName) => {
135
136
}
136
137
137
138
const sendUpdatesToBstack = async ( bsConfig , buildId , args , options , rawArgs ) => {
138
- let url = `${ config . buildUrl } ${ buildId } /build_artifacts/status` ;
139
+ options . url = `${ config . buildUrl } ${ buildId } /build_artifacts/status` ;
139
140
140
141
let cypressJSON = utils . getCypressJSON ( bsConfig ) ;
141
142
@@ -156,19 +157,38 @@ const sendUpdatesToBstack = async (bsConfig, buildId, args, options, rawArgs) =>
156
157
}
157
158
}
158
159
160
+ options . formData = data ;
161
+
159
162
try {
160
- await axios . post ( url , data , options ) ;
163
+ let responseData = null ;
164
+ request . post ( options , function ( err , resp , data ) {
165
+ if ( err ) {
166
+ utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
167
+ } else {
168
+ try {
169
+ responseData = JSON . parse ( data ) ;
170
+ } catch ( e ) {
171
+ responseData = { } ;
172
+ }
173
+ if ( resp . statusCode != 200 ) {
174
+ if ( responseData && responseData [ "error" ] ) {
175
+ utils . sendUsageReport ( bsConfig , args , responseData [ "error" ] , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
176
+ }
177
+ }
178
+ }
179
+ } ) ;
161
180
} catch ( err ) {
162
181
utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
163
182
}
164
183
}
165
184
166
185
exports . downloadBuildArtifacts = async ( bsConfig , buildId , args , rawArgs ) => {
186
+ console . log ( 'hello brother' )
167
187
BUILD_ARTIFACTS_FAIL_COUNT = 0 ;
168
188
BUILD_ARTIFACTS_TOTAL_COUNT = 0 ;
169
189
170
- let url = `${ config . buildUrl } ${ buildId } /build_artifacts` ;
171
190
let options = {
191
+ url : `${ config . buildUrl } ${ buildId } /build_artifacts` ,
172
192
auth : {
173
193
username : bsConfig . auth . username ,
174
194
password : bsConfig . auth . access_key ,
@@ -183,39 +203,56 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
183
203
let errorCode = null ;
184
204
185
205
try {
186
- const res = await axios . get ( url , options ) ;
187
- let buildDetails = res . data ;
188
-
189
- await createDirectories ( buildId , buildDetails ) ;
190
- await parseAndDownloadArtifacts ( buildId , buildDetails ) ;
191
-
192
- if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
193
- messageType = Constants . messageTypes . ERROR ;
194
- message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
195
- logger . error ( message ) ;
206
+ let buildDetails = null ;
207
+ request . get ( options , async function ( err , resp , body ) {
208
+ if ( err ) {
209
+ utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
196
210
process . exitCode = Constants . ERROR_EXIT_CODE ;
197
211
} else {
198
- messageType = Constants . messageTypes . SUCCESS ;
199
- message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_SUCCESS . replace ( '<build-id>' , buildId ) . replace ( '<user-path>' , process . cwd ( ) ) ;
200
- logger . info ( message ) ;
212
+ try {
213
+ buildDetails = JSON . parse ( body ) ;
214
+ if ( resp . statusCode != 200 ) {
215
+ logger . error ( 'Downloading the build artifacts failed.' ) ;
216
+ logger . error ( `Error: Request failed with status code ${ resp . statusCode } ` )
217
+ utils . sendUsageReport ( bsConfig , args , buildDetails , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
218
+ process . exitCode = Constants . ERROR_EXIT_CODE ;
219
+ } else {
220
+ await createDirectories ( buildId , buildDetails ) ;
221
+ await parseAndDownloadArtifacts ( buildId , buildDetails ) ;
222
+ console . log ( `roshan1 making request passed1 ${ inspect ( buildDetails ) } ` ) ;
223
+ if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
224
+ messageType = Constants . messageTypes . ERROR ;
225
+ message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
226
+ logger . error ( message ) ;
227
+ process . exitCode = Constants . ERROR_EXIT_CODE ;
228
+ } else {
229
+ messageType = Constants . messageTypes . SUCCESS ;
230
+ message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_SUCCESS . replace ( '<build-id>' , buildId ) . replace ( '<user-path>' , process . cwd ( ) ) ;
231
+ logger . info ( message ) ;
232
+ }
233
+ await sendUpdatesToBstack ( bsConfig , buildId , args , options , rawArgs ) ;
234
+ utils . sendUsageReport ( bsConfig , args , message , messageType , null , null , rawArgs ) ;
235
+ }
236
+ } catch ( err ) {
237
+ messageType = Constants . messageTypes . ERROR ;
238
+ errorCode = 'api_failed_build_artifacts' ;
239
+ if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
240
+ messageType = Constants . messageTypes . ERROR ;
241
+ message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
242
+ logger . error ( message ) ;
243
+ } else {
244
+ logger . error ( 'Downloading the build artifacts failed.' ) ;
245
+ }
246
+ utils . sendUsageReport ( bsConfig , args , err , messageType , errorCode , null , rawArgs ) ;
247
+ logger . error ( err . message ) ;
248
+ logger . error ( `Error: Request failed with status code ${ resp . statusCode } ` )
249
+ process . exitCode = Constants . ERROR_EXIT_CODE ;
250
+ }
201
251
}
202
-
203
- await sendUpdatesToBstack ( bsConfig , buildId , args , options , rawArgs ) ;
204
- utils . sendUsageReport ( bsConfig , args , message , messageType , null , null , rawArgs ) ;
252
+ } ) ;
205
253
} catch ( err ) {
206
- messageType = Constants . messageTypes . ERROR ;
207
- errorCode = 'api_failed_build_artifacts' ;
208
-
209
- if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
210
- messageType = Constants . messageTypes . ERROR ;
211
- message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
212
- logger . error ( message ) ;
213
- } else {
214
- logger . error ( 'Downloading the build artifacts failed.' ) ;
215
- }
216
-
217
- utils . sendUsageReport ( bsConfig , args , err , messageType , errorCode , null , rawArgs ) ;
218
- logger . error ( err . message ) ;
254
+ console . log ( `roshan1: error here ${ err } ` )
255
+ utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
219
256
process . exitCode = Constants . ERROR_EXIT_CODE ;
220
257
}
221
258
} ;
0 commit comments