Skip to content

Commit 868676d

Browse files
seb-liejetersen
authored andcommitted
JENKINS-48737 Fixing lightweight pull request checkouts, also for files in subdirectories (#174)
1 parent 3ea5b8b commit 868676d

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/filesystem/BitbucketSCMFileSystem.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
3333
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory;
3434
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
35+
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient;
3536
import com.cloudbees.plugins.credentials.CredentialsMatchers;
3637
import com.cloudbees.plugins.credentials.CredentialsProvider;
3738
import com.cloudbees.plugins.credentials.common.StandardCredentials;
@@ -139,17 +140,36 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
139140
BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials);
140141

141142
BitbucketApi apiClient = BitbucketApiFactory.newInstance(serverUrl, authenticator, owner, repository);
142-
String ref;
143+
String ref = null;
144+
143145
if (head instanceof BranchSCMHead) {
144146
ref = head.getName();
145147
} else if (head instanceof PullRequestSCMHead) {
148+
// working on a pull request - can be either "HEAD" or "MERGE"
146149
PullRequestSCMHead pr = (PullRequestSCMHead) head;
147-
if (!(pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) && pr.getRepository() != null) {
148-
return new BitbucketSCMFileSystem(apiClient, pr.getOriginName(), rev);
150+
if (pr.getRepository() == null) { // check access to repository (might be forked)
151+
return null;
152+
}
153+
154+
if (apiClient instanceof BitbucketCloudApiClient) {
155+
// support lightweight checkout for branches with same owner and repository
156+
if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.HEAD &&
157+
pr.getRepoOwner().equals(src.getRepoOwner()) &&
158+
pr.getRepository().equals(src.getRepository())) {
159+
ref = pr.getOriginName();
160+
} else {
161+
// Bitbucket cloud does not support refs for pull requests
162+
// Makes lightweight checkout for forks and merge strategy improbable
163+
// TODO waiting for cloud support: https://bitbucket.org/site/master/issues/5814/refify-pull-requests-by-making-them-a-ref
164+
return null;
165+
}
166+
} else if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.HEAD) {
167+
ref = "pull-requests/" + pr.getId() + "/from";
168+
} else if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) {
169+
ref = "pull-requests/" + pr.getId() + "/merge";
149170
}
150-
return null; // TODO support merge revisions somehow
151171
} else if (head instanceof BitbucketTagSCMHead) {
152-
ref = "tags/" + head.getName();
172+
ref = "tags/" + head.getName();
153173
} else {
154174
return null;
155175
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public BitbucketCommit call() throws Exception {
147147
private static final String API_PULL_REQUESTS_PATH = API_REPOSITORY_PATH + "/pull-requests{?start,limit,at,direction,state}";
148148
private static final String API_PULL_REQUEST_PATH = API_REPOSITORY_PATH + "/pull-requests/{id}";
149149
private static final String API_PULL_REQUEST_MERGE_PATH = API_REPOSITORY_PATH + "/pull-requests/{id}/merge";
150-
private static final String API_BROWSE_PATH = API_REPOSITORY_PATH + "/browse{/path*}{?at}";
150+
static final String API_BROWSE_PATH = API_REPOSITORY_PATH + "/browse/{+path}{?at}";
151151
private static final String API_COMMITS_PATH = API_REPOSITORY_PATH + "/commits{/hash}";
152152
private static final String API_PROJECT_PATH = API_BASE_PATH + "/projects/{owner}";
153153
private static final String API_COMMIT_COMMENT_PATH = API_REPOSITORY_PATH + "/commits{/hash}/comments";
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket.server.client;
2+
3+
import com.damnhandy.uri.template.UriTemplate;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import static com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient.API_BROWSE_PATH;
8+
9+
public class BitbucketServerAPIClientTest {
10+
11+
@Test
12+
public void repoBrowsePathFolder() {
13+
String expand = UriTemplate
14+
.fromTemplate(API_BROWSE_PATH)
15+
.set("owner", "test")
16+
.set("repo", "test")
17+
.set("path", "folder/Jenkinsfile")
18+
.set("at", "fix/test")
19+
.expand();
20+
Assert.assertEquals("/rest/api/1.0/projects/test/repos/test/browse/folder/Jenkinsfile?at=fix%2Ftest", expand);
21+
}
22+
23+
@Test
24+
public void repoBrowsePathFile() {
25+
String expand = UriTemplate
26+
.fromTemplate(API_BROWSE_PATH)
27+
.set("owner", "test")
28+
.set("repo", "test")
29+
.set("path", "Jenkinsfile")
30+
.expand();
31+
Assert.assertEquals("/rest/api/1.0/projects/test/repos/test/browse/Jenkinsfile", expand);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)