Skip to content
Permalink
Browse files
[FIXED JENKINS-21734] When a job is copied, the aliases as copied ove…
…r and corrupt the new job
  • Loading branch information
olivergondza committed Jun 1, 2014
1 parent 3b7dfb8 commit 616aa104304c2a36f9804a513541af89ac8253da
@@ -29,9 +29,12 @@
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.BuildListener;
import hudson.model.Item;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.RunListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
@@ -63,6 +66,8 @@
*/
public class BuildAliasSetter extends BuildWrapper implements MatrixAggregatable {

private final static Logger LOGGER = Logger.getLogger(BuildAliasSetter.class.getName());

private /*final*/ @Nonnull DescribableList<AliasProvider, AliasProvider.Descriptor> providers;

public BuildAliasSetter(@Nonnull DescribableList<AliasProvider, AliasProvider.Descriptor> providers) {
@@ -264,10 +269,8 @@ public String getDisplayName() {
@Extension
public static class DanglingAliasDeleter extends RunListener<AbstractBuild<?, ?>> {

private final static Logger LOGGER = Logger.getLogger(BuildAliasSetter.class.getName());

/**
* Delete aliases for builds that are being deleted
* Delete aliases for builds that are being deleted.
*/
@Override
public void onDeleted(final AbstractBuild<?, ?> build) {
@@ -288,4 +291,26 @@ public void onDeleted(final AbstractBuild<?, ?> build) {
}
}
}

@Extension
public static class CopyProjectPermalinksEraser extends ItemListener {

/**
* Erase aliases from newly created projects by copying.
*/
@Override
public void onCopied(Item src, Item item) {
if (!(item instanceof Job)) return;

Job<?, ?> job = (Job<?, ?>) item;
PermalinkStorage storage = job.getProperty(PermalinkStorage.class);
if (storage == null) return;

try {
job.removeProperty(storage);
} catch (IOException ex) {
LOGGER.log(Level.SEVERE, "Unable to erase aliases when coppying " + item.getFullName(), ex);
}
}
}
}
@@ -0,0 +1,49 @@
/*
* The MIT License
*
* Copyright (c) 2014 Red Hat, Inc.
*
* 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 org.jenkinsci.plugins.buildaliassetter;

import static org.junit.Assert.assertNull;
import hudson.matrix.MatrixProject;
import hudson.model.TopLevelItem;

import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;

public class IntegrationTest {

public @Rule JenkinsRule j = new JenkinsRule();

@Test @Bug(21734)
public void doNotCopyAliasesWithJob() throws Exception {
MatrixProject src = j.jenkins.createProject(MatrixProject.class, "src");
PermalinkStorage storage = new PermalinkStorage();

src.addProperty(storage);
MatrixProject dst = (MatrixProject) j.jenkins.copy((TopLevelItem) src, "dst");

assertNull("Permalinks copied with job", dst.getProperty(PermalinkStorage.class));
}
}

0 comments on commit 616aa10

Please sign in to comment.