From 455263c57692a0e97196e20478d3a569388ffb3d Mon Sep 17 00:00:00 2001 From: Filipe Roque Date: Wed, 20 Nov 2024 00:24:07 +0000 Subject: [PATCH] JENKINS-42475 Add support for pipeline --- pom.xml | 5 ++ .../hudson/plugins/SbtPluginBuilder.java | 15 +++- .../hudson/plugins/SbtPluginBuilderTest.java | 86 +++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java diff --git a/pom.xml b/pom.xml index 22ed04b..f220796 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,11 @@ io.jenkins.plugins commons-lang3-api + + org.jenkinsci.plugins + pipeline-model-definition + test + diff --git a/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java b/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java index 20be400..401ac23 100755 --- a/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java +++ b/src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java @@ -29,6 +29,7 @@ import jenkins.model.Jenkins; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.text.StrSubstitutor; +import org.jenkinsci.Symbol; import org.jenkinsci.remoting.RoleChecker; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; @@ -357,7 +358,7 @@ public SbtInstallation(String name, String home, String sbtArguments, List { public SbtInstallation[] getInstallations() { diff --git a/src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java b/src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java new file mode 100644 index 0000000..d8f762c --- /dev/null +++ b/src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java @@ -0,0 +1,86 @@ +package org.jvnet.hudson.plugins; + +import java.util.List; + +import hudson.model.Cause; +import hudson.model.CauseAction; +import hudson.model.DownloadService; +import hudson.model.Result; +import hudson.tools.InstallSourceProperty; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; + +@WithJenkins +class SbtPluginBuilderTest { + + @Test + public void testSbtInstallation(JenkinsRule jenkinsRule) throws Exception { + final var installationDescriptor = jenkinsRule.get(SbtPluginBuilder.SbtInstallation.DescriptorImpl.class); + Assertions.assertEquals(0, installationDescriptor.getInstallations().length); + + final var installer = new SbtPluginBuilder.SbtInstaller("installer1"); + final var installSourceProperty = new InstallSourceProperty(List.of(installer)); + + final var descriptor = jenkinsRule.jenkins.getDescriptorByType(SbtPluginBuilder.DescriptorImpl.class); + final var sbtInstallation = new SbtPluginBuilder.SbtInstallation("sbt1", null, null, List.of(installSourceProperty)); + descriptor.setInstallations(sbtInstallation); + + Assertions.assertEquals(1, installationDescriptor.getInstallations().length); + } + + @Test + public void testFreestyle(JenkinsRule jenkinsRule) throws Exception { + DownloadService.Downloadable mvnDl = + DownloadService.Downloadable.get("org.jvnet.hudson.plugins.SbtPluginBuilder.SbtInstaller"); + + mvnDl.updateNow(); + + final var installer = new SbtPluginBuilder.SbtInstaller("1.10.5"); + final var installSourceProperty = new InstallSourceProperty(List.of(installer)); + + final var descriptor = jenkinsRule.jenkins.getDescriptorByType(SbtPluginBuilder.DescriptorImpl.class); + final var sbtInstallation = new SbtPluginBuilder.SbtInstallation("sbt1", null, null, List.of(installSourceProperty)); + descriptor.setInstallations(sbtInstallation); + + final var freestyle1 = jenkinsRule.createFreeStyleProject("freestyle1"); + final var sbtPluginBuilder = new SbtPluginBuilder("sbt1", null, null, "about", null); + freestyle1.getBuildersList().add(sbtPluginBuilder); + + final var freeStyleBuildQueueTaskFuture = freestyle1.scheduleBuild2(0, new Cause.UserIdCause()); + + final var freeStyleBuild = freeStyleBuildQueueTaskFuture.get(); + + jenkinsRule.assertBuildStatus(Result.SUCCESS, freeStyleBuild); + jenkinsRule.assertLogContains("1.10.5", freeStyleBuild); + } + + @Test + public void testPipeline(JenkinsRule jenkinsRule) throws Exception { + DownloadService.Downloadable mvnDl = + DownloadService.Downloadable.get("org.jvnet.hudson.plugins.SbtPluginBuilder.SbtInstaller"); + + mvnDl.updateNow(); + + final var installer = new SbtPluginBuilder.SbtInstaller("1.10.5"); + final var installSourceProperty = new InstallSourceProperty(List.of(installer)); + + final var descriptor = jenkinsRule.jenkins.getDescriptorByType(SbtPluginBuilder.DescriptorImpl.class); + final var sbtInstallation = new SbtPluginBuilder.SbtInstallation("sbt1", null, null, List.of(installSourceProperty)); + descriptor.setInstallations(sbtInstallation); + + final var project = jenkinsRule.createProject(WorkflowJob.class, "project1"); + project.setDefinition(new CpsFlowDefinition("pipeline { agent any \n tools { sbt 'sbt1' } \n stages { stage('build') { steps { sh 'sbt about' } } } }", true)); + + final var workflowRunQueueTaskFuture = project.scheduleBuild2(0, new CauseAction(new Cause.UserIdCause())); + + final var workflowRun = workflowRunQueueTaskFuture.get(); + + jenkinsRule.assertBuildStatus(Result.SUCCESS, workflowRun); + jenkinsRule.assertLogContains("1.10.5", workflowRun); + } +}