Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-65813] Add @Symbol to support pipeline #80

Merged
merged 5 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Active Choices plug-in changelog

## Version 2.8.0 (2023/10/??)

1. [#80](https://github.com/jenkinsci/active-choices-plugin/pull/80): Add `@Symbol` to support declarative pipelines (thanks to @mamh2021, original submitted in #43)

## Version 2.7.2 (2023/08/17)

1. [#85](https://github.com/jenkinsci/active-choices-plugin/pull/85): fix: When configured using a pipeline, handle null filterable #85, thanks @rahulsom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.model.Script;
import org.kohsuke.stapler.DataBoundConstructor;
import org.jenkinsci.Symbol;

import java.util.Objects;

Expand Down Expand Up @@ -156,13 +157,18 @@ public Integer getFilterLength() {
// --- descriptor

@Extension
@Symbol({"reactiveChoice"})
public static final class DescriptImpl extends UnoChoiceParameterDescriptor {

@Override
public String getDisplayName() {
return "Active Choices Reactive Parameter";
}

@Override
public String getHelpFile() {
return "/plugin/uno-choice/help/parameter/active-choices-cascade-choice.html";
}
}

}
6 changes: 6 additions & 0 deletions src/main/java/org/biouno/unochoice/ChoiceParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.model.Script;
import org.kohsuke.stapler.DataBoundConstructor;
import org.jenkinsci.Symbol;

import java.util.Objects;

Expand Down Expand Up @@ -143,13 +144,18 @@ public Integer getFilterLength() {
// --- descriptor

@Extension
@Symbol({"activeChoice"})
public static final class DescriptImpl extends UnoChoiceParameterDescriptor {

@Override
public String getDisplayName() {
return "Active Choices Parameter";
}

@Override
public String getHelpFile() {
return "/plugin/uno-choice/help/parameter/active-choices-choice.html";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.model.Script;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -132,6 +133,7 @@ public String getChoicesAsStringForUI() {
// --- descriptor

@Extension
@Symbol({"activeChoiceHtml"})
public static final class DescriptorImpl extends UnoChoiceParameterDescriptor {

private AbstractProject<?, ?> project;
Expand Down Expand Up @@ -174,6 +176,11 @@ public FormValidation doCheckRequired(@QueryParameter String value) {
return FormValidation.ok();
}

@Override
public String getHelpFile() {
return "/plugin/uno-choice/help/parameter/active-choices-dynamic-reference-parameter.html";
}

}

}
2 changes: 2 additions & 0 deletions src/main/java/org/biouno/unochoice/model/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import javax.annotation.Nullable;

import org.biouno.unochoice.util.SafeHtmlExtendedMarkupFormatter;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ApprovalContext;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -311,6 +312,7 @@ public boolean equals(Object obj) {
// --- descriptor

@Extension
@Symbol({"groovyScript"})
public static class DescriptorImpl extends ScriptDescriptor {
/*
* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import edu.umd.cs.findbugs.annotations.NonNull;
import org.biouno.unochoice.util.Utils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.scriptler.ScriptlerManagement;
import org.jenkinsci.plugins.scriptler.builder.ScriptlerBuilder;
import org.jenkinsci.plugins.scriptler.config.Parameter;
Expand Down Expand Up @@ -231,6 +232,7 @@ public GroovyScript toGroovyScript() {
// --- descriptor

@Extension(optional = true)
@Symbol({"scriptlerScript"})
public static class DescriptorImpl extends ScriptDescriptor {
static {
// make sure this class fails to load during extension discovery if scriptler isn't present
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<p>Accepts another parameter as reference, and updates itself
whenever the other parameter is changed. The value of the
other parameter is made available as a variable that can
be used in this parameter's script.</p>
</div>
6 changes: 6 additions & 0 deletions src/main/webapp/help/parameter/active-choices-choice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<p>This is a simple parameter that executes a script and
renders the returned values as an HTML element. This
element can be a list, a dropdown box, radio or check
boxes.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div>
<p>This parameter is a special cascade choice parameter. It was
designed based on the cascade choice parameter, so it is also
able to re-render itself whenever another parameter changes.</p>

<p>The difference of this parameter is that users have more
freedom on how the HTML element is rendered (if rendered at
all). This is useful in cases where users want to display a
custom HTML element, write JavaScript that renders something
like D3.js or another JavaScript library.</p>
</div>
41 changes: 41 additions & 0 deletions src/test/java/org/biouno/unochoice/UiAcceptanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.lang3.StringUtils;
import org.htmlunit.ElementNotFoundException;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -10,11 +11,13 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

Expand All @@ -26,6 +29,7 @@
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UiAcceptanceTest {
Expand Down Expand Up @@ -66,13 +70,35 @@ public void setUp() throws Exception {
driver = new ChromeDriver(new ChromeOptions());
}
wait = new WebDriverWait(driver, MAX_WAIT);
driver.manage().window().setSize(new Dimension(1440, 900));
}

@After
public void tearDown() {
driver.quit();
}

@LocalData("test")
@Test
public void testHelpFiles() throws Exception {
// Load the page
driver.get(j.getURL().toString() + "job/test/configure");

// Get the help container div of PARAM1.
final WebElement param1ParamDiv = findParamDiv("PARAM1");

final WebElement helpTextDiv = param1ParamDiv.findElement(By.cssSelector("div.help-area > div.help"));
assertFalse(helpTextDiv.isDisplayed());

final WebElement helpIcon = param1ParamDiv.findElement(By.cssSelector("a.jenkins-help-button"));
wait.until(ExpectedConditions.elementToBeClickable(helpIcon));
helpIcon.click();

wait.withMessage(() -> "The help text should have been displayed").until(d -> helpTextDiv.isDisplayed());

assertTrue(helpTextDiv.getText().startsWith("This is a simple parameter"));
}

@LocalData
@Test
public void test() throws Exception {
Expand Down Expand Up @@ -202,6 +228,21 @@ private WebElement findSelect(String paramName) {
return driver.findElement(By.cssSelector("div.active-choice:has([name='name'][value='" + paramName + "']) > select"));
}

private WebElement findParamDiv(String paramName) {
final WebElement paramValueInput = driver.findElement(By.cssSelector("input[name='parameter.name'][value='" + paramName + "']"));
// Up to how many parent levels to we want to search for the help button?
// At the moment it's 3 levels up, so let's give it some room, use 7.
final int parentsLimit = 7;
WebElement parentElement = paramValueInput.findElement(By.xpath("./.."));
for (int i = 0; i < parentsLimit; i++) {
if (parentElement.getAttribute("name") != null && parentElement.getAttribute("name").equals("parameterDefinitions")) {
return parentElement;
}
parentElement = parentElement.findElement(By.xpath("./.."));
}
throw new ElementNotFoundException("div", "parameterDefinitions", "");
}

private void checkOptions(Supplier<WebElement> param1Input, String... options) {
wait.withMessage(() -> {
List<WebElement> optionElements = param1Input.get().findElements(By.cssSelector("option"));
Expand Down
Loading