|
32 | 32 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
|
33 | 33 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory;
|
34 | 34 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
|
| 35 | +import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient; |
35 | 36 | import com.cloudbees.plugins.credentials.CredentialsMatchers;
|
36 | 37 | import com.cloudbees.plugins.credentials.CredentialsProvider;
|
37 | 38 | import com.cloudbees.plugins.credentials.common.StandardCredentials;
|
@@ -139,17 +140,36 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
|
139 | 140 | BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials);
|
140 | 141 |
|
141 | 142 | BitbucketApi apiClient = BitbucketApiFactory.newInstance(serverUrl, authenticator, owner, repository);
|
142 |
| - String ref; |
| 143 | + String ref = null; |
| 144 | + |
143 | 145 | if (head instanceof BranchSCMHead) {
|
144 | 146 | ref = head.getName();
|
145 | 147 | } else if (head instanceof PullRequestSCMHead) {
|
| 148 | + // working on a pull request - can be either "HEAD" or "MERGE" |
146 | 149 | 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"; |
149 | 170 | }
|
150 |
| - return null; // TODO support merge revisions somehow |
151 | 171 | } else if (head instanceof BitbucketTagSCMHead) {
|
152 |
| - ref = "tags/" + head.getName(); |
| 172 | + ref = "tags/" + head.getName(); |
153 | 173 | } else {
|
154 | 174 | return null;
|
155 | 175 | }
|
|
0 commit comments