Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-19843] Provides an extension point for other plugins to supp…
…ort rebuild plugin.
  • Loading branch information
ikedam committed Oct 6, 2013
1 parent 4c59d7e commit 9da2497
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/sonyericsson/rebuild/RebuildAction.java
Expand Up @@ -445,4 +445,23 @@ private List<Action> constructRebuildCause(AbstractBuild up, ParametersAction pa
}
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();
page.setClazz(getClass());
page.setPage(String.format("%s.jelly", value.getClass().getSimpleName()));
return page;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/sonyericsson/rebuild/RebuildParameterPage.java
@@ -0,0 +1,58 @@
/*
* 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;

/**
* A bean contains information of the view to show parameters in rebuild page.
*/
public class RebuildParameterPage {
private Class<?> clazz = null;
/**
* @return the class for the view.
*/
public Class<?> getClazz() {
return clazz;
}
/**
* @param clazz the class for the view.
*/
public void setClazz(Class<?> clazz) {
this.clazz = clazz;
}

private String page = null;
/**
* @return the path of jelly(or groovy) file.
*/
public String getPage() {
return page;
}
/**
* @param page the path of jelly(or groovy) file.
*/
public void setPage(String page) {
this.page = page;
}
}
@@ -0,0 +1,89 @@
/*
* 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>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);
}
}
Expand Up @@ -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/>
Expand Down
94 changes: 94 additions & 0 deletions src/test/java/com/sonyericsson/rebuild/RebuildValidatorTest.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -230,4 +233,95 @@ 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();
page.setClazz(SupportedUnknownParameterValue.class);
page.setPage("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 9da2497

Please sign in to comment.