Skip to content

Commit

Permalink
JENKINS-42475 Add support for pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
froque committed Dec 10, 2024
1 parent b480cdd commit 455263c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -357,7 +358,7 @@ public SbtInstallation(String name, String home, String sbtArguments, List<? ext
}

private static String launderHome(String home) {
if (home.endsWith("/") || home.endsWith("\\")) {
if (home != null && (home.endsWith("/") || home.endsWith("\\"))) {

Check warning on line 361 in src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 361 is only partially covered, 2 branches are missing
// see https://issues.apache.org/bugzilla/show_bug.cgi?id=26947
// Ant doesn't like the trailing slash, especially on Windows
return home.substring(0, home.length() - 1);
Expand All @@ -383,11 +384,21 @@ public SbtInstallation forNode(Node node, TaskListener log) throws IOException,
return new SbtInstallation(getName(), translateFor(node, log), sbtArguments, getProperties().toList());
}

@Override
public void buildEnvVars(EnvVars env) {
String home = getHome();
if (home == null) {

Check warning on line 390 in src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 390 is only partially covered, one branch is missing
return;

Check warning on line 391 in src/main/java/org/jvnet/hudson/plugins/SbtPluginBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 391 is not covered by tests
}
// see EnvVars javadoc for why this adds PATH.
env.put("PATH+SBT", home + "/bin");
}

public String getSbtArguments() {
return sbtArguments;
}

@Extension
@Extension @Symbol("sbt")
public static class DescriptorImpl extends ToolDescriptor<SbtInstallation> {

public SbtInstallation[] getInstallations() {
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/org/jvnet/hudson/plugins/SbtPluginBuilderTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 455263c

Please sign in to comment.