Skip to content

Commit

Permalink
setup credentials when creating gitClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Oct 9, 2013
1 parent 4d7de06 commit 5bbee1f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>1.7.6</version>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>ssh-credentials</artifactId>
<version>1.4</version>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import hudson.plugins.git.extensions.impl.AuthorInChangelog;
import hudson.plugins.git.extensions.impl.BuildChooserSetting;
import hudson.plugins.git.extensions.impl.LocalBranch;
import hudson.plugins.git.extensions.impl.PreBuildMerge;
import hudson.plugins.git.extensions.impl.RemotePoll;
import hudson.plugins.git.opt.PreBuildMergeOptions;
Expand Down Expand Up @@ -481,7 +480,7 @@ private PollingResult compareRemoteRevisionWithImpl(AbstractProject<?, ?> projec
if (getExtensions().get(RemotePoll.class)!=null && singleBranch != null && buildData.lastBuild != null && buildData.lastBuild.getMarked() != null) {
final EnvVars environment = GitUtils.getPollEnvironment(project, workspace, launcher, listener, false);

GitClient git = createClient(listener, environment, Jenkins.getInstance(), null);
GitClient git = createClient(listener, environment, project, Jenkins.getInstance(), null);

String gitRepo = getParamExpandedRepos(lastBuild).get(0).getURIs().get(0).toString();
ObjectId head = git.getHeadRev(gitRepo, getBranches().get(0).getName());
Expand Down Expand Up @@ -520,22 +519,7 @@ private PollingResult compareRemoteRevisionWithImpl(AbstractProject<?, ?> projec
}
}

GitClient git = createClient(listener, environment, n, workingDirectory);
for (UserRemoteConfig c : getUserRemoteConfigs()) {
if (c.getCredentialsId() != null) {
String url = c.getUrl();
StandardUsernameCredentials credentials = CredentialsMatchers
.firstOrNull(
CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, project,
ACL.SYSTEM, URIRequirementBuilder.fromUri(url).build()),
CredentialsMatchers.allOf(CredentialsMatchers.withId(c.getCredentialsId()),
GitClient.CREDENTIALS_MATCHER));
if (credentials != null) {
git.addCredentials(url, credentials);
}
}
}
// TODO add default credentials
GitClient git = createClient(listener, environment, project, n, workingDirectory);

if (git.hasGitRepo()) {
// Repo is there - do a fetch
Expand Down Expand Up @@ -571,10 +555,10 @@ private PollingResult compareRemoteRevisionWithImpl(AbstractProject<?, ?> projec
public GitClient createClient(BuildListener listener, EnvVars environment, AbstractBuild<?,?> build) throws IOException, InterruptedException {
FilePath ws = workingDirectory(build.getProject(), build.getWorkspace(), environment, listener);
ws.mkdirs(); // ensure it exists
return createClient(listener,environment,build.getBuiltOn(),ws);
return createClient(listener,environment, build.getParent(), build.getBuiltOn(), ws);
}

/*package*/ GitClient createClient(TaskListener listener, EnvVars environment, Node n, FilePath ws) throws IOException, InterruptedException {
/*package*/ GitClient createClient(TaskListener listener, EnvVars environment, AbstractProject project, Node n, FilePath ws) throws IOException, InterruptedException {

String gitExe = getGitExe(n, listener);
Git git = Git.with(listener, environment).in(ws).using(gitExe);
Expand All @@ -583,6 +567,23 @@ public GitClient createClient(BuildListener listener, EnvVars environment, Abstr
for (GitSCMExtension ext : extensions) {
c = ext.decorate(this,c);
}

for (UserRemoteConfig uc : getUserRemoteConfigs()) {
if (uc.getCredentialsId() != null) {
String url = uc.getUrl();
StandardUsernameCredentials credentials = CredentialsMatchers
.firstOrNull(
CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, project,
ACL.SYSTEM, URIRequirementBuilder.fromUri(url).build()),
CredentialsMatchers.allOf(CredentialsMatchers.withId(uc.getCredentialsId()),
GitClient.CREDENTIALS_MATCHER));
if (credentials != null) {
c.addCredentials(url, credentials);
}
}
}
// TODO add default credentials

return c;
}

Expand Down

0 comments on commit 5bbee1f

Please sign in to comment.