Skip to content

Commit 57d869c

Browse files
Dohbedohbitwiseman
andauthored
[JENKINS-57775][JENKINS-62780] Handle PRs from/to non existing src/dest (#322)
* [JENKINS-57775][JENKINS-62780] Handle PRs from/to non existing source/dest * [JENKINS-57775][JENKINS-62780] Restore the condition on null branch * [JENKINS-57775][JENKINS-62780] Do not rely on getCommit for Bitbucket Server * [JENKINS-57775][JENKINS-62780] Address CheckForNull * [JENKINS-57775][JENKINS-62780] Add CheckForNull for destination commit Co-authored-by: Liam Newman <[email protected]> * [JENKINS-57775][JENKINS-62780] Rephrase comment Co-authored-by: Liam Newman <[email protected]>
1 parent e35b6c9 commit 57d869c

File tree

6 files changed

+324
-58
lines changed

6 files changed

+324
-58
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ public List<BitbucketPullRequestValue> getPullRequests() throws InterruptedExcep
307307
} while (page.getNext() != null);
308308

309309
// PRs with missing destination branch are invalid and should be ignored.
310-
pullRequests.removeIf(pr -> pr.getDestination().getBranch() == null);
310+
pullRequests.removeIf(pr -> pr.getSource().getRepository() == null
311+
|| pr.getSource().getCommit() == null
312+
|| pr.getDestination().getBranch() == null
313+
|| pr.getDestination().getCommit() == null);
311314

312315
for (BitbucketPullRequestValue pullRequest : pullRequests) {
313316
setupClosureForPRBranch(pullRequest);
@@ -336,9 +339,13 @@ public BitbucketCommit call() throws Exception {
336339

337340
private void setupClosureForPRBranch(BitbucketPullRequestValue pullRequest) {
338341
BitbucketCloudBranch branch = pullRequest.getSource().getBranch();
339-
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
342+
if (branch != null) {
343+
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
344+
}
340345
branch = pullRequest.getDestination().getBranch();
341-
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
346+
if (branch != null) {
347+
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
348+
}
342349
}
343350

344351
@Deprecated

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/client/events/BitbucketCloudPullRequestEvent.java

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket.client.events;
2525

26+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
2627
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
2728
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequestEvent;
2829
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
30+
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudBranch;
2931
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValue;
32+
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValueDestination;
33+
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValueRepository;
3034
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudRepository;
3135
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudRepositoryOwner;
3236
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -64,58 +68,61 @@ public void setRepository(BitbucketCloudRepository repository) {
6468

6569
private void reconstructMissingData() {
6670
if (this.repository != null && this.pullRequest != null) {
67-
if (this.pullRequest.getSource() != null
68-
&& this.pullRequest.getSource().getRepository() != null) {
69-
if (this.pullRequest.getSource().getRepository().getScm() == null) {
70-
this.pullRequest.getSource().getRepository().setScm(repository.getScm());
71-
}
72-
if (this.pullRequest.getSource().getRepository().getOwner() == null) {
73-
if (!this.pullRequest.getSource().getRepository().getOwnerName().equals(repository.getOwnerName())) { // i.e., a fork
74-
BitbucketCloudRepositoryOwner owner = new BitbucketCloudRepositoryOwner();
75-
owner.setUsername(this.pullRequest.getSource().getRepository().getOwnerName());
76-
owner.setDisplayName(this.pullRequest.getAuthorLogin());
77-
if (repository.isPrivate()) {
78-
this.pullRequest.getSource().getRepository().setPrivate(repository.isPrivate());
71+
BitbucketPullRequestValueRepository source = this.pullRequest.getSource();
72+
if (source != null) {
73+
BitbucketCloudRepository sourceRepository = source.getRepository();
74+
if (sourceRepository != null) {
75+
if (sourceRepository.getScm() == null) {
76+
sourceRepository.setScm(repository.getScm());
77+
}
78+
if (sourceRepository.getOwner() == null) {
79+
if (!sourceRepository.getOwnerName().equals(repository.getOwnerName())) { // i.e., a fork
80+
BitbucketCloudRepositoryOwner owner = new BitbucketCloudRepositoryOwner();
81+
owner.setUsername(sourceRepository.getOwnerName());
82+
owner.setDisplayName(this.pullRequest.getAuthorLogin());
83+
if (repository.isPrivate()) {
84+
sourceRepository.setPrivate(repository.isPrivate());
85+
}
86+
sourceRepository.setOwner(owner);
87+
} else { // origin branch
88+
sourceRepository.setOwner(repository.getOwner());
89+
sourceRepository.setPrivate(repository.isPrivate());
7990
}
80-
this.pullRequest.getSource().getRepository().setOwner(owner);
81-
} else { // origin branch
82-
this.pullRequest.getSource().getRepository().setOwner(repository.getOwner());
83-
this.pullRequest.getSource().getRepository().setPrivate(repository.isPrivate());
8491
}
8592
}
8693
}
87-
if (this.pullRequest.getSource() != null
88-
&& this.pullRequest.getSource().getCommit() != null
89-
&& this.pullRequest.getSource().getBranch() != null
90-
&& this.pullRequest.getSource().getBranch().getRawNode() == null) {
91-
this.pullRequest.getSource().getBranch()
92-
.setRawNode(this.pullRequest.getSource().getCommit().getHash());
93-
}
94-
if (this.pullRequest.getSource() != null
95-
&& this.pullRequest.getSource().getCommit() != null
96-
&& this.pullRequest.getSource().getBranch() != null
97-
&& this.pullRequest.getSource().getBranch().getDateMillis() == 0) {
98-
this.pullRequest.getSource().getBranch()
99-
.setDateMillis(toDate(this.pullRequest.getSource().getCommit().getDate()));
94+
if (source != null) {
95+
BitbucketCloudBranch sourceBranch = source.getBranch();
96+
BitbucketCommit sourceCommit = source.getCommit();
97+
if (sourceCommit != null
98+
&& sourceBranch != null) {
99+
if (sourceBranch.getRawNode() == null) {
100+
sourceBranch.setRawNode(source.getCommit().getHash());
101+
}
102+
if (sourceBranch.getDateMillis() == 0) {
103+
sourceBranch.setDateMillis(toDate(sourceCommit.getDate()));
104+
}
105+
}
100106
}
101-
if (this.pullRequest.getDestination() != null
102-
&& this.pullRequest.getDestination().getRepository() != null) {
103-
if (this.pullRequest.getDestination().getRepository().getScm() == null) {
104-
this.pullRequest.getDestination().getRepository().setScm(repository.getScm());
107+
BitbucketPullRequestValueDestination destination = this.pullRequest.getDestination();
108+
if (destination != null
109+
&& destination.getRepository() != null) {
110+
if (destination.getRepository().getScm() == null) {
111+
destination.getRepository().setScm(repository.getScm());
105112
}
106-
if (this.pullRequest.getDestination().getRepository().getOwner() == null
107-
&& this.pullRequest.getDestination().getRepository().getOwnerName()
108-
.equals(repository.getOwnerName())) {
109-
this.pullRequest.getDestination().getRepository().setOwner(repository.getOwner());
110-
this.pullRequest.getDestination().getRepository().setPrivate(repository.isPrivate());
113+
if (destination.getRepository().getOwner() == null
114+
&& destination.getRepository().getOwnerName()
115+
.equals(repository.getOwnerName())) {
116+
destination.getRepository().setOwner(repository.getOwner());
117+
destination.getRepository().setPrivate(repository.isPrivate());
111118
}
112119
}
113-
if (this.pullRequest.getDestination() != null
114-
&& this.pullRequest.getDestination().getCommit() != null
115-
&& this.pullRequest.getDestination().getBranch() != null
116-
&& this.pullRequest.getDestination().getBranch().getRawNode() == null) {
117-
this.pullRequest.getDestination().getBranch()
118-
.setRawNode(this.pullRequest.getDestination().getCommit().getHash());
120+
if (destination != null
121+
&& destination.getCommit() != null
122+
&& destination.getBranch() != null
123+
&& destination.getBranch().getRawNode() == null) {
124+
destination.getBranch()
125+
.setRawNode(destination.getCommit().getHash());
119126
}
120127
}
121128
}

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
@@ -33,6 +33,7 @@
3333
import com.fasterxml.jackson.annotation.JsonCreator;
3434
import com.fasterxml.jackson.annotation.JsonProperty;
3535
import com.fasterxml.jackson.databind.util.StdDateFormat;
36+
import edu.umd.cs.findbugs.annotations.CheckForNull;
3637
import edu.umd.cs.findbugs.annotations.NonNull;
3738
import java.util.Date;
3839

@@ -44,14 +45,14 @@ public class BitbucketPullRequestValueDestination implements BitbucketPullReques
4445
@JsonCreator
4546
public BitbucketPullRequestValueDestination(@NonNull @JsonProperty("repository") BitbucketCloudRepository repository,
4647
@JsonProperty("branch") BitbucketCloudBranch branch,
47-
@NonNull @JsonProperty("commit") BitbucketCloudCommit commit) {
48+
@CheckForNull @JsonProperty("commit") BitbucketCloudCommit commit) {
4849
this.repository = repository;
4950
this.branch = branch;
5051
this.commit = commit;
5152

5253
// 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
54+
if(this.branch != null && this.commit != null) {
55+
// Make the commit information available into impl objects
5556
this.branch.setRawNode(commit.getHash());
5657
}
5758
}
@@ -66,6 +67,7 @@ public void setRepository(BitbucketCloudRepository repository) {
6667
}
6768

6869
@Override
70+
@CheckForNull
6971
public BitbucketCloudBranch getBranch() {
7072
return branch;
7173
}
@@ -75,6 +77,7 @@ public void setBranch(BitbucketCloudBranch branch) {
7577
}
7678

7779
@Override
80+
@CheckForNull
7881
public BitbucketCommit getCommit() {
7982
if (branch != null && commit != null) {
8083
// initialise commit value using branch closure if not already valued

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.fasterxml.jackson.annotation.JsonCreator;
3232
import com.fasterxml.jackson.annotation.JsonProperty;
3333
import com.fasterxml.jackson.databind.util.StdDateFormat;
34-
import edu.umd.cs.findbugs.annotations.NonNull;
34+
import edu.umd.cs.findbugs.annotations.CheckForNull;
3535
import java.util.Date;
3636

3737
public class BitbucketPullRequestValueRepository implements BitbucketPullRequestSource {
@@ -40,18 +40,22 @@ public class BitbucketPullRequestValueRepository implements BitbucketPullRequest
4040
private BitbucketCloudCommit commit;
4141

4242
@JsonCreator
43-
public BitbucketPullRequestValueRepository(@NonNull @JsonProperty("repository") BitbucketCloudRepository repository,
44-
@NonNull @JsonProperty("branch") BitbucketCloudBranch branch,
45-
@NonNull @JsonProperty("commit") BitbucketCloudCommit commit) {
43+
public BitbucketPullRequestValueRepository(@JsonProperty("repository") BitbucketCloudRepository repository,
44+
@JsonProperty("branch") BitbucketCloudBranch branch,
45+
@JsonProperty("commit") BitbucketCloudCommit commit) {
4646
this.repository = repository;
4747
this.branch = branch;
4848
this.commit = commit;
4949

50-
// redound available the informations into impl objects
51-
this.branch.setRawNode(commit.getHash());
50+
// It is possible for a PR's original source to no longer exist.
51+
if(branch != null && commit != null) {
52+
// Make the commit information available into impl objects
53+
this.branch.setRawNode(commit.getHash());
54+
}
5255
}
5356

5457
@Override
58+
@CheckForNull
5559
public BitbucketCloudRepository getRepository() {
5660
return repository;
5761
}
@@ -61,6 +65,7 @@ public void setRepository(BitbucketCloudRepository repository) {
6165
}
6266

6367
@Override
68+
@CheckForNull
6469
public BitbucketCloudBranch getBranch() {
6570
return branch;
6671
}
@@ -70,6 +75,7 @@ public void setBranch(BitbucketCloudBranch branch) {
7075
}
7176

7277
@Override
78+
@CheckForNull
7379
public BitbucketCommit getCommit() {
7480
if (branch != null && commit != null) {
7581
// initialise commit value using branch closure if not already valued

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ private List<BitbucketServerPullRequest> getPullRequests(UriTemplate template)
330330
List<BitbucketServerPullRequest> pullRequests = getResources(template, BitbucketServerPullRequests.class);
331331

332332
// PRs with missing destination branch are invalid and should be ignored.
333-
pullRequests.removeIf(pr -> pr.getDestination().getBranch() == null);
333+
pullRequests.removeIf(pr -> pr.getSource().getRepository() == null
334+
|| pr.getSource().getBranch() == null
335+
|| pr.getDestination().getBranch() == null);
334336

335337
// set commit closure to make commit informations available when need, in a similar way to when request branches
336338
for (BitbucketServerPullRequest pullRequest : pullRequests) {

0 commit comments

Comments
 (0)