@@ -239,40 +239,65 @@ const checkPRMethod = (prCheckData, sha) => {
239
239
// bool to check whether we won't find expected attributes we need
240
240
let parseError = false ;
241
241
242
- if ( prCheckData . hasOwnProperty ( 'total_count' ) ) {
243
- if ( prCheckData . total_count >= 1 ) {
244
- let elementPositionToUse = prCheckData . total_count - 1 ;
245
- let infoItem = prCheckData . items [ elementPositionToUse ] ;
242
+ const parsePullRequestItems = ( prCheckData ) => {
243
+ let infoItem ;
244
+ if ( ! prCheckData . hasOwnProperty ( 'items' ) ) {
245
+ exitAndWriteResultToFile ( true , 'Could not parse expected attribute: "items". Format might have been changed.' ) ;
246
+ }
246
247
247
- if ( infoItem . hasOwnProperty ( 'state' ) ) {
248
- if ( infoItem . state !== 'open' ) {
249
- // we can abort - we found a PR, but this one is already closed
250
- exitAndWriteResultToFile ( true , "We've only found a closed PR. Exiting." , "FAIL_NO_PR" ) ;
251
- }
248
+ let foundPullRequestsArray = prCheckData . items . slice ( ) . reverse ( ) ;
249
+
250
+ // iterate in reverse order, as the last items are the most relevant ones
251
+ for ( let pullRequestItem of foundPullRequestsArray ) {
252
+ // check if found item is valid (means it must be part of our given repository)
253
+ if ( pullRequestItem . url && pullRequestItem . url . indexOf ( repository ) != - 1 ) {
254
+ // means we've found a valid repository into our given repository
255
+ infoItem = pullRequestItem ;
256
+ break ;
252
257
}
258
+ } ;
253
259
254
- if ( infoItem . hasOwnProperty ( 'body' ) ) {
255
- // just additional information, let's not fail here
256
- pullRequestExtraInformation . info = infoItem . body ;
260
+ if ( ! infoItem ) {
261
+ // We've not found any valid pull request, therefore we cannot continue.
262
+ // TODO Future: This is no actual error, we just do not need to continue with that script as it would be useless.
263
+ // Check how to handle this case in the future.
264
+ exitAndWriteResultToFile ( true , "Found no related pull request. Exiting. Nothing to do." , 'FAIL_NO_PR' ) ;
265
+ }
266
+
267
+ if ( infoItem . hasOwnProperty ( 'state' ) ) {
268
+ if ( infoItem . state !== 'open' ) {
269
+ // we can abort - we found a PR, but this one is already closed
270
+ exitAndWriteResultToFile ( true , "We've only found a closed PR. Exiting." , "FAIL_NO_PR" ) ;
257
271
}
272
+ }
258
273
259
- if ( infoItem . hasOwnProperty ( 'pull_request' ) ) {
260
- let pr = infoItem . pull_request ;
261
- if ( pr . hasOwnProperty ( 'url' ) ) {
262
- pullRequestExtraInformation . url = infoItem . pull_request . url ;
263
- } else {
264
- parseError = true ;
265
- }
274
+ if ( infoItem . hasOwnProperty ( 'body' ) ) {
275
+ // just additional information, let's not fail here
276
+ pullRequestExtraInformation . info = infoItem . body ;
277
+ }
278
+
279
+ if ( infoItem . hasOwnProperty ( 'pull_request' ) ) {
280
+ let pr = infoItem . pull_request ;
281
+ if ( pr . hasOwnProperty ( 'url' ) ) {
282
+ pullRequestExtraInformation . url = infoItem . pull_request . url ;
266
283
} else {
267
284
parseError = true ;
268
285
}
286
+ } else {
287
+ parseError = true ;
288
+ }
269
289
270
- if ( parseError ) {
271
- exitAndWriteResultToFile ( true , "Could not parse expected attributes. Format might have been changed." ) ;
272
- }
290
+ if ( parseError ) {
291
+ exitAndWriteResultToFile ( true , "Could not parse expected attributes. Format might have been changed." ) ;
292
+ }
273
293
274
- // if all good and PR found
275
- continueWithAction ( sha ) ;
294
+ // if all good and PR found
295
+ continueWithAction ( sha ) ;
296
+ } ;
297
+
298
+ if ( prCheckData . hasOwnProperty ( 'total_count' ) ) {
299
+ if ( prCheckData . total_count >= 1 ) {
300
+ parsePullRequestItems ( prCheckData ) ;
276
301
} else {
277
302
exitAndWriteResultToFile ( true , "Could not read pull request details from GitHub Search API" ) ;
278
303
}
@@ -284,7 +309,8 @@ const checkPRMethod = (prCheckData, sha) => {
284
309
}
285
310
286
311
const checkPRExists = ( sha ) => {
287
- const checkPRExistencePath = `/search/issues?q=${ sha } ` ;
312
+ const queryString = encodeURIComponent ( `${ sha } repo:${ repository } ` ) ;
313
+ const checkPRExistencePath = `/search/issues?q=${ queryString } ` ;
288
314
getRequest ( checkPRExistencePath , checkPRMethod , sha ) ;
289
315
}
290
316
0 commit comments