Skip to content
Permalink
Browse files
[FIX JENKINS-10330] Update project names in fingerprints when project…
…s are renamed.

- Added a RenameListener to Fingerprint. When the listener is invoked for
  renamed projects, it will iterate over all the builds of the project looking
  for fingerprint actions. If the build was the originator of the fingerprint,
  the buildPtr is updated; (had to make the name non-final to allow for this).
  If the build is the consumer of a fingerprint, the usages are updated.

   core/src/main/java/hudson/model/Fingerprint.java

- Expose the fingerprint data inline with the build data in the remote API.
  The renaming code also uses this method as a convenient way to get the
  fingerprints for a build.

   core/src/main/java/hudson/model/AbstractBuild.java

- Test.

   test/src/test/java/hudson/tasks/FingerprinterTest.java
  • Loading branch information
dty committed Aug 26, 2011
1 parent 532ab94 commit 8dfa5e19ea94195e9737f99457297623d4180c7f
@@ -73,6 +73,9 @@
<li class=rfe>
stdin/stdout based remote slaves, such as ones launched via SSH or script, now does a better redirect to avoid interference with JVM output to stdout.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-8856">issue 8856</a>)
<li class=bug>
Project names in fingerprint records weren't updated when a project is renamed.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-10330">issue 10330</a>)
</ul>
</div><!--=TRUNK-END=-->

@@ -718,6 +718,20 @@ protected final boolean preBuild(BuildListener listener,Iterable<? extends Build
}
}

/**
* get the fingerprints associated with this build
*
* @return never null
*/
@Exported(name = "fingerprint", inline = true, visibility = -1)
public Collection<Fingerprint> getBuildFingerprints() {
FingerprintAction fingerprintAction = getAction(FingerprintAction.class);
if (fingerprintAction != null) {
return fingerprintAction.getFingerprints().values();
}
return Collections.<Fingerprint>emptyList();
}

/**
* Gets the changes incorporated into this build.
*
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! 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
@@ -35,10 +35,13 @@
import hudson.Util;
import hudson.XmlFile;
import hudson.BulkChange;
import hudson.Extension;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.SaveableListener;
import hudson.util.HexBinaryConverter;
import hudson.util.Iterators;
import hudson.util.PersistedList;
import hudson.util.RunList;
import hudson.util.XStream2;
import jenkins.model.FingerprintFacet;
import jenkins.model.Jenkins;
@@ -77,7 +80,7 @@
*/
@ExportedBean(defaultVisibility=2)
public static class BuildPtr {
final String name;
String name;
final int number;

public BuildPtr(String name, int number) {
@@ -101,6 +104,10 @@ public String getName() {
return name;
}

void setName(String newName) {
name = newName;
}

/**
* Gets the {@link Job} that this pointer points to,
* or null if such a job no longer exists.
@@ -524,6 +531,31 @@ public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingCont
}
}

@Extension
public static final class ProjectRenameListener extends ItemListener {
@Override
public void onRenamed(Item item, String oldName, String newName) {
if (item instanceof AbstractProject) {
AbstractProject p = Hudson.getInstance().getItemByFullName(newName, AbstractProject.class);
if (p != null) {
RunList builds = p.getBuilds();
for (Object build : builds) {
if (build instanceof AbstractBuild) {
Collection<Fingerprint> fingerprints = ((AbstractBuild)build).getBuildFingerprints();
for (Fingerprint f : fingerprints) {
try {
f.rename(oldName, newName);
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to update fingerprint record " + f.getFileName() + " when " + oldName + " was renamed to " + newName, e);
}
}
}
}
}
}
}
}

private final Date timestamp;

/**
@@ -813,6 +845,32 @@ public synchronized void save() throws IOException {
logger.fine("Saving fingerprint "+file+" took "+(System.currentTimeMillis()-start)+"ms");
}

/**
* Update references to a renamed job in the fingerprint
*/
public synchronized void rename(String oldName, String newName) throws IOException {
boolean touched = false;
if (original != null) {
if (original.getName().equals(oldName)) {
original.setName(newName);
touched = true;
}
}

if (usages != null) {
RangeSet r = usages.get(oldName);
if (r != null) {
usages.put(newName, r);
usages.remove(oldName);
touched = true;
}
}

if (touched) {
save();
}
}

public Api getApi() {
return new Api(this);
}
@@ -0,0 +1,153 @@
/*
* The MIT License
*
* Copyright 2011 Yahoo!, 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 hudson.tasks;

import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixProject;
import hudson.model.AbstractProject;
import hudson.model.Fingerprint;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.Project;
import hudson.model.Result;
import hudson.tasks.Fingerprinter.FingerprintAction;
import hudson.util.RunList;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.jvnet.hudson.test.HudsonTestCase;

/**
*
* @author dty
*/
public class FingerprinterTest extends HudsonTestCase {
private static final String[] singleContents = {
"abcdef"
};
private static final String[] singleFiles = {
"test.txt"
};
private static final String[] singleContents2 = {
"ghijkl"
};
private static final String[] singleFiles2 = {
"test2.txt"
};
private static final String[] doubleContents = {
"abcdef",
"ghijkl"
};
private static final String[] doubleFiles = {
"test.txt",
"test2.txt"
};

private static final String renamedProject1 = "renamed project 1";
private static final String renamedProject2 = "renamed project 2";

public void testProjectRename() throws Exception {
FreeStyleProject upstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);
FreeStyleProject downstream = createFreeStyleProjectWithFingerprints(singleContents, singleFiles);

FreeStyleBuild upstreamBuild = assertBuildStatusSuccess(upstream.scheduleBuild2(0).get());
FreeStyleBuild downstreamBuild = assertBuildStatusSuccess(downstream.scheduleBuild2(0).get());

String oldUpstreamName = upstream.getName();
String oldDownstreamName = downstream.getName();

// Verify that owner entry in fingerprint record is changed
// after source project is renamed
upstream.renameTo(renamedProject1);
Fingerprinter.FingerprintAction action = upstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
assertNotNull(action);
Collection<Fingerprint> fingerprints = action.getFingerprints().values();
for (Fingerprint f: fingerprints) {
assertTrue(f.getOriginal().is(upstream));
assertTrue(f.getOriginal().getName().equals(renamedProject1));
assertFalse(f.getOriginal().getName().equals(oldUpstreamName));
}

action = downstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
assertNotNull(action);
fingerprints = action.getFingerprints().values();
for (Fingerprint f: fingerprints) {
assertTrue(f.getOriginal().is(upstream));
assertTrue(f.getOriginal().getName().equals(renamedProject1));
assertFalse(f.getOriginal().getName().equals(oldUpstreamName));
}

// Verify that usage entry in fingerprint record is changed after
// sink project is renamed
downstream.renameTo(renamedProject2);
upstream.renameTo(renamedProject1);
action = upstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
assertNotNull(action);
fingerprints = action.getFingerprints().values();
for (Fingerprint f: fingerprints) {
List<String> jobs = f.getJobs();

assertTrue(jobs.contains(renamedProject2));
assertFalse(jobs.contains(oldDownstreamName));
}

action = downstreamBuild.getAction(Fingerprinter.FingerprintAction.class);
assertNotNull(action);
fingerprints = action.getFingerprints().values();
for (Fingerprint f: fingerprints) {
List<String> jobs = f.getJobs();

assertTrue(jobs.contains(renamedProject2));
assertFalse(jobs.contains(oldDownstreamName));
}
}

private FreeStyleProject createFreeStyleProjectWithFingerprints(String[] contents, String[] files) throws IOException, Exception {
FreeStyleProject project = createFreeStyleProject();

addFingerprinterToProject(project, contents, files);

return project;
}

private void addFingerprinterToProject(AbstractProject<?, ?> project, String[] contents, String[] files) throws Exception {
StringBuilder targets = new StringBuilder();
for (int i = 0; i < contents.length; i++) {
if (project instanceof MatrixProject) {
((MatrixProject)project).getBuildersList().add(new Shell("echo " + contents[i] + " > " + files[i]));
} else {
((FreeStyleProject)project).getBuildersList().add(new Shell("echo " + contents[i] + " > " + files[i]));
}

targets.append(files[i]).append(',');
}

project.getPublishersList().add(new Fingerprinter(targets.toString(), false));
}
}

0 comments on commit 8dfa5e1

Please sign in to comment.