|
32 | 32 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
|
33 | 33 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketMirroredRepository;
|
34 | 34 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketMirroredRepositoryDescriptor;
|
| 35 | +import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketProject; |
35 | 36 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
|
36 | 37 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
|
37 | 38 | import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRequestException;
|
|
44 | 45 | import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
|
45 | 46 | import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint;
|
46 | 47 | import com.cloudbees.jenkins.plugins.bitbucket.hooks.HasPullRequests;
|
| 48 | +import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension; |
| 49 | +import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils; |
47 | 50 | import com.cloudbees.jenkins.plugins.bitbucket.server.BitbucketServerWebhookImplementation;
|
48 | 51 | import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
|
49 | 52 | import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
|
|
75 | 78 | import hudson.util.ListBoxModel;
|
76 | 79 | import java.io.IOException;
|
77 | 80 | import java.io.ObjectStreamException;
|
78 |
| -import java.net.MalformedURLException; |
79 |
| -import java.net.URL; |
80 | 81 | import java.util.ArrayList;
|
81 | 82 | import java.util.Arrays;
|
82 | 83 | import java.util.Collection;
|
|
128 | 129 | import org.jenkinsci.Symbol;
|
129 | 130 | import org.kohsuke.accmod.Restricted;
|
130 | 131 | import org.kohsuke.accmod.restrictions.NoExternalUse;
|
| 132 | +import org.kohsuke.accmod.restrictions.ProtectedExternally; |
131 | 133 | import org.kohsuke.stapler.AncestorInPath;
|
132 | 134 | import org.kohsuke.stapler.DataBoundConstructor;
|
133 | 135 | import org.kohsuke.stapler.DataBoundSetter;
|
@@ -385,6 +387,7 @@ public List<SCMSourceTrait> getTraits() {
|
385 | 387 | return Collections.unmodifiableList(traits);
|
386 | 388 | }
|
387 | 389 |
|
| 390 | + @Override |
388 | 391 | @DataBoundSetter
|
389 | 392 | public void setTraits(@CheckForNull List<SCMSourceTrait> traits) {
|
390 | 393 | this.traits = new ArrayList<>(Util.fixNull(traits));
|
@@ -690,7 +693,7 @@ class Skip extends IOException {
|
690 | 693 | boolean fork = !fullName.equalsIgnoreCase(pull.getSource().getRepository().getFullName());
|
691 | 694 | String pullRepoOwner = pull.getSource().getRepository().getOwnerName();
|
692 | 695 | String pullRepository = pull.getSource().getRepository().getRepositoryName();
|
693 |
| - final BitbucketApi pullBitbucket = fork && originBitbucket instanceof BitbucketCloudApiClient |
| 696 | + final BitbucketApi pullBitbucket = fork && BitbucketApiUtils.isCloud(originBitbucket) |
694 | 697 | ? BitbucketApiFactory.newInstance(
|
695 | 698 | getServerUrl(),
|
696 | 699 | authenticator(),
|
@@ -999,7 +1002,7 @@ private BitbucketCommit findPRDestinationCommit(BitbucketPullRequest pr, TaskLis
|
999 | 1002 | }
|
1000 | 1003 |
|
1001 | 1004 | @Override
|
1002 |
| - public SCM build(SCMHead head, SCMRevision revision) { |
| 1005 | + public SCM build(@NonNull SCMHead head, @CheckForNull SCMRevision revision) { |
1003 | 1006 | initCloneLinks();
|
1004 | 1007 |
|
1005 | 1008 | String scmCredentialsId = credentialsId;
|
@@ -1027,26 +1030,38 @@ public SCM build(SCMHead head, SCMRevision revision) {
|
1027 | 1030 | scmExtension = new GitClientAuthenticatorExtension(null);
|
1028 | 1031 | }
|
1029 | 1032 |
|
| 1033 | + String projectKey = getProjectKey(); |
| 1034 | + |
1030 | 1035 | return new BitbucketGitSCMBuilder(this, head, revision, scmCredentialsId)
|
1031 | 1036 | .withExtension(scmExtension)
|
| 1037 | + .withExtension(new BitbucketEnvVarExtension(getRepoOwner(), getRepository(), projectKey, getServerUrl())) |
1032 | 1038 | .withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
|
1033 | 1039 | .withTraits(traits)
|
1034 | 1040 | .build();
|
1035 | 1041 | }
|
1036 | 1042 |
|
| 1043 | + @CheckForNull |
| 1044 | + @Restricted(ProtectedExternally.class) |
| 1045 | + protected String getProjectKey() { |
| 1046 | + String projectKey = null; |
| 1047 | + try { |
| 1048 | + BitbucketProject project = buildBitbucketClient().getRepository().getProject(); |
| 1049 | + if (project != null) { |
| 1050 | + projectKey = project.getKey(); |
| 1051 | + } |
| 1052 | + } catch (IOException | InterruptedException e) { |
| 1053 | + LOGGER.severe("Failure getting the project key of repository " + getRepository() + " : " + e.getMessage()); |
| 1054 | + } |
| 1055 | + return projectKey; |
| 1056 | + } |
| 1057 | + |
1037 | 1058 | private void setPrimaryCloneLinks(List<BitbucketHref> links) {
|
1038 | 1059 | links.forEach(link -> {
|
1039 | 1060 | if (StringUtils.startsWithIgnoreCase(link.getName(), "http")) {
|
1040 |
| - try { |
1041 |
| - URL linkURL = new URL(link.getHref()); |
1042 |
| - // Remove the username from URL because it will be set into the GIT_URL variable |
1043 |
| - // credentials used to clone or for push/pull could be different than this will cause a failure |
1044 |
| - // Restore the behaviour before mirror link feature. |
1045 |
| - URL cleanURL = new URL(linkURL.getProtocol(), linkURL.getHost(), linkURL.getPort(), linkURL.getFile()); |
1046 |
| - link.setHref(cleanURL.toExternalForm()); |
1047 |
| - } catch (MalformedURLException e) { |
1048 |
| - // do nothing, URL can not be parsed, leave as is |
1049 |
| - } |
| 1061 | + // Remove the username from URL because it will be set into the GIT_URL variable |
| 1062 | + // credentials used to clone or for push/pull could be different than this will cause a failure |
| 1063 | + // Restore the behaviour before mirror link feature. |
| 1064 | + link.setHref(URLUtils.removeAuthority(link.getHref())); |
1050 | 1065 | }
|
1051 | 1066 | });
|
1052 | 1067 | primaryCloneLinks = links;
|
@@ -1112,13 +1127,15 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
|
1112 | 1127 | result.add(new BitbucketDefaultBranch(repoOwner, repository, defaultBranch));
|
1113 | 1128 | }
|
1114 | 1129 | UriTemplate template;
|
1115 |
| - if (BitbucketCloudEndpoint.SERVER_URL.equals(getServerUrl())) { |
| 1130 | + if (BitbucketApiUtils.isCloud(getServerUrl())) { |
1116 | 1131 | template = UriTemplate.fromTemplate(getServerUrl() + CLOUD_REPO_TEMPLATE);
|
1117 | 1132 | } else {
|
1118 | 1133 | template = UriTemplate.fromTemplate(getServerUrl() + SERVER_REPO_TEMPLATE);
|
1119 | 1134 | }
|
1120 |
| - template.set("owner", repoOwner).set("repo", repository); |
1121 |
| - String url = template.expand(); |
| 1135 | + String url = template |
| 1136 | + .set("owner", repoOwner) |
| 1137 | + .set("repo", repository) |
| 1138 | + .expand(); |
1122 | 1139 | result.add(new BitbucketLink("icon-bitbucket-repo", url));
|
1123 | 1140 | result.add(new ObjectMetadataAction(r.getRepositoryName(), null, url));
|
1124 | 1141 | return result;
|
|
0 commit comments