Skip to content
Permalink
Browse files

Merge pull request #23 from ikedam/feature/JENKINS-17680-3

[JENKINS-17680] Upgrade configurations from version 1.25
  • Loading branch information...
ikedam committed Aug 8, 2013
2 parents 428d32a + 355aedb commit f13ba75afffcd971475875873fefc398600a084b
@@ -32,6 +32,8 @@
import hudson.Util;
import hudson.console.HyperlinkNote;
import hudson.diagnosis.OldDataMonitor;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.maven.MavenModuleSet;
@@ -70,6 +72,10 @@
*/
public class CopyArtifact extends Builder {

// specifies upgradeCopyArtifact is needed to work.
private static boolean upgradeNeeded = false;
private static Logger LOGGER = Logger.getLogger(CopyArtifact.class.getName());

@Deprecated private String projectName;
private String project;
private String parameters;
@@ -110,6 +116,59 @@ public CopyArtifact(String projectName, String parameters, BuildSelector selecto
obj.selector = new StatusBuildSelector(obj.stable != null && obj.stable);
OldDataMonitor.report(context, "1.355"); // Core version# when CopyArtifact 1.2 released
}
if (obj.isUpgradeNeeded()) {
// A Copy Artifact to be upgraded.
// For information of the containing project is needed,
// The upgrade will be performed by upgradeCopyArtifact.
setUpgradeNeeded();
}
}
}

private static synchronized void setUpgradeNeeded() {
if (!upgradeNeeded) {
LOGGER.info("Upgrade for Copy Artifact is scheduled.");
upgradeNeeded = true;
}
}

// get all CopyArtifacts configured to AbstractProject. This works both for Project and MatrixProject.
private static List<CopyArtifact> getCopyArtifactsInProject(AbstractProject<?,?> project) throws IOException {
DescribableList<Builder,Descriptor<Builder>> list =
project instanceof Project ? ((Project<?,?>)project).getBuildersList()
: (project instanceof MatrixProject ?
((MatrixProject)project).getBuildersList() : null);
if (list == null) return Collections.emptyList();
return list.getAll(CopyArtifact.class);
}

@Initializer(after=InitMilestone.JOB_LOADED)
public static void upgradeCopyArtifact() {
if (!upgradeNeeded) {
return;
}
upgradeNeeded = false;

boolean isUpgraded = false;
for (AbstractProject<?,?> project: Jenkins.getInstance().getAllItems(AbstractProject.class)) {
try {
for (CopyArtifact target: getCopyArtifactsInProject(project)) {
try {
if (target.upgradeIfNecessary(project)) {
isUpgraded = true;
}
} catch(IOException e) {
LOGGER.log(Level.SEVERE, String.format("Failed to upgrade CopyArtifact in %s", project.getFullName()), e);
}
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, String.format("Failed to upgrade CopyArtifact in %s", project.getFullName()), e);
}
}

if (!isUpgraded) {
// No CopyArtifact is upgraded.
LOGGER.warning("Update of CopyArtifact is scheduled, but no CopyArtifact to upgrade was found!");
}
}

@@ -141,8 +200,8 @@ public boolean isOptional() {
return optional != null && optional;
}

private void upgradeIfNecessary(AbstractProject<?,?> job) throws IOException {
if (projectName != null) {
private boolean upgradeIfNecessary(AbstractProject<?,?> job) throws IOException {
if (isUpgradeNeeded()) {
int i = projectName.lastIndexOf('/');
if (i != -1 && projectName.indexOf('=', i) != -1 && /* not matrix */Jenkins.getInstance().getItem(projectName, job.getParent(), Job.class) == null) {
project = projectName.substring(0, i);
@@ -151,12 +210,19 @@ private void upgradeIfNecessary(AbstractProject<?,?> job) throws IOException {
project = projectName;
parameters = null;
}
Logger.getLogger(CopyArtifact.class.getName()).log(Level.INFO, "Split {0} into {1} with parameters {2}", new Object[] {projectName, project, parameters});
LOGGER.log(Level.INFO, "Split {0} into {1} with parameters {2}", new Object[] {projectName, project, parameters});
projectName = null;
job.save();
return true;
} else {
return false;
}
}

private boolean isUpgradeNeeded() {
return (projectName != null);
}

@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
@@ -353,12 +419,7 @@ public void onRenamed(Item item, String oldName, String newName) {
}

private static List<CopyArtifact> getCopiers(AbstractProject<?,?> project) throws IOException {
DescribableList<Builder,Descriptor<Builder>> list =
project instanceof Project ? ((Project<?,?>)project).getBuildersList()
: (project instanceof MatrixProject ?
((MatrixProject)project).getBuildersList() : null);
if (list == null) return Collections.emptyList();
List<CopyArtifact> copiers = list.getAll(CopyArtifact.class);
List<CopyArtifact> copiers = CopyArtifact.getCopyArtifactsInProject(project);
for (CopyArtifact copier : copiers) {
copier.upgradeIfNecessary(project);
}
@@ -33,6 +33,7 @@
import hudson.matrix.MatrixRun;
import hudson.maven.MavenModuleSet;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BooleanParameterDefinition;
import hudson.model.BooleanParameterValue;
import hudson.model.Action;
@@ -56,6 +57,7 @@
import hudson.slaves.DumbSlave;
import hudson.slaves.SlaveComputer;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildTrigger;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
@@ -71,6 +73,7 @@
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.TestExtension;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.FailureBuilder;
import org.jvnet.hudson.test.UnstableBuilder;
@@ -918,25 +921,65 @@ public void testFieldValidation() throws Exception {

@LocalData
public void testProjectNameSplit() throws Exception {
FreeStyleProject parameterized = Jenkins.getInstance().getItemByFullName("parameterized", FreeStyleProject.class);
assertNotNull(parameterized);
assertTrue(((BooleanParameterValue) parameterized.getBuildByNumber(2).getAction(ParametersAction.class).getParameter("good")).value);
FreeStyleProject copier = Jenkins.getInstance().getItemByFullName("copier", FreeStyleProject.class);
assertNotNull(copier);
String configXml = copier.getConfigFile().asString();
assertTrue(configXml, configXml.contains("<projectName>matrix/which=two</projectName>"));
FreeStyleBuild build = copier.scheduleBuild2(0).get();
@SuppressWarnings("deprecation") String log = build.getLog();
assertEquals(log, Result.SUCCESS, build.getResult());
assertTrue(log, log.contains("OK"));
configXml = copier.getConfigFile().asString();
assertFalse(configXml, configXml.contains("<projectName>"));
assertTrue(configXml, configXml.contains("<project>plain</project>"));
assertTrue(configXml, configXml.contains("<project>parameterized</project>"));
assertTrue(configXml, configXml.contains("<parameters>good=true</parameters>"));
assertTrue(configXml, configXml.contains("<project>matrix/which=two</project>"));

MatrixProject matrixCopier = Jenkins.getInstance().getItemByFullName("matrix-copier", MatrixProject.class);
assertNotNull(matrixCopier);
configXml = matrixCopier.getConfigFile().asString();
assertFalse(configXml, configXml.contains("<projectName>"));
// When a project is specified with a variable, it is split improperly.
assertTrue(configXml, configXml.contains("<project>matrix</project>"));
assertTrue(configXml, configXml.contains("<parameters>which=${which}</parameters>"));
}

// A builder wrapping another builder.
public static class WrapBuilder extends Builder {
private Builder wrappedBuilder;

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
BuildListener listener) throws InterruptedException, IOException {
return wrappedBuilder.perform(build, launcher, listener);
}

public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

@Override
public String getDisplayName() {
return "WrapBuilder";
}
}
}

@LocalData
public void testWrappedCopierProjectNameSplit() throws Exception {
// Project "copier" is configured with CopyArtifact wrapped with WrapBuilder.
// This causes failure of upgrading on loaded.
// Upgrading is performed when build is triggered.
FreeStyleProject copier = Jenkins.getInstance().getItemByFullName("copier", FreeStyleProject.class);
assertNotNull(copier);
String configXml = copier.getConfigFile().asString();
// not upgraded on loaded
assertTrue(configXml, configXml.contains("<projectName>plain</projectName>"));

// upgraded when a build is triggered.
assertBuildStatusSuccess(copier.scheduleBuild2(0));
configXml = copier.getConfigFile().asString();
assertFalse(configXml, configXml.contains("<projectName>"));
assertTrue(configXml, configXml.contains("<project>plain</project>"));
}

@Bug(17447)
@LocalData
public void testRenameBeforeProjectNameSplit() throws Exception {
Binary file not shown.
Binary file not shown.

0 comments on commit f13ba75

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