Skip to content

Commit d10a719

Browse files
authored
[JENKINS-75611] Disabling HTTPS cloning protocol on Data Center causes builds to fail even if SSH checkout feature is configured (#1034)
Fix the credentialsId passed to the SCMBuilder from the SCMSorce using the credentials that be used to checkout from the remote.
1 parent 0e79c3b commit d10a719

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private void withPullRequestRemote(PullRequestSCMHead head, String headName) { /
286286
}
287287

288288
@NonNull
289-
public String getCloudRepositoryUri(@NonNull String owner, @NonNull String repository) {
289+
private String getCloudRepositoryUri(@NonNull String owner, @NonNull String repository) {
290290
switch (protocol) {
291291
case HTTP:
292292
return "https://bitbucket.org/" + owner + "/" + repository + ".git";

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMRevision.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
package com.cloudbees.jenkins.plugins.bitbucket;
2525

2626
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
27-
import com.fasterxml.jackson.databind.util.StdDateFormat;
2827
import edu.umd.cs.findbugs.annotations.NonNull;
29-
import java.text.ParseException;
3028
import java.util.Date;
3129
import java.util.Objects;
3230
import jenkins.plugins.git.AbstractGitSCMSource.SCMRevisionImpl;
@@ -55,15 +53,7 @@ public BitbucketGitSCMRevision(@NonNull SCMHead head, @NonNull BitbucketCommit c
5553
super(head, commit.getHash());
5654
this.message = commit.getMessage();
5755
this.author = commit.getAuthor();
58-
Date commitDate = null;
59-
try {
60-
if (commit.getDate() != null) {
61-
commitDate = new StdDateFormat().parse(commit.getDate());
62-
}
63-
} catch (ParseException e) {
64-
commitDate = null;
65-
}
66-
this.date = commitDate;
56+
this.date = commit.getCommitterDate();
6757
}
6858

6959
/**

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import java.util.List;
9292
import java.util.Locale;
9393
import java.util.Map;
94+
import java.util.Optional;
9495
import java.util.Set;
9596
import java.util.concurrent.ConcurrentHashMap;
9697
import java.util.logging.Level;
@@ -721,17 +722,21 @@ private BitbucketCommit findPRDestinationCommit(BitbucketPullRequest pr, TaskLis
721722
public SCM build(@NonNull SCMHead head, @CheckForNull SCMRevision revision) {
722723
initCloneLinks();
723724

724-
BitbucketGitSCMBuilder scmBuilder = new BitbucketGitSCMBuilder(this, head, revision, credentialsId)
725+
SSHCheckoutTrait sshTrait = SCMTrait.find(traits, SSHCheckoutTrait.class);
726+
String checkoutCredentialsId = sshTrait != null ? sshTrait.getCredentialsId() : credentialsId;
727+
728+
BitbucketGitSCMBuilder scmBuilder = new BitbucketGitSCMBuilder(this, head, revision, checkoutCredentialsId)
725729
.withExtension(new BitbucketEnvVarExtension(getRepoOwner(), getRepository(), getProjectKey(), getServerUrl()))
726730
.withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
727731
.withTraits(traits);
728732

729-
boolean sshAuth = SCMTrait.find(traits, SSHCheckoutTrait.class) != null;
730-
731-
SCMSourceOwner owner = getOwner();
732-
String scmOwner = owner != null ? owner.getFullName() : null;
733+
// checkoutURL must be calculated after set withCloneLinks and credentials
734+
String checkoutURL = scmBuilder.remote();
735+
String scmOwner = Optional.ofNullable(getOwner())
736+
.map(SCMSourceOwner::getFullName)
737+
.orElse(null);
733738
return scmBuilder
734-
.withExtension(new GitClientAuthenticatorExtension(scmBuilder.remote(), serverUrl, scmOwner, sshAuth ? null : credentialsId))
739+
.withExtension(new GitClientAuthenticatorExtension(checkoutURL, serverUrl, scmOwner, sshTrait != null ? null : checkoutCredentialsId))
735740
.build();
736741
}
737742

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketAuthenticator.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ default StandardUsernameCredentials getCredentialsForSCM() {
110110
* Generates context that sub-classes can use to determine if they would be able to authenticate against the
111111
* provided server.
112112
*
113-
* @param serverUrl The URL being authenticated against
113+
* @param serverURL The URL being authenticated against
114114
* @return an {@link AuthenticationTokenContext} for use with the AuthenticationTokens APIs
115115
*/
116-
public static AuthenticationTokenContext<BitbucketAuthenticator> authenticationContext(String serverUrl) {
117-
if (serverUrl == null) {
118-
serverUrl = BitbucketCloudEndpoint.SERVER_URL;
116+
public static AuthenticationTokenContext<BitbucketAuthenticator> authenticationContext(String serverURL) {
117+
if (serverURL == null) {
118+
serverURL = BitbucketCloudEndpoint.SERVER_URL;
119119
}
120120

121-
String scheme = serverUrl.split(":")[0].toLowerCase();
122-
boolean isCloud = BitbucketApiUtils.isCloud(serverUrl);
121+
String scheme = serverURL.split(":")[0].toLowerCase();
122+
boolean isCloud = BitbucketApiUtils.isCloud(serverURL);
123123

124124
return AuthenticationTokenContext.builder(BitbucketAuthenticator.class)
125-
.with(SERVER_URL, serverUrl)
125+
.with(SERVER_URL, serverURL)
126126
.with(SCHEME, scheme)
127127
.with(BITBUCKET_INSTANCE_TYPE, isCloud ? BITBUCKET_INSTANCE_TYPE_CLOUD : BITBUCKET_INSTANCE_TYPE_SERVER)
128128
.build();

0 commit comments

Comments
 (0)