Skip to content
Closed
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 @@ -32,6 +32,8 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException;
import com.cloudbees.jenkins.plugins.bitbucket.server.PullRequestSCMServerHead;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
Expand Down Expand Up @@ -341,9 +343,16 @@ private void retrievePullRequests(SCMSourceCriteria criteria, SCMHeadObserver ob
Set<SCMHead> includes = observer.getIncludes();
for (final BitbucketPullRequest pull : pulls) {
checkInterrupt();
PullRequestSCMHead head = new PullRequestSCMHead(pull.getSource().getRepository().getOwnerName(),
pull.getSource().getRepository().getRepositoryName(), repositoryType,
pull.getSource().getBranch().getName(), pull);
PullRequestSCMHead head;
if (bitbucket instanceof BitbucketServerAPIClient) {
head = new PullRequestSCMServerHead(pull.getDestination().getRepository().getOwnerName(),
pull.getDestination().getRepository().getRepositoryName(), repositoryType,
pull.getSource().getBranch().getName(), pull);
} else {
head = new PullRequestSCMHead(pull.getSource().getRepository().getOwnerName(),
pull.getSource().getRepository().getRepositoryName(), repositoryType,
pull.getSource().getBranch().getName(), pull);
}
if (includes != null && !includes.contains(head)) {
continue;
}
Expand Down Expand Up @@ -378,9 +387,9 @@ private void retrievePullRequests(SCMSourceCriteria criteria, SCMHeadObserver ob
new ContributorMetadataAction(pull.getAuthorLogin(), null, null)
);
observe(criteria, observer, listener,
pull.getSource().getRepository().getOwnerName(),
pull.getSource().getRepository().getRepositoryName(),
pull.getSource().getBranch().getName(),
head.getRepoOwner(),
head.getRepository(),
head.getBranchName(),
hash,
head);
if (!observer.isObserving()) {
Expand Down Expand Up @@ -455,7 +464,7 @@ public long lastModified() {
public boolean exists(@NonNull String path) throws IOException {
try {
// TODO should be checking the revision not the head
return bitbucket.checkPathExists(branchName, path);
return bitbucket.checkPathExists(hash, path);
} catch (InterruptedException e) {
throw new IOException("Interrupted", e);
}
Expand Down Expand Up @@ -485,6 +494,10 @@ protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOExc
BitbucketApi bitbucket = head instanceof PullRequestSCMHead
? buildBitbucketClient((PullRequestSCMHead) head)
: buildBitbucketClient();
if (bitbucket instanceof BitbucketServerAPIClient && head instanceof PullRequestSCMHead) {
BitbucketPullRequest pr = bitbucket.getPullRequestById(Integer.valueOf(((PullRequestSCMHead) head).getId()));
return new AbstractGitSCMSource.SCMRevisionImpl(head, pr.getSource().getBranch().getRawNode());
}
String branchName = head instanceof PullRequestSCMHead ? ((PullRequestSCMHead) head).getBranchName() : head.getName();
List<? extends BitbucketBranch> branches = bitbucket.getBranches();
for (BitbucketBranch b : branches) {
Expand Down Expand Up @@ -597,7 +610,7 @@ protected List<UserRemoteConfig> getGitRemoteConfigs(BranchSCMHead head) {
protected List<UserRemoteConfig> getGitRemoteConfigs(PullRequestSCMHead head) {
List<UserRemoteConfig> result = new ArrayList<UserRemoteConfig>();
String remote = getRemote(head.getRepoOwner(), head.getRepository(), BitbucketRepositoryType.GIT);
result.add(new UserRemoteConfig(remote, getRemoteName(), "+refs/heads/" + head.getBranchName(), getCheckoutEffectiveCredentials()));
result.add(new UserRemoteConfig(remote, getRemoteName(), head.getRefSpec(), getCheckoutEffectiveCredentials()));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ public SCMHead getTarget() {
return target;
}

public String getRefSpec() {
return "+refs/heads/" + this.getBranchName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cloudbees.jenkins.plugins.bitbucket.server;

import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryType;

public class PullRequestSCMServerHead extends PullRequestSCMHead {

private static final long serialVersionUID = 1L;

public PullRequestSCMServerHead(String repoOwner, String repository, BitbucketRepositoryType repositoryType,
String branchName, BitbucketPullRequest pr) {
super(repoOwner, repository, repositoryType, branchName, pr);
}

public String getRefSpec() {
return "+refs/pull-requests/" + this.getId() + "/from:" + this.getBranchName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public static BitbucketCloudApiClient getAPIClientMock(BitbucketRepositoryType t

if (includePullRequests) {
when(bitbucket.getPullRequests()).thenReturn(Arrays.asList(getPullRequest()));
when(bitbucket.checkPathExists("my-feature-branch", "markerfile.txt")).thenReturn(true);
when(bitbucket.resolveSourceFullHash(any(BitbucketPullRequestValue.class)))
.thenReturn("e851558f77c098d21af6bb8cc54a423f7cf12147");
when(bitbucket.checkPathExists("e851558f77c098d21af6bb8cc54a423f7cf12147", "markerfile.txt")).thenReturn(true);
}

// mock file exists
when(bitbucket.checkPathExists("branch1", "markerfile.txt")).thenReturn(true);
when(bitbucket.checkPathExists("branch2", "markerfile.txt")).thenReturn(false);
when(bitbucket.checkPathExists("52fc8e220d77ec400f7fc96a91d2fd0bb1bc553a", "markerfile.txt")).thenReturn(true);
when(bitbucket.checkPathExists("707c59ce8292c927dddb6807fcf9c3c5e7c9b00f", "markerfile.txt")).thenReturn(false);

// Team discovering mocks
when(bitbucket.getTeam()).thenReturn(getTeam());
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/integration/ScanningFailuresTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void getBranchesFails(Callable<Throwable> exception, Result expectedResu
when(api.resolveCommit(sampleRepo.head())).thenReturn(commit);
when(commit.getDateMillis()).thenReturn(System.currentTimeMillis());

when(api.checkPathExists("master", "Jenkinsfile")).thenReturn(true);
when(api.checkPathExists(sampleRepo.head(), "Jenkinsfile")).thenReturn(true);

when(api.getRepositoryUri(eq(BitbucketRepositoryType.GIT),
any(BitbucketRepositoryProtocol.class),
Expand Down Expand Up @@ -191,7 +191,7 @@ public void checkPathExistsFails() throws Exception {
when(api.resolveCommit(sampleRepo.head())).thenReturn(commit);
when(commit.getDateMillis()).thenReturn(System.currentTimeMillis());

when(api.checkPathExists("master", "Jenkinsfile")).thenReturn(true);
when(api.checkPathExists(sampleRepo.head(), "Jenkinsfile")).thenReturn(true);

when(api.getRepositoryUri(eq(BitbucketRepositoryType.GIT),
any(BitbucketRepositoryProtocol.class),
Expand All @@ -218,7 +218,7 @@ public void checkPathExistsFails() throws Exception {
assertThat(master, notNullValue());

// an error in checkPathExists(...)
when(api.checkPathExists("master", "Jenkinsfile")).thenThrow(new IOException(message));
when(api.checkPathExists(sampleRepo.head(), "Jenkinsfile")).thenThrow(new IOException(message));

mp.scheduleBuild2(0).getFuture().get();
assertThat(mp.getIndexing().getResult(), is(Result.FAILURE));
Expand Down Expand Up @@ -249,7 +249,7 @@ public void resolveCommitFails() throws Exception {
when(api.resolveCommit(sampleRepo.head())).thenReturn(commit);
when(commit.getDateMillis()).thenReturn(System.currentTimeMillis());

when(api.checkPathExists("master", "Jenkinsfile")).thenReturn(true);
when(api.checkPathExists(sampleRepo.head(), "Jenkinsfile")).thenReturn(true);

when(api.getRepositoryUri(eq(BitbucketRepositoryType.GIT),
any(BitbucketRepositoryProtocol.class),
Expand Down Expand Up @@ -311,7 +311,7 @@ public void branchRemoved() throws Exception {
when(api.resolveCommit(sampleRepo.head())).thenReturn(commit);
when(commit.getDateMillis()).thenReturn(System.currentTimeMillis());

when(api.checkPathExists("master", "Jenkinsfile")).thenReturn(true);
when(api.checkPathExists(sampleRepo.head(), "Jenkinsfile")).thenReturn(true);

when(api.getRepositoryUri(eq(BitbucketRepositoryType.GIT),
any(BitbucketRepositoryProtocol.class),
Expand Down