Skip to content

Commit

Permalink
Merge pull request #65 from jglick/CustomDescribableModel-JENKINS-44892
Browse files Browse the repository at this point in the history
CustomDescribableModel can address SCMSourceRetriever.scm.id problem
  • Loading branch information
dwnusbaum authored Jun 25, 2019
2 parents e7363ca + 8cc94b5 commit 95ebc4c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
23 changes: 14 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@
<properties>
<revision>2.14</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.121.1</jenkins.version>
<jenkins.version>2.138.4</jenkins.version>
<java.level>8</java.level>
<useBeta>true</useBeta>
<workflow-cps-plugin.version>2.69</workflow-cps-plugin.version>
<git-plugin.version>3.6.4</git-plugin.version>
<scm-api-plugin.version>2.2.7</scm-api-plugin.version>
<git-plugin.version>4.0.0-rc</git-plugin.version>
<scm-api-plugin.version>2.3.0</scm-api-plugin.version>
<workflow-scm-step-plugin.version>2.4</workflow-scm-step-plugin.version>
<workflow-multibranch-plugin.version>2.10</workflow-multibranch-plugin.version>
<workflow-step-api-plugin.version>2.19</workflow-step-api-plugin.version>
Expand Down Expand Up @@ -96,11 +97,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>2.6.0</version> <!-- do not pick up an obsolete version from git-server -->
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
Expand All @@ -124,7 +120,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.17</version>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -275,6 +271,15 @@
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git-client</artifactId>
<version>3.0.0-rc</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceDescriptor;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.steps.scm.GenericSCMStep;
import org.jenkinsci.plugins.workflow.steps.scm.SCMStep;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -178,7 +182,7 @@ private static String getFilePathSuffix() {
}

@Symbol("modernSCM")
@Extension public static class DescriptorImpl extends LibraryRetrieverDescriptor {
@Extension public static class DescriptorImpl extends LibraryRetrieverDescriptor implements CustomDescribableModel {

@Override public String getDisplayName() {
return "Modern SCM";
Expand All @@ -198,6 +202,19 @@ public Collection<SCMSourceDescriptor> getSCMDescriptors() {
return descriptors;
}

@Override public UninstantiatedDescribable customUninstantiate(UninstantiatedDescribable ud) {
Object scm = ud.getArguments().get("scm");
if (scm instanceof UninstantiatedDescribable) {
UninstantiatedDescribable scmUd = (UninstantiatedDescribable) scm;
Map<String, Object> scmArguments = new HashMap<>(scmUd.getArguments());
scmArguments.remove("id");
Map<String, Object> retrieverArguments = new HashMap<>(ud.getArguments());
retrieverArguments.put("scm", scmUd.withArguments(scmArguments));
return ud.withArguments(retrieverArguments);
}
return ud;
}

}

@Restricted(DoNotUse.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
import java.util.Collections;
import java.util.List;

import hudson.tools.ToolInstallation;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.plugins.git.traits.BranchDiscoveryTrait;
import jenkins.scm.api.trait.SCMSourceTrait;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.Snippetizer;
import org.jenkinsci.plugins.workflow.cps.SnippetizerTester;
import org.jenkinsci.plugins.workflow.cps.replay.ReplayAction;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
Expand Down Expand Up @@ -69,10 +71,14 @@ public class LibraryStepTest {
r.assertEqualDataBoundBeans(s, stepTester.configRoundTrip(s));
snippetizerTester.assertRoundTrip(s, "library 'foo'");
s = new LibraryStep("foo@master");
s.setRetriever(new SCMSourceRetriever(new GitSCMSource("id", "https://nowhere.net/", "", "origin", "+refs/heads/*:refs/remotes/origin/*", "*", "", true)));
GitSCMSource scmSource = new GitSCMSource("https://nowhere.net/");
scmSource.setTraits(Collections.<SCMSourceTrait>singletonList(new BranchDiscoveryTrait()));
scmSource.setCredentialsId(""); // TODO the setter ought to use fixEmpty
s.setRetriever(new SCMSourceRetriever(scmSource));
s.setChangelog(true);
r.assertEqualDataBoundBeans(s, stepTester.configRoundTrip(s));
snippetizerTester.assertRoundTrip(s, "library identifier: 'foo@master', retriever: modernSCM([$class: 'GitSCMSource', credentialsId: '', id: 'id', remote: 'https://nowhere.net/', traits: [[$class: 'BranchDiscoveryTrait'], [$class: 'IgnoreOnPushNotificationTrait']]])");
// TODO uninstantiate works but SnippetizerTester.assertRoundTrip fails due to differing SCMSource.id values
assertEquals("library identifier: 'foo@master', retriever: modernSCM([$class: 'GitSCMSource', credentialsId: '', remote: 'https://nowhere.net/', traits: [gitBranchDiscovery()]])", Snippetizer.object2Groovy(s));
s.setRetriever(new SCMRetriever(new GitSCM(Collections.singletonList(new UserRemoteConfig("https://nowhere.net/", null, null, null)),
Collections.singletonList(new BranchSpec("${library.foo.version}")),
false, Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList())));
Expand Down

0 comments on commit 95ebc4c

Please sign in to comment.