diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index b9b3096b50..a50c0b3cdc 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -913,9 +913,6 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN Git git = Git.with(listener, environment).in(ws).using(gitExe); GitClient c = git.getClient(); - for (GitSCMExtension ext : extensions) { - c = ext.decorate(this,c); - } for (UserRemoteConfig uc : getUserRemoteConfigs()) { String ucCredentialsId = uc.getCredentialsId(); @@ -939,6 +936,10 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN } // TODO add default credentials + for (GitSCMExtension ext : extensions) { + c = ext.decorate(this,c); + } + return c; } diff --git a/src/test/java/hudson/plugins/git/GitSCMTest.java b/src/test/java/hudson/plugins/git/GitSCMTest.java index d535db9694..f9cf6fee2e 100644 --- a/src/test/java/hudson/plugins/git/GitSCMTest.java +++ b/src/test/java/hudson/plugins/git/GitSCMTest.java @@ -5,6 +5,7 @@ import com.cloudbees.plugins.credentials.CredentialsStore; import com.cloudbees.plugins.credentials.SystemCredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; import com.cloudbees.plugins.credentials.domains.Domain; import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; import org.htmlunit.html.HtmlPage; @@ -2935,6 +2936,30 @@ public void testCommitMessageIsPrintedToLogs() throws Exception { r.waitForMessage("Commit message: \"test commit\"", run); } + @Issue("JENKINS-73677") + @Test + public void testExtensionsDecorateClientAfterSettingCredentials() throws Exception { + assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable()); + FreeStyleProject project = setupSimpleProject("master"); + StandardCredentials extensionCredentials = createCredential(CredentialsScope.GLOBAL, "github"); + store.addCredentials(Domain.global(), extensionCredentials); + // setup global config + List remoteConfigs = GitSCM.createRepoList("https://github.com/jenkinsci/git-plugin", null); + project.setScm(new GitSCM( + remoteConfigs, + Collections.singletonList(new BranchSpec("master")), + false, + null, + null, + null, + List.of(new TestSetCredentialsGitSCMExtension((StandardUsernameCredentials) extensionCredentials)))); + sampleRepo.init(); + sampleRepo.write("file", "v1"); + sampleRepo.git("commit", "--all", "--message=test commit"); + Run run = r.buildAndAssertSuccess(project); + r.waitForMessage("using GIT_ASKPASS to set credentials " + extensionCredentials.getDescription(), run); + } + private void setupJGit(GitSCM git) { git.gitTool="jgit"; r.jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(new JGitTool(Collections.emptyList())); @@ -2968,4 +2993,19 @@ private boolean isWindows() { private StandardCredentials createCredential(CredentialsScope scope, String id) throws FormException { return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password-needs-to-be-14"); } + + public static class TestSetCredentialsGitSCMExtension extends GitSCMExtension { + + private final StandardUsernameCredentials credentials; + + public TestSetCredentialsGitSCMExtension(StandardUsernameCredentials credentials) { + this.credentials = credentials; + } + + @Override + public GitClient decorate(GitSCM scm, GitClient git) throws GitException { + git.setCredentials(credentials); + return git; + } + } }