Skip to content

Commit

Permalink
[JENKINS-29942] Make project in SystemGroovyChoiceListProvider transi…
Browse files Browse the repository at this point in the history
…ent not to break Serializable constraint.
  • Loading branch information
ikedam committed Nov 28, 2015
1 parent 1777c06 commit 6619f61
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Expand Up @@ -30,6 +30,7 @@
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.XStream2;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -46,6 +47,11 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;

/**
* A choice provider whose choices are determined by a Groovy script.
*/
Expand Down Expand Up @@ -307,7 +313,7 @@ public boolean isUsePredefinedVariables()
* This will be stored in job configuration XML like
* <project class="project" reference="../../../../../.." />
*/
private AbstractProject<?,?> project;
private transient AbstractProject<?,?> project;

/**
* @param project
Expand All @@ -327,4 +333,33 @@ protected AbstractProject<?,?> getProject()
{
return project;
}

public static class ConverterImpl extends XStream2.PassthruConverter<SystemGroovyChoiceListProvider> {
private final Mapper mapper;

public ConverterImpl(XStream2 xstream) {
super(xstream);
mapper = xstream.getMapper();
}

@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
super.marshal(source, writer, context);

// As project is transient, serialize it forcibly.
SystemGroovyChoiceListProvider src = (SystemGroovyChoiceListProvider)source;
if (src.project != null) {
writer.startNode("project");
String attributeName = mapper.aliasForSystemAttribute("class");
if (attributeName != null) {
writer.addAttribute(attributeName, mapper.serializedClass(src.project.getClass()));
}
context.convertAnother(src.project);
writer.endNode();
}
}
@Override protected void callback(SystemGroovyChoiceListProvider obj, UnmarshallingContext context) {
// nothing to do
}
}
}
Expand Up @@ -359,5 +359,14 @@ public void testProjectVariable() throws Exception
assertEquals(0, cli.execute("build", p.getFullName(), "-s"));
j.assertBuildStatusSuccess(p.getLastBuild());
assertEquals(p.getFullName(), ceb.getEnvVars().get("test"));

// this should be preserved even after reboot
p.save();
j.jenkins.reload();
p = j.jenkins.getItemByFullName(p.getFullName(), FreeStyleProject.class);
ceb = p.getBuildersList().get(CaptureEnvironmentBuilder.class);
assertEquals(0, cli.execute("build", p.getFullName(), "-s"));
j.assertBuildStatusSuccess(p.getLastBuild());
assertEquals(p.getFullName(), ceb.getEnvVars().get("test"));
}
}

0 comments on commit 6619f61

Please sign in to comment.