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

Commit

Permalink
Merge pull request #390 from jglick/SCMStep-JENKINS-35247-1.4.x
Browse files Browse the repository at this point in the history
[JENKINS-35247] Delegate to Git and Subversion plugins via reflection (1.4.x)
  • Loading branch information
jglick committed Jun 1, 2016
2 parents 141f269 + 34b8877 commit 8a10c79
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 39 deletions.
2 changes: 1 addition & 1 deletion aggregator/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-aggregator</artifactId>
<packaging>hpi</packaging>
Expand Down
Expand Up @@ -650,6 +650,7 @@ public static void finish(final boolean terminate) {
});
}

@RandomlyFails("TODO sometimes get AssertionError from CpsStepContext$1.run(CpsStepContext.java:247)")
@Issue("JENKINS-26513")
@Test public void executorStepRestart() {
story.addStep(new Statement() {
Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.For;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RandomlyFails;

@For(GenericSCMStep.class) // formerly a dedicated MercurialStep
public class MercurialStepTest {
Expand All @@ -66,6 +67,7 @@ private static void hg(File repo, String... cmds) throws Exception {
r.jenkins.getDescriptorByType(SCMTrigger.DescriptorImpl.class).synchronousPolling = true;
}

@RandomlyFails("TODO sometimes get expected:<2> but was:<3> (probably need to suppress builds during notifyCommit)")
@Test public void multipleSCMs() throws Exception {
File sampleRepo = tmp.newFolder();
hg(sampleRepo, "init");
Expand Down
2 changes: 1 addition & 1 deletion api/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-api</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion basic-steps/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-basic-steps</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cps-global-lib/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-cps-global-lib</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion cps/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-cps</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion durable-task-step/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-durable-task-step</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion job/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-job</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -32,7 +32,7 @@
</parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Workflow: Parent</name>
<url>https://github.com/jenkinsci/workflow-plugin</url>
Expand Down
11 changes: 8 additions & 3 deletions scm-step/pom.xml
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-scm-step</artifactId>
<packaging>hpi</packaging>
Expand All @@ -16,15 +16,20 @@
<artifactId>workflow-step-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>1.11.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>subversion</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
Expand Up @@ -33,23 +33,18 @@
import hudson.Util;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitTool;
import hudson.plugins.git.Messages;
import hudson.plugins.git.SubmoduleConfig;
import hudson.plugins.git.UserRemoteConfig;
import hudson.scm.SCM;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.util.Collection;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jenkinsci.plugins.gitclient.GitURIRequirementsBuilder;
Expand All @@ -59,9 +54,10 @@
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.Nonnull;
import jenkins.model.Jenkins;

/**
* Runs Git using {@link GitSCM}.
* Runs Git using {@code GitSCM}.
*/
public final class GitStep extends SCMStep {

Expand Down Expand Up @@ -93,18 +89,35 @@ public String getCredentialsId() {
this.credentialsId = credentialsId;
}

@SuppressWarnings("rawtypes")
@Override public SCM createSCM() {
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, String credentialsId) {
List<UserRemoteConfig> repoList = new ArrayList<UserRemoteConfig>();
repoList.add(new UserRemoteConfig(url, null, null, credentialsId));
return repoList;
try {
Jenkins j = Jenkins.getInstance();
assert j != null;
ClassLoader cl = j.getPluginManager().uberClassLoader;
return (SCM) cl.loadClass("hudson.plugins.git.GitSCM").getConstructor(
List.class, // userRemoteConfigs
List.class, // branches
Boolean.class, // doGenerateSubmoduleConfigurations
Collection.class, // submoduleCfg
cl.loadClass("hudson.plugins.git.browser.GitRepositoryBrowser"), // browser
String.class, // gitTool
List.class // extensions
).newInstance(
Collections.singletonList(cl.loadClass("hudson.plugins.git.UserRemoteConfig").getConstructor(String.class, String.class, String.class, String.class).newInstance(url, null, null, credentialsId)),
Collections.singletonList(cl.loadClass("hudson.plugins.git.BranchSpec").getConstructor(String.class).newInstance("*/" + branch)),
false,
Collections.EMPTY_LIST,
null,
null,
null);
} catch (RuntimeException x) {
throw x;
} catch (Exception x) {
throw new IllegalStateException(x);
}
}


private static StandardCredentials lookupCredentials(Item project, @Nonnull String credentialId, String uri) {
return CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(StandardCredentials.class, project, ACL.SYSTEM,
Expand All @@ -114,9 +127,19 @@ private static StandardCredentials lookupCredentials(Item project, @Nonnull Stri

@Extension(optional=true) public static final class DescriptorImpl extends SCMStepDescriptor {

public DescriptorImpl() {
// Fail now if dependency plugin not loaded. Descriptor.<init> will actually fail anyway, but this is just to be sure.
GitSCM.class.hashCode();
public DescriptorImpl() throws Exception {
Jenkins j = Jenkins.getInstance();
assert j != null;
ClassLoader cl = j.getPluginManager().uberClassLoader;
// Dependency plugin must be loaded…
cl.loadClass("hudson.plugins.git.GitSCM");
// …but not our own replacement.
try {
cl.loadClass("jenkins.plugins.git.GitStep");
throw new IllegalStateException("skip the old copy of GitStep");
} catch (ClassNotFoundException x) {
// good
}
}

// copy/paste from GitSCM.DescriptorImpl
Expand Down Expand Up @@ -164,7 +187,7 @@ public FormValidation doCheckUrl(@AncestorInPath Item project,
try {
git.getHeadRev(url, "HEAD");
} catch (GitException e) {
return FormValidation.error(Messages.UserRemoteConfig_FailedToConnect(e.getMessage()));
return FormValidation.error("Failed to connect to repository : " + e.getMessage());
}

return FormValidation.ok();
Expand Down
Expand Up @@ -26,11 +26,11 @@

import hudson.Extension;
import hudson.scm.SCM;
import hudson.scm.SubversionSCM;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Runs Subversion using {@link SubversionSCM}.
* Runs Subversion using {@code SubversionSCM}.
*/
public final class SubversionStep extends SCMStep {

Expand All @@ -45,14 +45,33 @@ public String getUrl() {
}

@Override protected SCM createSCM() {
return new SubversionSCM(url); // TODO maybe default to UpdateWithCleanUpdater, etc.
try {
Jenkins j = Jenkins.getInstance();
assert j != null;
ClassLoader cl = j.getPluginManager().uberClassLoader;
return (SCM) cl.loadClass("hudson.scm.SubversionSCM").getConstructor(String.class).newInstance(url);
} catch (RuntimeException x) {
throw x;
} catch (Exception x) {
throw new IllegalStateException(x);
}
}

@Extension(optional=true) public static final class DescriptorImpl extends SCMStepDescriptor {

public DescriptorImpl() {
// Fail now if dependency plugin not loaded. Descriptor.<init> will actually fail anyway, but this is just to be sure.
SubversionSCM.class.hashCode();
public DescriptorImpl() throws Exception {
Jenkins j = Jenkins.getInstance();
assert j != null;
ClassLoader cl = j.getPluginManager().uberClassLoader;
// Dependency plugin must be loaded…
cl.loadClass("hudson.scm.SubversionSCM");
// …but not our own replacement.
try {
cl.loadClass("jenkins.scm.impl.subversion.SubversionStep");
throw new IllegalStateException("skip the old copy of SubversionStep");
} catch (ClassNotFoundException x) {
// good
}
}

@Override public String getFunctionName() {
Expand Down
2 changes: 1 addition & 1 deletion step-api/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-step-api</artifactId>
<packaging>hpi</packaging>
Expand Down
2 changes: 1 addition & 1 deletion support/pom.xml
Expand Up @@ -28,7 +28,7 @@
<parent>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-pom</artifactId>
<version>1.4.3-beta-2-SNAPSHOT</version>
<version>1.4.3-SNAPSHOT</version>
</parent>
<artifactId>workflow-support</artifactId>
<packaging>hpi</packaging>
Expand Down

0 comments on commit 8a10c79

Please sign in to comment.