Skip to content

Commit

Permalink
[JENKINS-17680] Upgrade the configuration < 1.26 automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikedam committed Jul 27, 2013
1 parent da0db31 commit addc953
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/main/java/hudson/plugins/copyartifact/CopyArtifact.java
Expand Up @@ -123,6 +123,12 @@ public static class ConverterImpl extends XStream2.PassthruConverter<CopyArtifac
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 CopyArtifactUpgradeListener.
CopyArtifactUpgradeListener.setUpgradeNeeded();
}
}
}

Expand Down Expand Up @@ -154,20 +160,27 @@ public boolean isOptional() {
return optional != null && optional;
}

private void upgradeIfNecessary(AbstractProject<?,?> job) throws IOException {
if (projectName != null) {
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);
parameters = projectName.substring(i + 1);
} else {
project = projectName;
parameters = null;
}
Logger.getLogger(CopyArtifact.class.getName()).log(Level.INFO, "Split {0} into {1} with parameters {2}", new Object[] {projectName, project, parameters});
projectName = null;
job.save();
/* package scope */
boolean upgradeIfNecessary(AbstractProject<?,?> job) throws IOException {
if (!isUpgradeNeeded()) {
return false;
}
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);
parameters = projectName.substring(i + 1);
} else {
project = projectName;
parameters = null;
}
Logger.getLogger(CopyArtifact.class.getName()).log(Level.INFO, "Split {0} into {1} with parameters {2}", new Object[] {projectName, project, parameters});
projectName = null;
job.save();
return true;
}

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

@Override
Expand Down
@@ -0,0 +1,85 @@
/*
* 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 hudson.plugins.copyartifact;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;
import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.Project;
import hudson.model.listeners.ItemListener;

/**
* Upgrade configuration of CopyArtifact after all jobs are loaded.
*/
@Extension
public class CopyArtifactUpgradeListener extends ItemListener {
private static boolean upgradeNeeded = false;
private static Logger LOGGER = Logger.getLogger(CopyArtifactUpgradeListener.class.getName());

/**
* Set {@link CopyArtifactUpgradeListener} work.
*/
synchronized static void setUpgradeNeeded() {
if (!upgradeNeeded) {
LOGGER.info("Upgrade for Copy Artifact is scheduled.");
upgradeNeeded = true;
}
}

/**
* Scan all jobs and upgrade the configuration of Copy Artfifact if needed.
*
* Works when {@link CopyArtifactUpgradeListener#setUpgradeNeeded()} was called.
*/
@Override
public void onLoaded() {
if (!upgradeNeeded) {
return;
}
upgradeNeeded = false;

boolean isUpgraded = false;
for (Project<?,?> project: Jenkins.getInstance().getAllItems(Project.class)) {
for (CopyArtifact target: Util.filter(project.getBuilders(), CopyArtifact.class)) {
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);
}
}
}

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

0 comments on commit addc953

Please sign in to comment.