Skip to content

Commit

Permalink
Merge pull request #5 from rinokadijk/master
Browse files Browse the repository at this point in the history
"Rebuild Last" Action and JENKINS-14905
  • Loading branch information
shemeersulaiman committed Jan 29, 2013
2 parents 3f147ce + d460622 commit 1bcd410
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 110 deletions.
61 changes: 35 additions & 26 deletions src/main/java/com/sonyericsson/rebuild/RebuildAction.java
Expand Up @@ -50,7 +50,7 @@
import hudson.model.PasswordParameterValue;
import hudson.model.RunParameterValue;
import hudson.model.StringParameterValue;

import hudson.model.SimpleParameterDefinition;

/**
* Rebuild RootAction implementation class. This class will basically reschedule
Expand All @@ -72,6 +72,9 @@ public class RebuildAction implements Action {
private transient ParametersDefinitionProperty pdp;
private static final String PARAMETERIZED_URL = "parameterized";

public RebuildAction() {
}

/**
* Getter method for pdp.
*
Expand Down Expand Up @@ -164,13 +167,14 @@ public String getUrlName() {
}
}

/**
/**
* Handles the rebuild request and redirects to parameterized
* and non parameterized build when needed.
* @param request StaplerRequest the request.
*
* @param request StaplerRequest the request.
* @param response StaplerResponse the response handler.
* @throws IOException in case of Stapler issues
* @throws ServletException if something unfortunate happens.
* @throws IOException in case of Stapler issues
* @throws ServletException if something unfortunate happens.
* @throws InterruptedException if something unfortunate happens.
*/
public void doIndex(StaplerRequest request, StaplerResponse response) throws
Expand All @@ -191,26 +195,26 @@ public void doIndex(StaplerRequest request, StaplerResponse response) throws
* non parameterized build. .
*
* @param currentBuild current build.
* @param response current response object.
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
* @param response current response object.
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
* @throws InterruptedException if something unfortunate happens.
*/
public void nonParameterizedRebuild(AbstractBuild currentBuild, StaplerResponse
response) throws ServletException, IOException, InterruptedException {
response) throws ServletException, IOException, InterruptedException {
getProject().checkPermission(AbstractProject.BUILD);
Hudson.getInstance().getQueue().schedule(currentBuild.getProject(), 0, null,
new CauseAction(new Cause.UserIdCause()));
response.sendRedirect("../../");
}
}

/**
* Saves the form to the configuration and disk.
*
* @param req StaplerRequest
* @param rsp StaplerResponse
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
* @throws InterruptedException if something unfortunate happens.
*/
public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws
Expand All @@ -231,10 +235,9 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws
if (!formData.isEmpty()) {
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
for (Object o : a) {
JSONObject jo = (JSONObject)o;
JSONObject jo = (JSONObject) o;
String name = jo.getString("name");
ParameterValue parameterValue;
parameterValue = getParameterValue(paramDefProp, name, paramAction, req, jo);
ParameterValue parameterValue = getParameterValue(paramDefProp, name, paramAction, req, jo);
if (parameterValue != null) {
values.add(parameterValue);
}
Expand Down Expand Up @@ -278,23 +281,29 @@ public boolean isRebuildAvailable() {
* Method for getting the ParameterValue instance from ParameterDefinition
* or ParamterAction.
*
* @param paramDefProp ParametersDefinitionProperty
* @param paramDefProp ParametersDefinitionProperty
* @param parameterName Name of the Parameter.
* @param paramAction ParametersAction
* @param req StaplerRequest
* @param jo JSONObject
* @param paramAction ParametersAction
* @param req StaplerRequest
* @param jo JSONObject
* @return ParameterValue instance of subclass of ParameterValue
*/
public ParameterValue getParameterValue(ParametersDefinitionProperty paramDefProp,
String parameterName, ParametersAction paramAction, StaplerRequest req, JSONObject jo) {
String parameterName, ParametersAction paramAction, StaplerRequest req, JSONObject jo) {
ParameterDefinition paramDef;
// this is normal case when user try to rebuild a parameterized job.
if (paramDefProp != null) {
paramDef = paramDefProp.getParameterDefinition(parameterName);
if (paramDef != null) {
return paramDef.createValue(req, jo);
if (paramDefProp != null) {
paramDef = paramDefProp.getParameterDefinition(parameterName);
if (paramDef != null) {
// The copy artifact plugin throws an exception when using createValue(req, jo)
// If the parameter comes from the copy artifact plugin, then use the single argument createValue
if (jo.toString().contains("BuildSelector") || jo.toString().contains("WorkspaceSelector")){
SimpleParameterDefinition parameterDefinition = (SimpleParameterDefinition) paramDefProp.getParameterDefinition(parameterName);
return parameterDefinition.createValue(jo.getString("value"));
}
return paramDef.createValue(req, jo);
}
}
/*
* when user try to rebuild a build that was invoked by
* parameterized trigger plugin in that case ParameterDefinition
Expand All @@ -306,9 +315,9 @@ public ParameterValue getParameterValue(ParametersDefinitionProperty paramDefPro
* In contrast to all other parameterActions, ListSubversionTagsParameterValue uses "tag" instead of "value"
*/
if (jo.containsKey("value")) {
return cloneParameter(paramAction.getParameter(parameterName), jo.getString("value"));
return cloneParameter(paramAction.getParameter(parameterName), jo.getString("value"));
} else {
return cloneParameter(paramAction.getParameter(parameterName), jo.getString("tag"));
return cloneParameter(paramAction.getParameter(parameterName), jo.getString("tag"));
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/sonyericsson/rebuild/RebuildActionFactory.java
@@ -0,0 +1,59 @@
/*
* The MIT License
*
* Copyright 2012 Rino Kadijk.
*
* 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 hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.Hudson;
import hudson.model.Run;
import hudson.model.TransientBuildActionFactory;

import java.util.Collection;

import static java.util.Collections.emptyList;
import static java.util.Collections.singleton;

/**
* Enables rebuild for builds that ran before installing the rebuild plugin.
*/
@Extension
public class RebuildActionFactory extends TransientBuildActionFactory {

@Override
public Collection<? extends Action> createFor(Run target) {
AbstractBuild build = (AbstractBuild) target;
boolean hasRebuildAction = target.getAction(RebuildAction.class) != null;
if (hasRebuildAction) {
return emptyList();
}
for (RebuildValidator rebuildValidator : Hudson.getInstance().
getExtensionList(RebuildValidator.class)) {
if (rebuildValidator.isApplicable(build)) {
return emptyList();
}
}
return singleton(new RebuildAction());
}
}
@@ -0,0 +1,51 @@
/*
* The MIT License
*
* Copyright 2012 Rino Kadijk.
*
* 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;

/**
* Reschedules last completed build for the project if available.
* Otherwise it behaves as if the user clicked on the build now button.
*/
public class RebuildLastCompletedBuildAction extends RebuildAction {

@Override
public String getUrlName() {
boolean isBuildable = getProject() != null && getProject().isBuildable();
boolean hasCompletedBuild = getProject().getLastCompletedBuild() != null;
if (isBuildable) {
if (hasCompletedBuild) {
return getProject().getLastCompletedBuild().getNumber() + "/rebuild";
} else {
return "build?delay=0sec";
}
} else {
return null;
}
}

@Override
public String getDisplayName() {
return "Rebuild Last";
}
}
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright 2012 Rino Kadijk.
*
* 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 hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.TransientProjectActionFactory;

import java.util.Collection;

import static java.util.Collections.singleton;

/**
* Makes the rebuild button available on the project level.
* Rebuilds the last completed build.
*/
@Extension
public class RebuildProjectActionFactory extends TransientProjectActionFactory {

@Override
public Collection<? extends Action> createFor(AbstractProject abstractProject) {
return singleton(new RebuildLastCompletedBuildAction());
}
}
Expand Up @@ -21,7 +21,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.sonyericsson.rebuild;

import hudson.ExtensionPoint;
Expand Down

0 comments on commit 1bcd410

Please sign in to comment.