Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import jenkins.model.Jenkins;
import jenkins.plugins.git.GitTagSCMHead;
import jenkins.plugins.git.traits.GitBrowserSCMSourceTrait;
import jenkins.scm.api.SCMEvent.Type;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.SCMHeadEvent;
Expand Down Expand Up @@ -342,9 +343,10 @@
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer,
@CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener)
throws IOException, InterruptedException {
try (BitbucketSCMSourceRequest request = new BitbucketSCMSourceContext(criteria, observer)
.withTraits(traits)
.newRequest(this, listener)) {
try (BitbucketApi client = buildBitbucketClient();
BitbucketSCMSourceRequest request = new BitbucketSCMSourceContext(criteria, observer)
.withTraits(traits)
.newRequest(this, listener)) {
StandardCredentials scanCredentials = credentials();
if (scanCredentials == null) {
listener.getLogger().format("Connecting to %s with no credentials, anonymous access%n", getServerUrl());
Expand All @@ -357,65 +359,70 @@

// now serve the request
if (request.isFetchPRs() && !request.isComplete()) {
if (event instanceof HasPullRequests prEvent) {
request.setPullRequests(getBitbucketPullRequestsFromEvent(prEvent, listener));
if (event instanceof HasPullRequests) {
// extract PRs from event
request.setPullRequests(getBitbucketPullRequestsFromEvent(client, event, listener));
}
// Search pull requests
retrievePullRequests(request);
}
if (request.isFetchBranches() && !request.isComplete()) {
if (event instanceof HasBranches branchEvent) {
request.setBranches(getBitbucketBranchesFromEvent(branchEvent, listener));
if (event instanceof HasBranches) {

Check warning on line 370 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 370 is only partially covered, one branch is missing
request.setBranches(getBitbucketBranchesFromEvent(client, event, listener));

Check warning on line 371 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 371 is not covered by tests
}
// Search branches
retrieveBranches(request);
}
if (request.isFetchTags() && !request.isComplete()) {
if (event instanceof HasTags tagEvent) {
request.setTags(getBitbucketTagsFromEvent(tagEvent, listener));
if (event instanceof HasTags) {
// extract tags from event
request.setTags(getBitbucketTagsFromEvent(client, event, listener));
}
// Search tags
retrieveTags(request);
}
}
}

private Iterable<BitbucketBranch> getBitbucketTagsFromEvent(@NonNull HasTags incomingTagEvent, @NonNull TaskListener listener) throws IOException, InterruptedException {
private Iterable<BitbucketBranch> getBitbucketTagsFromEvent(@NonNull BitbucketApi client,
@NonNull SCMHeadEvent<?> event,
@NonNull TaskListener listener) throws IOException {
Collection<BitbucketBranch> initializedTags = new HashSet<>();
try (BitbucketApi bitBucket = buildBitbucketClient()) {
if (event instanceof HasTags incomingTagEvent) {

Check warning on line 391 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 391 is only partially covered, one branch is missing
Iterable<BitbucketBranch> tags = incomingTagEvent.getTags(BitbucketSCMSource.this);
for (BitbucketBranch tag : tags) {
initializedTags.add(bitBucket.getTag(tag.getName()));
initializedTags.add(event.getType() == Type.REMOVED ? tag : client.getTag(tag.getName()));

Check warning on line 394 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 394 is only partially covered, one branch is missing
listener.getLogger().format("Initialized Tag: %s%n", tag.getName());
}
}
return initializedTags;
}

private Iterable<BitbucketPullRequest> getBitbucketPullRequestsFromEvent(@NonNull HasPullRequests incomingPrEvent,
@NonNull TaskListener listener) throws IOException, InterruptedException {
private Iterable<BitbucketPullRequest> getBitbucketPullRequestsFromEvent(@NonNull BitbucketApi client,
@NonNull SCMHeadEvent<?> event,
@NonNull TaskListener listener) throws IOException {
Collection<BitbucketPullRequest> initializedPRs = new HashSet<>();
try (BitbucketApi bitBucket = buildBitbucketClient()) {
Iterable<BitbucketPullRequest> pullRequests = incomingPrEvent.getPullRequests(BitbucketSCMSource.this);
if (event instanceof HasPullRequests prEvent) {

Check warning on line 405 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 405 is only partially covered, one branch is missing
Iterable<BitbucketPullRequest> pullRequests = prEvent.getPullRequests(BitbucketSCMSource.this);
for (BitbucketPullRequest pr : pullRequests) {
// ensure that the PR is properly initialised via /changes API
// see BitbucketServerAPIClient.setupPullRequest()
initializedPRs.add(bitBucket.getPullRequestById(Integer.parseInt(pr.getId())));
// PRs can not be deleted only closed/declined
initializedPRs.add(client.getPullRequestById(Integer.parseInt(pr.getId())));
listener.getLogger().format("Initialized PR: %s%n", pr.getLink());
}
}
return initializedPRs;
}

private Iterable<BitbucketBranch> getBitbucketBranchesFromEvent(@NonNull HasBranches incomingEvent,
@NonNull TaskListener listener) throws IOException, InterruptedException {
private Iterable<BitbucketBranch> getBitbucketBranchesFromEvent(@NonNull BitbucketApi client,
@NonNull SCMHeadEvent<?> event,
@NonNull TaskListener listener) throws IOException {
Collection<BitbucketBranch> initializedBranches = new HashSet<>();
try (BitbucketApi bitBucket = buildBitbucketClient()) {
Iterable<BitbucketBranch> branches = incomingEvent.getBranches(BitbucketSCMSource.this);
if (event instanceof HasBranches branchEvent) {
Iterable<BitbucketBranch> branches = branchEvent.getBranches(BitbucketSCMSource.this);
for (BitbucketBranch branch : branches) {
// ensure that the PR is properly initialised via /changes API
// see BitbucketServerAPIClient.setupPullRequest()
initializedBranches.add(bitBucket.getBranch(branch.getName()));
initializedBranches.add(event.getType() == Type.REMOVED ? branch : client.getBranch(branch.getName()));

Check warning on line 425 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 422-425 are not covered by tests
listener.getLogger().format("Initialized branch: %s%n", branch.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@

public interface HasBranches {

Iterable<BitbucketBranch> getBranches(BitbucketSCMSource src) throws InterruptedException;
Iterable<BitbucketBranch> getBranches(BitbucketSCMSource src);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@

public interface HasPullRequests {

Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) throws InterruptedException;
Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -50,8 +49,7 @@ public class BitbucketCloudBranch implements BitbucketBranch {

@JsonCreator
public BitbucketCloudBranch(@NonNull @JsonProperty("name") String name,
@Nullable @JsonProperty("target") BitbucketCloudBranch.Target target,
@Nullable @JsonProperty("heads") List<Head> heads) { // TODO delete heads arg if possible
@Nullable @JsonProperty("target") BitbucketCloudBranch.Target target) {
this.name = name;
if (target != null) {
this.dateInMillis = target.date.getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Map<SCMHead, SCMRevision> heads(@NonNull SCMSource source) {
}

@Override
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) throws InterruptedException {
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) {
if (hookEvent == PULL_REQUEST_DECLINED || hookEvent == PULL_REQUEST_MERGED) {
return Collections.emptySet();
}
Expand All @@ -166,7 +166,7 @@ public Iterable<BitbucketBranch> getTags(BitbucketSCMSource src) {
}

@Override
public Iterable<BitbucketBranch> getBranches(BitbucketSCMSource src) throws InterruptedException {
public Iterable<BitbucketBranch> getBranches(BitbucketSCMSource src) {
List<BitbucketBranch> branches = new ArrayList<>();

BitbucketPullRequest pr = getPayload().getPullRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.HasBranches;
import com.cloudbees.jenkins.plugins.bitbucket.api.HasPullRequests;
import com.cloudbees.jenkins.plugins.bitbucket.api.HasTags;
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudAuthor;
import com.cloudbees.jenkins.plugins.bitbucket.client.branch.BitbucketCloudBranch;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.ArrayList;
Expand Down Expand Up @@ -81,28 +80,23 @@

Map<SCMHead, SCMRevision> result = new HashMap<>();
for (BitbucketPushEvent.Change change: getPayload().getChanges()) {
if (change.isClosed()) {
result.put(new BranchSCMHead(change.getOld().getName()), null);
} else {
// created is true
Reference newChange = change.getNew();
Target target = newChange.getTarget();
Reference changeRef = change.isClosed() ? change.getOld(): change.getNew();
Target target = changeRef.getTarget();

SCMHead head = null;
String eventType = newChange.getType();
if ("tag".equals(eventType)) {
// for BB Cloud date is valued only in case of annotated tag
Date tagDate = newChange.getDate() != null ? newChange.getDate() : target.getDate();
if (tagDate == null) {
// fall back to the jenkins time when the request is processed
tagDate = new Date();
}
head = new BitbucketTagSCMHead(newChange.getName(), tagDate.getTime());
} else {
head = new BranchSCMHead(newChange.getName());
SCMHead head = null;
String eventType = changeRef.getType();
if ("tag".equals(eventType)) {
// for BB Cloud date is valued only in case of annotated tag
Date tagDate = changeRef.getDate() != null ? changeRef.getDate() : target.getDate();
if (tagDate == null) {
// fall back to the jenkins time when the request is processed
tagDate = new Date();
}
result.put(head, new AbstractGitSCMSource.SCMRevisionImpl(head, target.getHash()));
head = new BitbucketTagSCMHead(changeRef.getName(), tagDate.getTime());
} else {
head = new BranchSCMHead(changeRef.getName());
}
result.put(head, new AbstractGitSCMSource.SCMRevisionImpl(head, target.getHash()));
}
return result;
}
Expand All @@ -116,58 +110,46 @@
public Iterable<BitbucketBranch> getTags(BitbucketSCMSource src) {
List<BitbucketBranch> tags = new ArrayList<>();
for (BitbucketPushEvent.Change change: getPayload().getChanges()) {
if (!change.isClosed()) {
// created is true
Reference newChange = change.getNew();
Target target = newChange.getTarget();
Reference changeRef = change.isCreated() ? change.getNew() : change.getOld();
Target target = changeRef.getTarget();

String eventType = newChange.getType();
if ("tag".equals(eventType)) {
// for BB Cloud date is valued only in case of annotated tag
Date tagDate = newChange.getDate() != null ? newChange.getDate() : target.getDate();
if (tagDate == null) {
// fall back to the jenkins time when the request is processed
tagDate = new Date();
}
String hash = target.getHash();
BitbucketCloudBranch.Target tagTarget = new BitbucketCloudBranch.Target(hash, "", tagDate, new BitbucketCloudAuthor(target.getAuthor()));
@SuppressWarnings("deprecation")
BitbucketCloudBranch.Head head = new BitbucketCloudBranch.Head(hash);
tags.add(new BitbucketCloudBranch(newChange.getName(), tagTarget, List.of(head)));
String eventType = changeRef.getType();
if ("tag".equals(eventType)) {
// for BB Cloud date is valued only in case of annotated tag
Date tagDate = changeRef.getDate() != null ? changeRef.getDate() : target.getDate();
if (tagDate == null) {
// fall back to the jenkins time when the request is processed
tagDate = new Date();
}
BitbucketCloudBranch tagRef = new BitbucketCloudBranch(changeRef.getName(), target.getHash(), tagDate.getTime());
tagRef.setAuthor(target.getAuthor());
tags.add(tagRef);
}
}
return tags;
}

@Override
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) throws InterruptedException {
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) {
return Collections.emptyList();
}

@Override
public Iterable<BitbucketBranch> getBranches(BitbucketSCMSource src) throws InterruptedException {
public Iterable<BitbucketBranch> getBranches(BitbucketSCMSource src) {
List<BitbucketBranch> branches = new ArrayList<>();
for (BitbucketPushEvent.Change change: getPayload().getChanges()) {
if (!change.isClosed()) {
// created is true
Reference newChange = change.getNew();
Target target = newChange.getTarget();
Reference changeRef = change.isCreated() ? change.getNew() : change.getOld();
Target target = changeRef.getTarget();

String eventType = newChange.getType();
if ("branch".equals(eventType)) {
// for BB Cloud date is valued only in case of annotated tag
Date commitDate = newChange.getDate() != null ? newChange.getDate() : target.getDate();
if (commitDate == null) {
// fall back to the jenkins time when the request is processed
commitDate = new Date();
}
String hash = target.getHash();
BitbucketCloudBranch.Target commitTarget = new BitbucketCloudBranch.Target(hash, "", commitDate, new BitbucketCloudAuthor(target.getAuthor()));
@SuppressWarnings("deprecation")
BitbucketCloudBranch.Head head = new BitbucketCloudBranch.Head(hash);
branches.add(new BitbucketCloudBranch(newChange.getName(), commitTarget, List.of(head)));
String eventType = changeRef.getType();
if ("branch".equals(eventType)) {
// for BB Cloud date is valued only in case of annotated tag
Date commitDate = changeRef.getDate() != null ? changeRef.getDate() : target.getDate();

Check warning on line 147 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/impl/webhook/cloud/CloudPushEvent.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 147 is only partially covered, one branch is missing
if (commitDate == null) {
// fall back to the jenkins time when the request is processed
commitDate = new Date();
}
branches.add(new BitbucketCloudBranch(changeRef.getName(), target.getHash(), commitDate.getTime()));
}
}
return branches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Map<SCMHead, SCMRevision> heads(@NonNull SCMSource source) {
}

@Override
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) throws InterruptedException {
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) {
if (hookEvent == PULL_REQUEST_DECLINED || hookEvent == PULL_REQUEST_MERGED) {
return Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected Map<SCMHead, SCMRevision> heads(@NonNull BitbucketSCMSource source) {
}

@Override
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) throws InterruptedException {
public Iterable<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) {
if (Type.REMOVED.equals(getType())) {
return Collections.emptySet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private Map<String, BitbucketServerPullRequest> loadPullRequests(BitbucketSCMSou
}

@Override
public Collection<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) throws InterruptedException {
public Collection<BitbucketPullRequest> getPullRequests(BitbucketSCMSource src) {
List<BitbucketPullRequest> prs = new ArrayList<>();
for (final NativeServerChange change : getPayload()) {
Map<String, BitbucketServerPullRequest> prsForChange = getPullRequests(src, change);
Expand Down
Loading
Loading