Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
[FIXED JENKINS-26085] Credentials support for git step
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Mar 6, 2015
1 parent 0e956a5 commit 6838a3c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
14 changes: 13 additions & 1 deletion scm-step/README.md
Expand Up @@ -64,7 +64,19 @@ You may specify `poll: false` to disable polling for an SCM checkout.
# Built-in integrations

Currently there are special integrations with the Git (`git` step) and Subversion (`svn` step) plugins.
At the moment these are very simple and take just a `url` parameter.

## git

the `git` step lets you define a repository to clone. For authentication, a reference to a configured credential can
be passed.

```groovy
git url: 'git@git.local.domain:ps/project.git', credentialsId: '67c3072d-b9a7-44fa-a5aa-560ba9c1662f'
```

## svn

At the moment this step is very simple and takes just a `url` parameter.
Richer configuration may come in the future.

# Generic SCM step
Expand Down
7 changes: 7 additions & 0 deletions scm-step/pom.xml
Expand Up @@ -26,5 +26,12 @@
<artifactId>subversion</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -43,6 +43,7 @@ public final class GitStep extends SCMStep {

private final String url;
private String branch = "master";
private String credentialsId;

@DataBoundConstructor public GitStep(String url) {
this.url = url;
Expand All @@ -56,18 +57,26 @@ public String getBranch() {
return branch;
}

public String getCredentialsId() {
return credentialsId;
}

@DataBoundSetter public void setBranch(String branch) {
this.branch = branch;
}

@DataBoundSetter public void setCredentialsId(String credentialsId) {
this.credentialsId = credentialsId;
}

@Override public SCM createSCM() {
return new GitSCM(createRepoList(url), Collections.singletonList(new BranchSpec("*/" + branch)), false, Collections.<SubmoduleConfig>emptyList(), null, null, null);
return new GitSCM(createRepoList(url, credentialsId), Collections.singletonList(new BranchSpec("*/" + branch)), false, Collections.<SubmoduleConfig>emptyList(), null, null, null);
}

// copied from GitSCM
static private List<UserRemoteConfig> createRepoList(String url) {
static private List<UserRemoteConfig> createRepoList(String url, String credentialsId) {
List<UserRemoteConfig> repoList = new ArrayList<UserRemoteConfig>();
repoList.add(new UserRemoteConfig(url, null, null, null));
repoList.add(new UserRemoteConfig(url, null, null, credentialsId));
return repoList;
}

Expand Down
@@ -0,0 +1,55 @@
/*
* The MIT License
*
* Copyright 2014 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.steps.scm;

import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepConfigTester;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
public class GitStepTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Test
public void roundtrip() throws Exception {
GitStep step = new GitStep("git@github.com:jenkinsci/workflow-plugin.git");
Step roundtrip = new StepConfigTester(r).configRoundTrip(step);
r.assertEqualDataBoundBeans(step, roundtrip);
}

@Test
public void roundtrip_withcredentials() throws Exception {
GitStep step = new GitStep("git@github.com:jenkinsci/workflow-plugin.git");
step.setCredentialsId("1234567890abcdef");
Step roundtrip = new StepConfigTester(r).configRoundTrip(step);
r.assertEqualDataBoundBeans(step, roundtrip);
}

}

0 comments on commit 6838a3c

Please sign in to comment.