Skip to content

Commit 7a4955e

Browse files
authored
[JENKINS-73677] Reapply: Decorate GitClient after adding credentials (#1687)
1 parent d270243 commit 7a4955e

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,9 +913,6 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN
913913
Git git = Git.with(listener, environment).in(ws).using(gitExe);
914914

915915
GitClient c = git.getClient();
916-
for (GitSCMExtension ext : extensions) {
917-
c = ext.decorate(this,c);
918-
}
919916

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

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

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

Lines changed: 40 additions & 0 deletions
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;
@@ -2935,6 +2936,30 @@ public void testCommitMessageIsPrintedToLogs() throws Exception {
29352936
r.waitForMessage("Commit message: \"test commit\"", run);
29362937
}
29372938

2939+
@Issue("JENKINS-73677")
2940+
@Test
2941+
public void testExtensionsDecorateClientAfterSettingCredentials() throws Exception {
2942+
assumeTrue("Test class max time " + MAX_SECONDS_FOR_THESE_TESTS + " exceeded", isTimeAvailable());
2943+
FreeStyleProject project = setupSimpleProject("master");
2944+
StandardCredentials extensionCredentials = createCredential(CredentialsScope.GLOBAL, "github");
2945+
store.addCredentials(Domain.global(), extensionCredentials);
2946+
// setup global config
2947+
List<UserRemoteConfig> remoteConfigs = GitSCM.createRepoList("https://github.com/jenkinsci/git-plugin", null);
2948+
project.setScm(new GitSCM(
2949+
remoteConfigs,
2950+
Collections.singletonList(new BranchSpec("master")),
2951+
false,
2952+
null,
2953+
null,
2954+
null,
2955+
List.of(new TestSetCredentialsGitSCMExtension((StandardUsernameCredentials) extensionCredentials))));
2956+
sampleRepo.init();
2957+
sampleRepo.write("file", "v1");
2958+
sampleRepo.git("commit", "--all", "--message=test commit");
2959+
Run<?, ?> run = r.buildAndAssertSuccess(project);
2960+
r.waitForMessage("using GIT_ASKPASS to set credentials " + extensionCredentials.getDescription(), run);
2961+
}
2962+
29382963
private void setupJGit(GitSCM git) {
29392964
git.gitTool="jgit";
29402965
r.jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(new JGitTool(Collections.emptyList()));
@@ -2968,4 +2993,19 @@ private boolean isWindows() {
29682993
private StandardCredentials createCredential(CredentialsScope scope, String id) throws FormException {
29692994
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password-needs-to-be-14");
29702995
}
2996+
2997+
public static class TestSetCredentialsGitSCMExtension extends GitSCMExtension {
2998+
2999+
private final StandardUsernameCredentials credentials;
3000+
3001+
public TestSetCredentialsGitSCMExtension(StandardUsernameCredentials credentials) {
3002+
this.credentials = credentials;
3003+
}
3004+
3005+
@Override
3006+
public GitClient decorate(GitSCM scm, GitClient git) throws GitException {
3007+
git.setCredentials(credentials);
3008+
return git;
3009+
}
3010+
}
29713011
}

0 commit comments

Comments
 (0)