Skip to content
Permalink
Browse files

[JENKINS-41124] Move name mangling support responsibility to Abstract…

…Folder

- Needs more tests... like way more...
- [ ] Need a test that verifies after migration and then a restart that all is still as expected.
- [ ] Need a test that verifies after a migration and then a reload that all is still as expected.
- [ ] Need a test that verifies after a migration and thene a restart and then a reload that all is still as expected.
- [ ] Need a set of all those migration tests using the migration from a 2.0.0 pristine set of items
- [ ] Need a set of all those migration tests using the migration from a 1.x set of items that was migrated to 2.0.0 and then lands on this set
  • Loading branch information...
stephenc committed Jan 18, 2017
1 parent c669899 commit 1d254fad966168ad5dfff97c829fda458c917968
@@ -93,7 +93,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.16</version>
<version>5.17-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -153,7 +153,7 @@ public String getName() {
* @since 0.2-beta-7
*/
public String getEncodedName() {
return NameMangler.apply(getName());
return NameEncoder.encode(getName());
}

/**
@@ -161,34 +161,13 @@ protected MultiBranchProject(ItemGroup parent, String name) {
public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
super.onLoad(parent, name);
init2();
// migrate any non-mangled names
ArrayList<P> items = new ArrayList<>(getItems());
BranchProjectFactory<P, R> projectFactory = getProjectFactory();
for (P item : items) {
if (projectFactory.isProject(item)) {
String itemName = item.getName();
Branch branch = projectFactory.getBranch(item);
String mangledName = branch.getEncodedName();
if (!itemName.equals(mangledName)) {
if (super.getItem(mangledName) == null) {
LOGGER.log(Level.INFO, "Non-mangled name detected for branch {0}. Renaming {1}/{2} to {1}/{3}",
new Object[]{branch.getName(), getFullName(), itemName, mangledName});
item.renameTo(mangledName);
if (item.getDisplayNameOrNull() == null) {
item.setDisplayName(branch.getName());
item.save();
}
} // else will be removed by the orphaned item strategy on next scan
}
}
}
try {
srcDigest = Util.getDigestOf(Items.XSTREAM2.toXML(sources));
} catch (XStreamException e) {
srcDigest = null;
}
try {
facDigest = Util.getDigestOf(Items.XSTREAM2.toXML(projectFactory));
facDigest = Util.getDigestOf(Items.XSTREAM2.toXML(getProjectFactory()));
} catch (XStreamException e) {
facDigest = null;
}
@@ -578,23 +557,16 @@ public P getItem(String name) {
if (name == null) {
return null;
}
if (name.indexOf('%') != -1) {
String decoded = rawDecode(name);
P item = super.getItem(decoded);
if (item != null) {
return item;
}
item = super.getItem(NameMangler.apply(decoded));
P item = super.getItem(name);
if (item == null && name.indexOf('%') != -1) {
String decoded = NameEncoder.decode(name);
item = super.getItem(decoded);
if (item != null) {
return item;
}
// fall through for double decoded call paths
}
P item = super.getItem(name);
if (item != null) {
return item;
// fall through for double decoded call paths // TODO is this necessary
}
return super.getItem(NameMangler.apply(name));
return super.getItem(NameEncoder.encode(name));
}

/**
@@ -607,17 +579,7 @@ public P getItem(String name) {
*/
@CheckForNull
public P getItemByBranchName(@NonNull String branchName) {
BranchProjectFactory<P, R> factory = getProjectFactory();
P item = getItem(NameMangler.apply(branchName));
if (item != null && factory.isProject(item) && branchName.equals(factory.getBranch(item).getName())) {
return item;
}
for (P p: getItems()) {
if (factory.isProject(p) && branchName.equals(factory.getBranch(p).getName())) {
return p;
}
}
return null;
return super.getItem(NameEncoder.encode(branchName));
}

/**
@@ -23,18 +23,24 @@
*/
package jenkins.branch;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.AbstractFolderDescriptor;
import com.cloudbees.hudson.plugins.folder.ChildNameGenerator;
import com.cloudbees.hudson.plugins.folder.FolderIconDescriptor;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.model.TopLevelItemDescriptor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMSourceDescriptor;
import org.jvnet.tiger_types.Types;
@@ -206,4 +212,52 @@ protected MultiBranchProjectDescriptor() {
public boolean isIconConfigurable() {
return false;
}

@SuppressWarnings("unchecked")
@Override
@NonNull
public <I extends TopLevelItem> ChildNameGenerator<AbstractFolder<I>,I> childNameGenerator() {
return (ChildNameGenerator<AbstractFolder<I>, I>)ChildNameGeneratorImpl.INSTANCE;
}

public static class ChildNameGeneratorImpl<P extends Job<P, R> & TopLevelItem,
R extends Run<P, R>> extends ChildNameGenerator<MultiBranchProject<P,R>,P> {

public static final ChildNameGeneratorImpl INSTANCE = new ChildNameGeneratorImpl();

@Override
@CheckForNull
public String itemNameFromItem(@NonNull MultiBranchProject<P,R> parent, @NonNull P item) {
BranchProjectFactory<P, R> factory = parent.getProjectFactory();
if (factory.isProject(item)) {
return NameEncoder.encode(factory.getBranch(item).getName());
} else {
return null;
}
}

@Override
@CheckForNull
public String dirNameFromItem(@NonNull MultiBranchProject<P,R> parent, @NonNull P item) {
BranchProjectFactory<P, R> factory = parent.getProjectFactory();
if (factory.isProject(item)) {
return NameMangler.apply(factory.getBranch(item).getName());
} else {
return null;
}
}

@Override
@NonNull
public String itemNameFromLegacy(@NonNull MultiBranchProject<P, R> parent, @NonNull String legacyDirName) {
return NameEncoder.decode(legacyDirName);
}

@Override
@NonNull
public String dirNameFromLegacy(@NonNull MultiBranchProject<P, R> parent, @NonNull String legacyDirName) {
return NameMangler.apply(NameEncoder.decode(legacyDirName));
}
}

}

0 comments on commit 1d254fa

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