Skip to content

Commit

Permalink
Merge pull request #406 from jglick/SCMStep-JENKINS-35247
Browse files Browse the repository at this point in the history
[JENKINS-35247] Moved GitStep to git-plugin
  • Loading branch information
MarkEWaite committed Jun 10, 2016
2 parents 7a31858 + d86c536 commit 7e9edc7
Show file tree
Hide file tree
Showing 8 changed files with 521 additions and 4 deletions.
31 changes: 31 additions & 0 deletions pom.xml
Expand Up @@ -28,6 +28,7 @@
<concurrency>2</concurrency>
<!-- TODO: Ongoing work on https://github.com/MarkEWaite/git-plugin/tree/master-findbugs-fixes. -->
<findbugs.failOnError>false</findbugs.failOnError>
<workflow.version>1.14.2</workflow.version>
</properties>

<build>
Expand Down Expand Up @@ -81,6 +82,17 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<!-- TODO check if http://stackoverflow.com/q/1771561/12916 lets us add the test-jar goal to attach-sources/attach-javadocs executions from the inherited jenkins-release profile -->
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -136,6 +148,11 @@
<artifactId>scm-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>${workflow.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
Expand Down Expand Up @@ -202,6 +219,20 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -135,9 +135,9 @@ public void setSubmoduleCfg(Collection<SubmoduleConfig> submoduleCfg) {
this.submoduleCfg = submoduleCfg;
}

static private List<UserRemoteConfig> createRepoList(String url) {
public static 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 All @@ -149,7 +149,7 @@ static private List<UserRemoteConfig> createRepoList(String url) {
*/
public GitSCM(String repositoryUrl) {
this(
createRepoList(repositoryUrl),
createRepoList(repositoryUrl, null),
Collections.singletonList(new BranchSpec("")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null, null);
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/jenkins/plugins/git/GitStep.java
@@ -0,0 +1,118 @@
/*
* 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 jenkins.plugins.git;

import com.google.inject.Inject;
import hudson.Extension;
import hudson.Util;
import hudson.model.Item;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.SubmoduleConfig;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.LocalBranch;
import hudson.scm.SCM;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.io.IOException;
import java.util.Collections;
import org.jenkinsci.plugins.workflow.steps.scm.SCMStep;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

/**
* Runs Git using {@link GitSCM}.
*/
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;
}

public String getUrl() {
return url;
}

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 = Util.fixEmpty(credentialsId);
}

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

@Extension
public static final class DescriptorImpl extends SCMStepDescriptor {

@Inject
private UserRemoteConfig.DescriptorImpl delegate;

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project,
@QueryParameter String url) {
return delegate.doFillCredentialsIdItems(project, url);
}

public FormValidation doCheckUrl(@AncestorInPath Item project,
@QueryParameter String credentialsId,
@QueryParameter String value) throws IOException, InterruptedException {
return delegate.doCheckUrl(project, credentialsId, value);
}

@Override
public String getFunctionName() {
return "git";
}

@Override
public String getDisplayName() {
return Messages.GitStep_git();
}

}

}
38 changes: 38 additions & 0 deletions src/main/resources/jenkins/plugins/git/GitStep/config.jelly
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form" xmlns:c="/lib/credentials">
<f:entry field="url" title="Repository URL">
<f:textbox/>
</f:entry>
<f:entry field="branch" title="Branch">
<f:textbox default="master"/>
</f:entry>
<f:entry field="credentialsId" title="Credentials">
<c:select/>
</f:entry>
<st:include page="config-generic.jelly" class="org.jenkinsci.plugins.workflow.steps.scm.SCMStep"/>
</j:jelly>
11 changes: 11 additions & 0 deletions src/main/resources/jenkins/plugins/git/GitStep/help.html
@@ -0,0 +1,11 @@
<div>
<p>
Git step. It performs a clone from the specified repository.
</p>
<p>
Note that this step is shorthand for the generic SCM step:<pre>
checkout([$class: 'GitSCM', branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
</pre>
</p>
</div>
3 changes: 2 additions & 1 deletion src/main/resources/jenkins/plugins/git/Messages.properties
Expand Up @@ -21,4 +21,5 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
GitSCMSource.DisplayName=Git
GitSCMSource.DisplayName=Git
GitStep.git=Git
62 changes: 62 additions & 0 deletions src/test/java/jenkins/plugins/git/GitSampleRepoRule.java
@@ -0,0 +1,62 @@
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* 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 jenkins.plugins.git;

import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import org.jenkinsci.plugins.workflow.steps.scm.AbstractSampleDVCSRepoRule;
import org.jvnet.hudson.test.JenkinsRule;

/**
* Manages a sample Git repository.
*/
public final class GitSampleRepoRule extends AbstractSampleDVCSRepoRule {

public void git(String... cmds) throws Exception {
run("git", cmds);
}

@Override
public void init() throws Exception {
run(true, tmp.getRoot(), "git", "version");
git("init");
write("file", "");
git("add", "file");
git("commit", "--message=init");
}

public void notifyCommit(JenkinsRule r) throws Exception {
synchronousPolling(r);
WebResponse webResponse = r.createWebClient().goTo("git/notifyCommit?url=" + bareUrl(), "text/plain").getWebResponse();
System.out.println(webResponse.getContentAsString());
for (NameValuePair pair : webResponse.getResponseHeaders()) {
if (pair.getName().equals("Triggered")) {
System.out.println("Triggered: " + pair.getValue());
}
}
r.waitUntilNoActivity();
}

}

0 comments on commit 7e9edc7

Please sign in to comment.