Skip to content

JENKINS-31843 - Merge build parameter values when "Build is parameterized" #34

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions src/main/java/com/sonyericsson/rebuild/RebuildAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.sonyericsson.rebuild;

import com.google.common.collect.*;
import hudson.Extension;
import hudson.model.Action;

Expand All @@ -32,6 +33,8 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Comparator;

import hudson.matrix.MatrixRun;
import hudson.model.BooleanParameterValue;
Expand All @@ -57,9 +60,6 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import com.sonyericsson.rebuild.RebuildParameterPage;
import com.sonyericsson.rebuild.RebuildParameterProvider;

/**
* Rebuild RootAction implementation class. This class will basically reschedule
* the build with existing parameters.
Expand Down Expand Up @@ -297,7 +297,13 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws Servl
}
}

List<Action> actions = constructRebuildCause(build, new ParametersAction(values));
Set<ParameterValue> mergedValues = ImmutableSortedSet.copyOf(new Comparator<ParameterValue>() {
@Override
public int compare(ParameterValue o1, ParameterValue o2) {
return o1.getName().compareTo(o2.getName());
}
}, Iterables.concat(values, paramAction == null ? new ArrayList<ParameterValue>() : paramAction.getParameters()));
List<Action> actions = constructRebuildCause(build, new ParametersAction(Lists.newArrayList(mergedValues)));
Hudson.getInstance().getQueue().schedule((Queue.Task) build.getParent(), 0, actions);

rsp.sendRedirect("../../");
Expand Down Expand Up @@ -361,14 +367,14 @@ public boolean isRebuildAvailable() {
&& project.hasPermission(Item.BUILD)
&& project.isBuildable()
&& project instanceof Queue.Task
&& !isMatrixRun()
&& !isMatrixRun()
&& !isRebuildDisbaled();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have some formatting changes


}

private boolean isRebuildDisbaled() {
RebuildSettings settings = (RebuildSettings)getProject().getProperty(RebuildSettings.class);

if (settings != null && settings.getRebuildDisabled()) {
return true;
}
Expand Down
108 changes: 108 additions & 0 deletions src/test/java/com/sonyericsson/rebuild/RebuildValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
import java.util.List;
import java.util.concurrent.ExecutionException;

import static com.sonyericsson.rebuild.matchers.StringParameterValuesMatcher.hasStringParamValues;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;

/**
* For testing the extension point.
*
Expand Down Expand Up @@ -393,6 +397,110 @@ public void testRebuildSupportedUnknownParameterValue() throws Exception {
page.asText().contains("This is a mark for test"));
}

public void testNewParametersShouldOverrideExistingParametersIfHaveSameName()
throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to rename this test, I think there's something wrong with the sentence itself and it is not really clear what's the goal of the test, probably something like "newParametersShouldOverrideExistingPatametersIfHaveSameName" or something like that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

FreeStyleProject project = createFreeStyleProject();
project.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("existing", "defaultValue")));

// Build (#1)
project.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("existing", "overridingValue")))
.get();
HtmlPage rebuildConfigPage = createWebClient().getPage(project,
"1/rebuild");
// Rebuild (#2)
submit(rebuildConfigPage.getFormByName("config"));

List<ParameterValue> parameterValues = project.getBuildByNumber(2).getAction(ParametersAction.class).getParameters();

assertThat(parameterValues, hasStringParamValues(new StringParameterValue("existing", "overridingValue")));
assertThat(parameterValues, not(hasStringParamValues(new StringParameterValue("existing", "defaultValue"))));
}

public void testRebuildingLastBuildShouldMaintainExistingParameters()
throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, something like "rebuildingLastBuildShouldMaintainExistingParameters"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

FreeStyleProject project = createFreeStyleProject();
project.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("existing", "defaultValue")));

// Build (#1)
project.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("existing", "overridingValue")))
.get();
HtmlPage rebuildConfigPage = createWebClient().getPage(project,
"1/rebuild");
// Rebuild (#2)
submit(rebuildConfigPage.getFormByName("config"));

HtmlPage projectPage = createWebClient().getPage(project);
WebAssert.assertLinkPresentWithText(projectPage, "Rebuild Last");

HtmlAnchor rebuildHref = projectPage.getAnchorByText("Rebuild Last");
assertEquals("Rebuild Last should point to the second build", "/"
+ project.getUrl() + "lastCompletedBuild/rebuild",
rebuildHref.getHrefAttribute());

List<ParameterValue> parameterValues = project.getLastCompletedBuild().getAction(ParametersAction.class).getParameters();

assertThat(parameterValues, hasStringParamValues(new StringParameterValue("existing", "overridingValue")));
assertThat(parameterValues, not(hasStringParamValues(new StringParameterValue("existing", "defaultValue"))));
}

public void testInjectedParametersShouldOverrideExistingParametersIfHaveSameName()
throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("oldName", "oldValue")));

// Build (#1)
project.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("injectedName", "injectedValue")))
.get();
HtmlPage rebuildConfigPage = createWebClient().getPage(project,
"1/rebuild");
// Rebuild (#2)
submit(rebuildConfigPage.getFormByName("config"));

List<ParameterValue> parameterValues = project.getBuildByNumber(2).getAction(ParametersAction.class).getParameters();

assertThat(parameterValues, hasStringParamValues(
new StringParameterValue("oldName", "oldValue"),
new StringParameterValue("injectedName", "injectedValue")
));
}

public void testRebuildingLastBuildShouldMaintainInjectedParameters()
throws Exception {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

FreeStyleProject project = createFreeStyleProject();
project.addProperty(new ParametersDefinitionProperty(
new StringParameterDefinition("oldName", "oldValue")));

// Build (#1)
project.scheduleBuild2(0, new Cause.UserIdCause(),
new ParametersAction(new StringParameterValue("injectedName", "injectedValue")))
.get();
HtmlPage rebuildConfigPage = createWebClient().getPage(project,
"1/rebuild");
// Rebuild (#2)
submit(rebuildConfigPage.getFormByName("config"));

HtmlPage projectPage = createWebClient().getPage(project);
WebAssert.assertLinkPresentWithText(projectPage, "Rebuild Last");

HtmlAnchor rebuildHref = projectPage.getAnchorByText("Rebuild Last");
assertEquals("Rebuild Last should point to the second build", "/"
+ project.getUrl() + "lastCompletedBuild/rebuild",
rebuildHref.getHrefAttribute());

List<ParameterValue> parameterValues = project.getLastCompletedBuild().getAction(ParametersAction.class).getParameters();

assertThat(parameterValues, hasStringParamValues(
new StringParameterValue("oldName", "oldValue"),
new StringParameterValue("injectedName", "injectedValue")
));
}

/**
* A parameter value rebuild plugin does not know.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.sonyericsson.rebuild.matchers;

import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;
import org.hamcrest.Description;
import org.junit.internal.matchers.TypeSafeMatcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class StringParameterValuesMatcher extends TypeSafeMatcher<Collection<ParameterValue>> {

private final List<StringParameterValue> thisParameters;

public static StringParameterValuesMatcher hasStringParamValues(StringParameterValue... parameters) {
return new StringParameterValuesMatcher(parameters);
}

StringParameterValuesMatcher(StringParameterValue... parameters) {
this.thisParameters = new ArrayList<StringParameterValue>(parameters.length);
for (StringParameterValue parameter : parameters) {
thisParameters.add(parameter);
}
}

@Override
public boolean matchesSafely(Collection<ParameterValue> parameters) {
Map<StringParameterValue, Boolean> resultMap = new HashMap<StringParameterValue, Boolean>();
for (StringParameterValue stringParameterValue : thisParameters) {
resultMap.put(stringParameterValue, false);
for (ParameterValue parameter : parameters) {
if (matchesInternal(stringParameterValue, parameter)) {
resultMap.put(stringParameterValue, true);
break;
}
}
}
return !resultMap.values().contains(false);
}

private boolean matchesInternal(StringParameterValue thisParameter, ParameterValue parameter) {
return parameter instanceof StringParameterValue
&& thisParameter.getName().equals((parameter).getName())
&& thisParameter.value.equals(((StringParameterValue)parameter).value);
}

@Override
public void describeTo(Description description) {
description.appendValueList("<[", ", ", "]>", thisParameters);
}
}