Skip to content

Commit

Permalink
[JENKINS-41124] Move name mangling support responsibility to Abstract…
Browse files Browse the repository at this point in the history
…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 1d254fa
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 130 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jenkins/branch/Branch.java
Expand Up @@ -153,7 +153,7 @@ public String getName() {
* @since 0.2-beta-7
*/
public String getEncodedName() {
return NameMangler.apply(getName());
return NameEncoder.encode(getName());
}

/**
Expand Down
54 changes: 8 additions & 46 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/jenkins/branch/MultiBranchProjectDescriptor.java
Expand Up @@ -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;
Expand Down Expand Up @@ -206,4 +212,52 @@ public List<FolderIconDescriptor> getIconDescriptors() {
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.