@@ -79,6 +79,13 @@ public class BitbucketHgSCMBuilder extends MercurialSCMBuilder<BitbucketHgSCMBui
7979 @ NonNull
8080 private List <BitbucketHref > cloneLinks = Collections .emptyList ();
8181
82+ /**
83+ * The {@link BitbucketRepositoryProtocol} that should be used.
84+ * Enables support for blank SSH credentials.
85+ */
86+ @ NonNull
87+ private BitbucketRepositoryProtocol protocol = BitbucketRepositoryProtocol .HTTP ;
88+
8289 /**
8390 * Constructor.
8491 *
@@ -90,7 +97,7 @@ public class BitbucketHgSCMBuilder extends MercurialSCMBuilder<BitbucketHgSCMBui
9097 * credentials or connecting anonymously.
9198 */
9299 public BitbucketHgSCMBuilder (@ NonNull BitbucketSCMSource scmSource , @ NonNull SCMHead head ,
93- @ CheckForNull SCMRevision revision , String credentialsId ) {
100+ @ CheckForNull SCMRevision revision , @ CheckForNull String credentialsId ) {
94101 super (head , revision , /*dummy value*/ scmSource .getServerUrl (), credentialsId );
95102 this .scmSource = scmSource ;
96103 AbstractBitbucketEndpoint endpoint =
@@ -106,6 +113,9 @@ public BitbucketHgSCMBuilder(@NonNull BitbucketSCMSource scmSource, @NonNull SCM
106113 } catch (MalformedURLException e ) {
107114 // ignore, we are providing a well formed URL and if we are not then we shouldn't apply a browser
108115 }
116+
117+ // Test for protocol
118+ withCredentialsId (credentialsId , null );
109119 }
110120
111121 /**
@@ -140,6 +150,43 @@ public List<BitbucketHref> cloneLinks() {
140150 return Collections .unmodifiableList (cloneLinks );
141151 }
142152
153+ /**
154+ * Configures the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to the
155+ * {@link #source()}
156+ *
157+ * @param credentialsId the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to
158+ * the {@link #source()} or {@code null} to let the git client choose between providing its own
159+ * credentials or connecting anonymously.
160+ * @param protocol the {@link BitbucketRepositoryProtocol} of the {@link Credentials} to use or {@code null}
161+ * to detect the the protocol based on the credentialsId. Defaults to HTTP if credentials are
162+ * {@code null}. Enables support for blank SSH credentials.
163+ * @return {@code this} for method chaining.
164+ */
165+ @ NonNull
166+ public BitbucketHgSCMBuilder withCredentialsId (String credentialsId , BitbucketRepositoryProtocol protocol ) {
167+ if (StringUtils .isNotBlank (credentialsId )) {
168+ StandardCredentials credentials = BitbucketCredentials .lookupCredentials (
169+ scmSource .getServerUrl (),
170+ scmSource .getOwner (),
171+ credentialsId ,
172+ StandardCredentials .class
173+ );
174+
175+ if (protocol == null ) {
176+ protocol = credentials instanceof SSHUserPrivateKey
177+ ? BitbucketRepositoryProtocol .SSH
178+ : BitbucketRepositoryProtocol .HTTP ;
179+ }
180+ } else if (protocol == null ) {
181+ // If we set credentials to empty reset the type to HTTP.
182+ // To set the build to use empty SSH credentials, call withProtocol after setting credentials
183+ protocol = BitbucketRepositoryProtocol .HTTP ;
184+ }
185+
186+ this .protocol = protocol ;
187+ return withCredentialsId (credentialsId );
188+ }
189+
143190 /**
144191 * Updates the {@link MercurialSCMBuilder#withSource(String)} based on the current {@link #head()} and
145192 * {@link #revision()}.
@@ -150,18 +197,6 @@ public List<BitbucketHref> cloneLinks() {
150197 */
151198 @ NonNull
152199 public BitbucketHgSCMBuilder withBitbucketSource () {
153- // Apply clone links and credentials
154- StandardCredentials credentials = StringUtils .isBlank (credentialsId ())
155- ? null
156- : BitbucketCredentials .lookupCredentials (
157- scmSource ().getServerUrl (),
158- scmSource ().getOwner (),
159- credentialsId (),
160- StandardCredentials .class
161- );
162- BitbucketRepositoryProtocol protocol = credentials instanceof SSHUserPrivateKey
163- ? BitbucketRepositoryProtocol .SSH
164- : BitbucketRepositoryProtocol .HTTP ;
165200 SCMHead h = head ();
166201 String repoOwner ;
167202 String repository ;
@@ -249,5 +284,4 @@ public MercurialSCM build() {
249284 withRevision (r );
250285 }
251286 }
252-
253287}
0 commit comments