Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-57775][JENKINS-62780] Handle PRs from/to non existing src/dest #322

Merged
merged 7 commits into from
Jul 17, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ public List<BitbucketPullRequestValue> getPullRequests() throws InterruptedExcep
} while (page.getNext() != null);

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

for (BitbucketPullRequestValue pullRequest : pullRequests) {
setupClosureForPRBranch(pullRequest);
Expand Down Expand Up @@ -336,9 +339,13 @@ public BitbucketCommit call() throws Exception {

private void setupClosureForPRBranch(BitbucketPullRequestValue pullRequest) {
BitbucketCloudBranch branch = pullRequest.getSource().getBranch();
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
if (branch != null) {
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
}
branch = pullRequest.getDestination().getBranch();
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
if (branch != null) {
branch.setCommitClosure(new CommitClosure(branch.getRawNode()));
}
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@
*/
package com.cloudbees.jenkins.plugins.bitbucket.client.events;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequestEvent;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudBranch;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValue;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValueDestination;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValueRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudRepositoryOwner;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -64,58 +68,61 @@ public void setRepository(BitbucketCloudRepository repository) {

private void reconstructMissingData() {
if (this.repository != null && this.pullRequest != null) {
if (this.pullRequest.getSource() != null
&& this.pullRequest.getSource().getRepository() != null) {
if (this.pullRequest.getSource().getRepository().getScm() == null) {
this.pullRequest.getSource().getRepository().setScm(repository.getScm());
}
if (this.pullRequest.getSource().getRepository().getOwner() == null) {
if (this.pullRequest.getSource().getRepository().getOwnerName().equals(this.pullRequest.getAuthorLogin())) {
BitbucketCloudRepositoryOwner owner = new BitbucketCloudRepositoryOwner();
owner.setUsername(this.pullRequest.getAuthorLogin());
owner.setDisplayName(this.pullRequest.getAuthorDisplayName());
if (repository.isPrivate()) {
this.pullRequest.getSource().getRepository().setPrivate(repository.isPrivate());
BitbucketPullRequestValueRepository source = this.pullRequest.getSource();
if (source != null) {
BitbucketCloudRepository sourceRepository = source.getRepository();
if(sourceRepository != null) {
if (sourceRepository.getScm() == null) {
sourceRepository.setScm(repository.getScm());
}
if (sourceRepository.getOwner() == null) {
if (sourceRepository.getOwnerName().equals(this.pullRequest.getAuthorLogin())) {
BitbucketCloudRepositoryOwner owner = new BitbucketCloudRepositoryOwner();
owner.setUsername(this.pullRequest.getAuthorLogin());
owner.setDisplayName(this.pullRequest.getAuthorDisplayName());
if (this.repository.isPrivate()) {
sourceRepository.setPrivate(this.repository.isPrivate());
}
sourceRepository.setOwner(owner);
} else if (sourceRepository.getOwnerName().equals(this.repository.getOwnerName())) {
sourceRepository.setOwner(this.repository.getOwner());
sourceRepository.setPrivate(this.repository.isPrivate());
}
this.pullRequest.getSource().getRepository().setOwner(owner);
} else if (this.pullRequest.getSource().getRepository().getOwnerName().equals(repository.getOwnerName())) {
this.pullRequest.getSource().getRepository().setOwner(repository.getOwner());
this.pullRequest.getSource().getRepository().setPrivate(repository.isPrivate());
}
}
}
if (this.pullRequest.getSource() != null
&& this.pullRequest.getSource().getCommit() != null
&& this.pullRequest.getSource().getBranch() != null
&& this.pullRequest.getSource().getBranch().getRawNode() == null) {
this.pullRequest.getSource().getBranch()
.setRawNode(this.pullRequest.getSource().getCommit().getHash());
}
if (this.pullRequest.getSource() != null
&& this.pullRequest.getSource().getCommit() != null
&& this.pullRequest.getSource().getBranch() != null
&& this.pullRequest.getSource().getBranch().getDateMillis() == 0) {
this.pullRequest.getSource().getBranch()
.setDateMillis(toDate(this.pullRequest.getSource().getCommit().getDate()));
if (source != null) {
BitbucketCloudBranch sourceBranch = source.getBranch();
BitbucketCommit sourceCommit = source.getCommit();
if (sourceCommit != null
&& sourceBranch != null) {
if (sourceBranch.getRawNode() == null) {
sourceBranch.setRawNode(source.getCommit().getHash());
}
if (sourceBranch.getDateMillis() == 0) {
sourceBranch.setDateMillis(toDate(sourceCommit.getDate()));
}
}
}
if (this.pullRequest.getDestination() != null
&& this.pullRequest.getDestination().getRepository() != null) {
if (this.pullRequest.getDestination().getRepository().getScm() == null) {
this.pullRequest.getDestination().getRepository().setScm(repository.getScm());
BitbucketPullRequestValueDestination destination = this.pullRequest.getDestination();
if (destination != null
&& destination.getRepository() != null) {
if (destination.getRepository().getScm() == null) {
destination.getRepository().setScm(repository.getScm());
}
if (this.pullRequest.getDestination().getRepository().getOwner() == null
&& this.pullRequest.getDestination().getRepository().getOwnerName()
if (destination.getRepository().getOwner() == null
&& destination.getRepository().getOwnerName()
.equals(repository.getOwnerName())) {
this.pullRequest.getDestination().getRepository().setOwner(repository.getOwner());
this.pullRequest.getDestination().getRepository().setPrivate(repository.isPrivate());
destination.getRepository().setOwner(repository.getOwner());
destination.getRepository().setPrivate(repository.isPrivate());
}
}
if (this.pullRequest.getDestination() != null
&& this.pullRequest.getDestination().getCommit() != null
&& this.pullRequest.getDestination().getBranch() != null
&& this.pullRequest.getDestination().getBranch().getRawNode() == null) {
this.pullRequest.getDestination().getBranch()
.setRawNode(this.pullRequest.getDestination().getCommit().getHash());
if (destination != null
&& destination.getCommit() != null
&& destination.getBranch() != null
&& destination.getBranch().getRawNode() == null) {
destination.getBranch()
.setRawNode(destination.getCommit().getHash());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Date;

Expand All @@ -44,14 +45,14 @@ public class BitbucketPullRequestValueDestination implements BitbucketPullReques
@JsonCreator
public BitbucketPullRequestValueDestination(@NonNull @JsonProperty("repository") BitbucketCloudRepository repository,
@JsonProperty("branch") BitbucketCloudBranch branch,
@NonNull @JsonProperty("commit") BitbucketCloudCommit commit) {
@CheckForNull @JsonProperty("commit") BitbucketCloudCommit commit) {
this.repository = repository;
this.branch = branch;
this.commit = commit;

// It is possible for a PR's original destination to no longer exist.
if(this.branch != null) {
// redound available the informations into impl objects
if(this.branch != null && this.commit != null) {
// Make the commit information available into impl objects
this.branch.setRawNode(commit.getHash());
}
}
Expand All @@ -66,6 +67,7 @@ public void setRepository(BitbucketCloudRepository repository) {
}

@Override
@CheckForNull
public BitbucketCloudBranch getBranch() {
return branch;
}
Expand All @@ -75,6 +77,7 @@ public void setBranch(BitbucketCloudBranch branch) {
}

@Override
@CheckForNull
public BitbucketCommit getCommit() {
if (branch != null && commit != null) {
// initialise commit value using branch closure if not already valued
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import java.util.Date;

public class BitbucketPullRequestValueRepository implements BitbucketPullRequestSource {
Expand All @@ -40,18 +40,22 @@ public class BitbucketPullRequestValueRepository implements BitbucketPullRequest
private BitbucketCloudCommit commit;

@JsonCreator
public BitbucketPullRequestValueRepository(@NonNull @JsonProperty("repository") BitbucketCloudRepository repository,
@NonNull @JsonProperty("branch") BitbucketCloudBranch branch,
@NonNull @JsonProperty("commit") BitbucketCloudCommit commit) {
public BitbucketPullRequestValueRepository(@JsonProperty("repository") BitbucketCloudRepository repository,
@JsonProperty("branch") BitbucketCloudBranch branch,
@JsonProperty("commit") BitbucketCloudCommit commit) {
this.repository = repository;
this.branch = branch;
this.commit = commit;

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

@Override
@CheckForNull
public BitbucketCloudRepository getRepository() {
return repository;
}
Expand All @@ -61,6 +65,7 @@ public void setRepository(BitbucketCloudRepository repository) {
}

@Override
@CheckForNull
public BitbucketCloudBranch getBranch() {
return branch;
}
Expand All @@ -70,6 +75,7 @@ public void setBranch(BitbucketCloudBranch branch) {
}

@Override
@CheckForNull
public BitbucketCommit getCommit() {
if (branch != null && commit != null) {
// initialise commit value using branch closure if not already valued
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ private List<BitbucketServerPullRequest> getPullRequests(UriTemplate template)
List<BitbucketServerPullRequest> pullRequests = getResources(template, BitbucketServerPullRequests.class);

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

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