Skip to content
Permalink
Browse files

Merge pull request #17 from ikedam/feature/JENKINS-19843_RebuildParam…

…eterProvider

[JENKINS-19843] Provides an extension point for other plugins to support rebuild plugin.
  • Loading branch information...
shemeersulaiman committed Nov 14, 2013
2 parents 4c59d7e + aa83650 commit bd3fb15730c29d0dbf03955a69a2129f5bdfdf46
@@ -445,4 +445,24 @@ private ParameterValue cloneParameter(ParameterValue oldValue, String newValue)
}
return actions;
}

/**
* @param value the parameter value to show to rebuild.
* @return page for the parameter value.
*/
public RebuildParameterPage getRebuildParameterPage(ParameterValue value) {
for (RebuildParameterProvider provider: RebuildParameterProvider.all()) {
RebuildParameterPage page = provider.getRebuildPage(value);
if (page != null) {
return page;
}
}

// No provider available, use a view provided by rebuild plugin.
RebuildParameterPage page = new RebuildParameterPage(
getClass(),
String.format("%s.jelly", value.getClass().getSimpleName())
);
return page;
}
}
@@ -0,0 +1,55 @@
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.sonyericsson.rebuild;

/**
* An object contains information of the view to show parameters in rebuild page.
*/
public class RebuildParameterPage {
private final Class<?> clazz;
/**
* @return the class for the view.
*/
public Class<?> getClazz() {
return clazz;
}

private final String page;
/**
* @return the path of jelly(or groovy) file.
*/
public String getPage() {
return page;
}

/**
* @param clazz the class for the view.
* @param page the path of jelly(or groovy) file.
*/
public RebuildParameterPage(Class<?> clazz, String page) {
this.clazz = clazz;
this.page = page;
}
}
@@ -0,0 +1,92 @@
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.sonyericsson.rebuild;

import jenkins.model.Jenkins;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.ParameterValue;

/**
* Extension point to provide views to show parameters in rebuild page.
*
* If you want your custom {@link ParameterValue} to work with rebuild plugin,
* do as followings:
* <ol>
* <li>Add a dependency to rebuild plugin to your pom.xml.
* You should specify &lt;optional&gt;true&lt;/optional&gt;
* to have your plugin work without rebuild plugin.</li>
* <li>Define a class implementing {@link RebuildParameterProvider}.</li>
* <li>Annotate the class with {@link Extension}.
* You should specify option=true in {@link Extension} annotation
* to have your plugin work without rebuild plugin.</li>
* <li>Override {@link RebuildParameterProvider#getRebuildPage(ParameterValue)}.
* Don't forget to return <code>null</code> for parameter values
* other than your custom {@link ParameterValue}.
* There are two recommended ways to set values to {@link RebuildParameterPage}:
* <table>
* <tr>
* <th>&nbsp;</th>
* <th>Recommended 1</th>
* <th>Recommended 2</th>
* </tr>
* <tr>
* <th>clazz</th>
* <td>your custom {@link ParameterValue}</td>
* <td>the class implementing {@link RebuildParameterProvider}</td>
* </tr>
* <tr>
* <th>page</th>
* <td>a file in the resource directory of your custom {@link ParameterValue}</td>
* <td>a file in the resource directory of the class implementing {@link RebuildParameterProvider}</td>
* </tr>
* </table>
* </li>
* </ol>
*/
public abstract class RebuildParameterProvider implements ExtensionPoint {
// This is defined not as an interface but as an abstract class.
// If defined as an interface, developers might carelessly apply this
// to mandatory class in their plugin and their plugins get not to work
// without rebuild plugin.

/**
* Provide a view for specified {@link ParameterValue}.
*
* Return null if cannot handle specified {@link ParameterValue}.
*
* @param value a value to be shown in a rebuild page.
* @return page for the parameter value. null for parameter values cannot be handled.
*/
public abstract RebuildParameterPage getRebuildPage(ParameterValue value);

/**
* @return all {@link RebuildParameterProvider} registered to Jenkins.
*/
public static ExtensionList<RebuildParameterProvider> all() {
return Jenkins.getInstance().getExtensionList(RebuildParameterProvider.class);
}
}
@@ -51,9 +51,11 @@ THE SOFTWARE.
<j:set var="paramAction" value="${build.getAction(parmactionClass)}" />
<f:form method="post" action="configSubmit" name="config">
<j:forEach var="parameterValue" items="${paramAction.parameters}">
<j:set var="valuePage" value="${parameterValue.class.simpleName}" />
<j:scope>
<j:set var="page" value="${it.getRebuildParameterPage(parameterValue)}" />
<st:include it="${parameterValue}"
from="${it}" page="${valuePage}.jelly" />
class="${page.clazz}" page="${page.page}" />
</j:scope>
</j:forEach>
<br/>
<br/>
@@ -23,12 +23,14 @@
*/
package com.sonyericsson.rebuild;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Build;
import hudson.model.ParameterValue;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.FreeStyleBuild;
@@ -37,6 +39,7 @@
import hudson.model.Project;
import hudson.model.StringParameterValue;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.TestExtension;

import java.io.IOException;
import java.util.List;
@@ -230,4 +233,93 @@ public boolean isApplicable(AbstractBuild build) {
return true;
}
}

/**
* Creates a new freestyle project and build with a parameter value whose type is
* unknown to rebuild plugin.
* Rebuild and verify that an exception should occur
* when that parameter value does not support {@link RebuildableParameterValue}.
*
* @throws Exception Exception
*/
public void testRebuildUnsupportedUnknownParameterValue() throws Exception {
WebClient wc = createWebClient();
FreeStyleProject project = createFreeStyleProject();

assertBuildStatusSuccess(project.scheduleBuild2(
0,
new Cause.RemoteCause("host", "note"),
new ParametersAction(
new UnsupportedUnknownParameterValue("param1", "value1")
)
));
FreeStyleBuild build = project.getLastBuild();
try {
wc.getPage(build, "rebuild");
fail("Request should fail.");
} catch(FailingHttpStatusCodeException e) {
// always fail for rebuild plugin does not know it.
assertEquals(500, e.getResponse().getStatusCode());
}
}

/**
* Creates a new freestyle project and build with a parameter value whose type is
* unknown to rebuild plugin.
* Verify that rebuild succeeds if that parameter value supports {@link RebuildableParameterValue}.
*
* @throws Exception Exception
*/
public void testRebuildSupportedUnknownParameterValue() throws Exception {
WebClient wc = createWebClient();
FreeStyleProject project = createFreeStyleProject();

assertBuildStatusSuccess(project.scheduleBuild2(
0,
new Cause.RemoteCause("host", "note"),
new ParametersAction(
new SupportedUnknownParameterValue("param1", "value1")
)
));
FreeStyleBuild build = project.getLastBuild();
HtmlPage page = wc.getPage(build, "rebuild");
assertTrue(page.asText(), page.asText().contains("This is a mark for test"));
}

/**
* A parameter value rebuild plugin does not know.
*/
public static class UnsupportedUnknownParameterValue extends StringParameterValue {
private static final long serialVersionUID = 3182218854913929L;

public UnsupportedUnknownParameterValue(String name, String value) {
super(name, value);
}
}

/**
* A parameter value rebuild plugin does not know, but supported by {@link TestRebuildParameterProvider}.
*/
public static class SupportedUnknownParameterValue extends StringParameterValue {
private static final long serialVersionUID = 114922627975966439L;

public SupportedUnknownParameterValue(String name, String value) {
super(name, value);
}
}

/**
* Provides a view for {@link SupportedUnknownParameterValue} when rebuilding.
*/
@TestExtension
public static class TestRebuildParameterProvider extends RebuildParameterProvider {
@Override
public RebuildParameterPage getRebuildPage(ParameterValue value) {
if (!(value instanceof SupportedUnknownParameterValue)) {
return null;
}
RebuildParameterPage page = new RebuildParameterPage(SupportedUnknownParameterValue.class, "rebuild.groovy");
return page;
}
}
}
@@ -0,0 +1,16 @@
package com.sonyericsson.rebuild.RebuildValidatorTest.SupportedUnknownParameterValue;

f = namespace("lib/form")

// "it" will be overridden inside nexted tags.
String itName = it.name
String itValue = it.value

f.entry(title: it.name, description: it.description) {
div(name: "parameter") {
input(type: "hidden", name: "name", value: itName)
input(type: "text", name: "value", value: itValue)
text("This is a mark for test")
}
}

0 comments on commit bd3fb15

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