Skip to content

Commit

Permalink
Fix missing credentialsId to be passed to GitSCM when authentication …
Browse files Browse the repository at this point in the history
…is username/password or the standard credential provided by BitbucketAuthenticator is null.
  • Loading branch information
nfalco79 committed Nov 24, 2024
1 parent 89ddddb commit 7fbdce4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.damnhandy.uri.template.UriTemplate;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand All @@ -66,6 +67,7 @@
import hudson.model.Items;
import hudson.model.TaskListener;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.scm.SCM;
import hudson.security.AccessControlled;
import hudson.util.FormFillFailure;
Expand Down Expand Up @@ -1000,12 +1002,32 @@ private BitbucketCommit findPRDestinationCommit(BitbucketPullRequest pr, TaskLis
public SCM build(SCMHead head, SCMRevision revision) {
initCloneLinks();

boolean sshAuth = traits.stream()
.anyMatch(SSHCheckoutTrait.class::isInstance);
String scmCredentialsId = credentialsId;

BitbucketAuthenticator authenticator = authenticator();
return new BitbucketGitSCMBuilder(this, head, revision, null)
.withExtension(new GitClientAuthenticatorExtension(authenticator == null || sshAuth ? null : authenticator.getCredentialsForSCM()))
GitSCMExtension scmExtension;
if (authenticator != null) {

Check warning on line 1009 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1009 is only partially covered, one branch is missing
boolean sshAuth = traits.stream()
.anyMatch(SSHCheckoutTrait.class::isInstance);
if (sshAuth) {
// trait will do the magic
scmCredentialsId = null;
scmExtension = new GitClientAuthenticatorExtension(null);
} else {
StandardUsernameCredentials scmCredentials = authenticator.getCredentialsForSCM();
// extension overrides the configured credentialsId with a custom StandardUsernameCredentials provided by the Authenticator
scmExtension = new GitClientAuthenticatorExtension(scmCredentials);
if (scmCredentials != null) {
// will be overridden by git extension
scmCredentialsId = null;
}
}
} else {

Check warning on line 1025 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 1010-1025 are not covered by tests
scmExtension = new GitClientAuthenticatorExtension(null);
}

return new BitbucketGitSCMBuilder(this, head, revision, scmCredentialsId)
.withExtension(scmExtension)
.withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
.withTraits(traits)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void configureContext(HttpClientContext context, HttpHost host) {
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
}

/*
@Override
public StandardUsernameCredentials getCredentialsForSCM() {
try {
Expand All @@ -88,4 +88,5 @@ public StandardUsernameCredentials getCredentialsForSCM() {
throw new RuntimeException(e);
}
}
*/
}

0 comments on commit 7fbdce4

Please sign in to comment.