Skip to content
Permalink
Browse files

[FIXED JENKINS-17875] added variables jenkins, project in SystemGroov…

…yChoiceListProvider.
  • Loading branch information...
ikedam committed Dec 6, 2013
1 parent 01c7e27 commit 3862977e8ea7e1557e2ca1b42e313fff5d057ebb
@@ -26,6 +26,7 @@
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
@@ -42,6 +43,7 @@
import org.codehaus.groovy.control.CompilerConfiguration;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;

/**
* A choice provider whose choices are determined by a Groovy script.
@@ -163,8 +165,15 @@ public FormValidation doTest(@QueryParameter String scriptText)
cl = Thread.currentThread().getContextClassLoader();
}

Binding binding = new Binding();
binding.setVariable("jenkins", Jenkins.getInstance());
if (Stapler.getCurrentRequest() != null) {
binding.setVariable("project", Stapler.getCurrentRequest().findAncestorObject(AbstractProject.class));
} else {
binding.setVariable("project", null);
}
GroovyShell shell =
new GroovyShell(cl, new Binding(), compilerConfig);
new GroovyShell(cl, binding, compilerConfig);

Object out = shell.evaluate(scriptText);
if(out == null)
@@ -6,6 +6,14 @@
<li>Null is ignored (not appeared in choices).</li>
<li>When exception occurred or returned a non-list value, choices will be empty.</li>
</ul>
<p>following variables are pre-defined:</p>
<dl>
<dt>jenkins</dt>
<dd>The instance of <a href="http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html">Jenkins</a>. Just a short cut for Jenkins.getInstance().</dd>
<dt>project</dt>
<dd>The project. An instance of <a href="http://javadoc.jenkins-ci.org/hudson/model/AbstractProject.html">AbstractProject</a>. Available only when a build is triggered from a browser. Otherwise, e.g. when triggered with polling or triggered from CLI, this will be null.</dd>
</dl>
<br>
<br>
<p>Here is an example:</p>
<code style="white-space: pre-wrap;">
@@ -5,6 +5,13 @@
<li>null要素は無視します(選択肢に表示しません)</li>
<li>例外が発生したり、配列でない値を返した場合、選択肢は空になります。</li>
</ul>
<p>スクリプト内では以下の変数が事前に定義されています。</p>
<dl>
<dt>jenkins</dt>
<dd><a href="http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html">Jenkins</a> のインスタンス。 Jenkins.getInstance() のショートカットです。</dd>
<dt>project</dt>
<dd>対象のプロジェクト。<a href="http://javadoc.jenkins-ci.org/hudson/model/AbstractProject.html">AbstractProject</a> のインスタンス。ブラウザからビルドを行った時のみ有効で、それ以外の場合 (ポーリング時、CLIからの起動時など) は null になります。</dd>
</dl>
<br>
<p>スクリプトの例:</p>
<code style="white-space: pre-wrap;">
@@ -29,10 +29,13 @@

import java.io.IOException;

import org.apache.commons.httpclient.HttpStatus;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestEnvironment;
import org.jvnet.hudson.test.TestPluginManager;

import com.gargoylesoftware.htmlunit.WebResponse;

/**
* Utility class for Tests.
* For JenkinsRules have its may tests methods protected in Jenkins < 1.479,
@@ -119,4 +122,41 @@ public FreeStyleProject createFreeStyleProject(String name)
{
return super.createFreeStyleProject(name);
}

/**
* Get Web Client that allows 405 Method Not Allowed.
* This happens when accessing build page of a project with parameters.
*
* @return WebClient
*/
public WebClient createAllow405WebClient()
{
return new WebClient()
{
private static final long serialVersionUID = -7231209645303821638L;

@Override
public void throwFailingHttpStatusCodeExceptionIfNecessary(
WebResponse webResponse)
{
if(webResponse.getStatusCode() == HttpStatus.SC_METHOD_NOT_ALLOWED)
{
// allow 405.
return;
}
super.throwFailingHttpStatusCodeExceptionIfNecessary(webResponse);
}

@Override
public void printContentIfNecessary(WebResponse webResponse)
{
if(webResponse.getStatusCode() == HttpStatus.SC_METHOD_NOT_ALLOWED)
{
// allow 405.
return;
}
super.printContentIfNecessary(webResponse);
}
};
}
}
@@ -24,7 +24,8 @@
package jp.ikedam.jenkins.plugins.extensible_choice_parameter;

import static org.junit.Assert.*;

import hudson.model.FreeStyleProject;
import hudson.model.ParametersDefinitionProperty;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

@@ -33,6 +34,11 @@

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;

import jenkins.model.Jenkins;

@@ -290,4 +296,27 @@ public void testGetChoiceList()
assertEquals("null must return an empty list", 0, ret.size());
}
}

@Test
public void testVariables() throws Exception
{
FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new ExtensibleChoiceParameterDefinition(
"test",
new SystemGroovyChoiceListProvider("[jenkins.rootDir.absolutePath, project.fullName]", null),
false,
"test"
)));

WebClient wc = j.createAllow405WebClient();
HtmlPage page = wc.getPage(p, "build");

List<HtmlElement> elements = page.getElementsByTagName("select");
assertEquals(1, elements.size());
assertTrue(elements.get(0) instanceof HtmlSelect);
HtmlSelect sel = (HtmlSelect)elements.get(0);
assertEquals(2, sel.getOptionSize());
assertEquals(j.jenkins.getRootDir().getAbsolutePath(), sel.getOption(0).getValueAttribute());
assertEquals(p.getFullName(), sel.getOption(1).getValueAttribute());
}
}

0 comments on commit 3862977

Please sign in to comment.
You can’t perform that action at this time.