Skip to content

Commit 25932e4

Browse files
Allowing PRs without a destination branch to be parsed and ignored. (#283)
Co-authored-by: Joseph Petersen <[email protected]>
1 parent ad087b3 commit 25932e4

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketCloudApiClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ public List<BitbucketPullRequestValue> getPullRequests() throws InterruptedExcep
300300
pullRequests.addAll(page.getValues());
301301
} while (page.getNext() != null);
302302

303+
// PRs with missing destination branch are invalid and should be ignored.
304+
pullRequests.removeIf(pr -> pr.getDestination().getBranch() == null);
305+
303306
for (BitbucketPullRequestValue pullRequest : pullRequests) {
304307
setupClosureForPRBranch(pullRequest);
305308
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/pullrequest/BitbucketPullRequestValueDestination.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ public class BitbucketPullRequestValueDestination implements BitbucketPullReques
4343

4444
@JsonCreator
4545
public BitbucketPullRequestValueDestination(@NonNull @JsonProperty("repository") BitbucketCloudRepository repository,
46-
@NonNull @JsonProperty("branch") BitbucketCloudBranch branch,
46+
@JsonProperty("branch") BitbucketCloudBranch branch,
4747
@NonNull @JsonProperty("commit") BitbucketCloudCommit commit) {
4848
this.repository = repository;
4949
this.branch = branch;
5050
this.commit = commit;
5151

52-
// redound available the informations into impl objects
53-
this.branch.setRawNode(commit.getHash());
52+
// It is possible for a PR's original destination to no longer exist.
53+
if(this.branch != null) {
54+
// redound available the informations into impl objects
55+
this.branch.setRawNode(commit.getHash());
56+
}
5457
}
5558

5659
@Override

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ private List<BitbucketServerPullRequest> getPullRequests(UriTemplate template)
328328
throws IOException, InterruptedException {
329329
List<BitbucketServerPullRequest> pullRequests = getResources(template, BitbucketServerPullRequests.class);
330330

331+
// PRs with missing destination branch are invalid and should be ignored.
332+
pullRequests.removeIf(pr -> pr.getDestination().getBranch() == null);
333+
331334
// set commit closure to make commit informations available when need, in a similar way to when request branches
332335
for (BitbucketServerPullRequest pullRequest : pullRequests) {
333336
setupClosureForPRBranch(pullRequest);

src/test/resources/com/cloudbees/jenkins/plugins/bitbucket/client/payload/2.0-repositories-amuniz-test-repos-pullrequests_page_1_pagelen_50.json

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,144 @@
278278
},
279279
"merge_commit": null,
280280
"closed_by": null
281+
},
282+
{
283+
"description": "PR with destination branch deleted",
284+
"links": {
285+
"decline": {
286+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/decline"
287+
},
288+
"commits": {
289+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/commits"
290+
},
291+
"self": {
292+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3"
293+
},
294+
"comments": {
295+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/comments"
296+
},
297+
"merge": {
298+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/merge"
299+
},
300+
"html": {
301+
"href": "https://bitbucket.org/amuniz/test-repos/pull-requests/3"
302+
},
303+
"activity": {
304+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/activity"
305+
},
306+
"diff": {
307+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/diff"
308+
},
309+
"approve": {
310+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/approve"
311+
},
312+
"statuses": {
313+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/pullrequests/3/statuses"
314+
}
315+
},
316+
"title": "Bugfix 123",
317+
"close_source_branch": true,
318+
"type": "pullrequest",
319+
"id": 3,
320+
"destination": {
321+
"commit": {
322+
"hash": "bf4f4ce8a3a8",
323+
"type": "commit",
324+
"links": {
325+
"self": {
326+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/commit/bf4f4ce8a3a8"
327+
},
328+
"html": {
329+
"href": "https://bitbucket.org/amuniz/test-repos/commits/bf4f4ce8a3a8"
330+
}
331+
}
332+
},
333+
"repository": {
334+
"links": {
335+
"self": {
336+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos"
337+
},
338+
"html": {
339+
"href": "https://bitbucket.org/amuniz/test-repos"
340+
},
341+
"avatar": {
342+
"href": "https://bytebucket.org/ravatar/%7B3deb8c29-778a-450c-8f69-3e50a18079df%7D?ts=default"
343+
}
344+
},
345+
"type": "repository",
346+
"name": "test-repos",
347+
"full_name": "amuniz/test-repos",
348+
"uuid": "{3deb8c29-778a-450c-8f69-3e50a18079df}"
349+
},
350+
"branch": null
351+
},
352+
"created_on": "2018-09-21T14:57:59.455870+00:00",
353+
"summary": {
354+
"raw": "PR with destination branch deleted",
355+
"markup": "markdown",
356+
"html": "<p>PR with destination branch deleted</p>",
357+
"type": "rendered"
358+
},
359+
"source": {
360+
"commit": {
361+
"hash": "bf0e8b7962c4",
362+
"type": "commit",
363+
"links": {
364+
"self": {
365+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos/commit/bf0e8b7962c4"
366+
},
367+
"html": {
368+
"href": "https://bitbucket.org/amuniz/test-repos/commits/bf0e8b7962c4"
369+
}
370+
}
371+
},
372+
"repository": {
373+
"links": {
374+
"self": {
375+
"href": "https://api.bitbucket.org/2.0/repositories/amuniz/test-repos"
376+
},
377+
"html": {
378+
"href": "https://bitbucket.org/amuniz/test-repos"
379+
},
380+
"avatar": {
381+
"href": "https://bytebucket.org/ravatar/%7B3deb8c29-778a-450c-8f69-3e50a18079df%7D?ts=default"
382+
}
383+
},
384+
"type": "repository",
385+
"name": "test-repos",
386+
"full_name": "amuniz/test-repos",
387+
"uuid": "{3deb8c29-778a-450c-8f69-3e50a18079df}"
388+
},
389+
"branch": {
390+
"name": "bugfix/123"
391+
}
392+
},
393+
"comment_count": 0,
394+
"state": "OPEN",
395+
"task_count": 0,
396+
"reason": "",
397+
"updated_on": "2018-09-21T14:57:59.488719+00:00",
398+
"author": {
399+
"username": "amuniz",
400+
"display_name": "Nikolas Falco",
401+
"account_id": "557058:ca1cd232-2017-4216-94be-99637899e18d",
402+
"links": {
403+
"self": {
404+
"href": "https://api.bitbucket.org/2.0/users/amuniz"
405+
},
406+
"html": {
407+
"href": "https://bitbucket.org/amuniz/"
408+
},
409+
"avatar": {
410+
"href": "https://bitbucket.org/account/amuniz/avatar/"
411+
}
412+
},
413+
"nickname": "amuniz",
414+
"type": "user",
415+
"uuid": "{644c7fc2-b15a-4445-9f89-35390694fac9}"
416+
},
417+
"merge_commit": null,
418+
"closed_by": null
281419
}],
282420
"page": 1,
283421
"size": 2

0 commit comments

Comments
 (0)