Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73677] Decorate GitClient after adding credentials #1649

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,6 @@
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();
Expand All @@ -936,8 +933,12 @@
}
}
}
// TODO add default credentials

Check warning on line 936 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: add default credentials

for (GitSCMExtension ext : extensions) {
c = ext.decorate(this,c);
}

return c;
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2915,6 +2916,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<UserRemoteConfig> 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()));
Expand Down Expand Up @@ -2948,4 +2973,19 @@ private boolean isWindows() {
private StandardCredentials createCredential(CredentialsScope scope, String id) {
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password");
}

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;
}
}
}