Skip to content

Commit 843d48b

Browse files
DohbedohMarkEWaite
andauthored
[JENKINS-73677] Decorate GitClient after adding credentials (#1649)
Co-authored-by: Mark Waite <[email protected]>
1 parent 9ba71f1 commit 843d48b

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/main/java/hudson/plugins/git/GitSCM.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -912,9 +912,6 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN
912912
Git git = Git.with(listener, environment).in(ws).using(gitExe);
913913

914914
GitClient c = git.getClient();
915-
for (GitSCMExtension ext : extensions) {
916-
c = ext.decorate(this,c);
917-
}
918915

919916
for (UserRemoteConfig uc : getUserRemoteConfigs()) {
920917
String ucCredentialsId = uc.getCredentialsId();
@@ -938,6 +935,10 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN
938935
}
939936
// TODO add default credentials
940937

938+
for (GitSCMExtension ext : extensions) {
939+
c = ext.decorate(this,c);
940+
}
941+
941942
return c;
942943
}
943944

src/test/java/hudson/plugins/git/GitSCMTest.java

+40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.cloudbees.plugins.credentials.CredentialsStore;
66
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
77
import com.cloudbees.plugins.credentials.common.StandardCredentials;
8+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
89
import com.cloudbees.plugins.credentials.domains.Domain;
910
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
1011
import org.htmlunit.html.HtmlPage;
@@ -2915,6 +2916,30 @@ public void testCommitMessageIsPrintedToLogs() throws Exception {
29152916
r.waitForMessage("Commit message: \"test commit\"", run);
29162917
}
29172918

2919+
@Issue("JENKINS-73677")
2920+
@Test
2921+
public void testExtensionsDecorateClientAfterSettingCredentials() throws Exception {
2922+
assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable());
2923+
FreeStyleProject project = setupSimpleProject("master");
2924+
StandardCredentials extensionCredentials = createCredential(CredentialsScope.GLOBAL, "github");
2925+
store.addCredentials(Domain.global(), extensionCredentials);
2926+
// setup global config
2927+
List<UserRemoteConfig> remoteConfigs = GitSCM.createRepoList("https://github.com/jenkinsci/git-plugin", null);
2928+
project.setScm(new GitSCM(
2929+
remoteConfigs,
2930+
Collections.singletonList(new BranchSpec("master")),
2931+
false,
2932+
null,
2933+
null,
2934+
null,
2935+
List.of(new TestSetCredentialsGitSCMExtension((StandardUsernameCredentials) extensionCredentials))));
2936+
sampleRepo.init();
2937+
sampleRepo.write("file", "v1");
2938+
sampleRepo.git("commit", "--all", "--message=test commit");
2939+
Run<?, ?> run = r.buildAndAssertSuccess(project);
2940+
r.waitForMessage("using GIT_ASKPASS to set credentials " + extensionCredentials.getDescription(), run);
2941+
}
2942+
29182943
private void setupJGit(GitSCM git) {
29192944
git.gitTool="jgit";
29202945
r.jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(new JGitTool(Collections.emptyList()));
@@ -2948,4 +2973,19 @@ private boolean isWindows() {
29482973
private StandardCredentials createCredential(CredentialsScope scope, String id) {
29492974
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password");
29502975
}
2976+
2977+
public static class TestSetCredentialsGitSCMExtension extends GitSCMExtension {
2978+
2979+
private final StandardUsernameCredentials credentials;
2980+
2981+
public TestSetCredentialsGitSCMExtension(StandardUsernameCredentials credentials) {
2982+
this.credentials = credentials;
2983+
}
2984+
2985+
@Override
2986+
public GitClient decorate(GitSCM scm, GitClient git) throws GitException {
2987+
git.setCredentials(credentials);
2988+
return git;
2989+
}
2990+
}
29512991
}

0 commit comments

Comments
 (0)