Skip to content

Commit

Permalink
Merge pull request #18 from jglick/slashy-branches-JENKINS-30744
Browse files Browse the repository at this point in the history
[JENKINS-30744] Introduced Branch.encodedName
  • Loading branch information
jglick committed Nov 6, 2015
2 parents 4d7ce28 + 02dc202 commit 9c4bd01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/main/java/jenkins/branch/Branch.java
Expand Up @@ -23,22 +23,13 @@
*/
package jenkins.branch;

import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.Run;
import hudson.Util;
import hudson.scm.NullSCM;
import hudson.scm.SCM;
import hudson.tasks.BuildWrapper;
import hudson.tasks.Publisher;
import jenkins.scm.api.SCMHead;
import jenkins.scm.impl.NullSCMSource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import jenkins.scm.api.SCMHead;
import jenkins.scm.impl.NullSCMSource;

/**
* A source code branch.
Expand Down Expand Up @@ -99,6 +90,15 @@ public String getName() {
return head.getName();
}

/**
* Gets a branch name suitable for use in paths.
* @return {@link #getName} with URL-unsafe characters escaped
* @since FIXME
*/
public String getEncodedName() {
return Util.rawEncode(getName());
}

/**
* Returns the {@link SCMHead of the branch}
*
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/jenkins/branch/BranchProjectFactory.java
Expand Up @@ -73,8 +73,7 @@ public abstract class BranchProjectFactory<P extends Job<P, R> & TopLevelItem,

/**
* Creates a new branch project.
* {@link Item#getName} must match {@link Branch#getName}.
* (But TBD about slashes; cf. JENKINS-30744.)
* {@link Item#getName} must match {@link Branch#getEncodedName}.
* @param branch the branch.
* @return the new branch project instance.
*/
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/jenkins/branch/MultiBranchProject.java
Expand Up @@ -289,8 +289,9 @@ protected void computeChildren(final ChildObserver<P> observer, final TaskListen
@Override
public void observe(@NonNull SCMHead head, @NonNull SCMRevision revision) {
Branch branch = newBranch(source, head);
String name = branch.getName();
P project = observer.shouldUpdate(name);
String rawName = branch.getName();
String encodedName = branch.getEncodedName();
P project = observer.shouldUpdate(encodedName);
if (project != null) {
if (!_factory.isProject(project)) {
listener.getLogger().println("Detected unsupported subitem " + project + ", skipping");
Expand Down Expand Up @@ -319,18 +320,25 @@ public void observe(@NonNull SCMHead head, @NonNull SCMRevision revision) {
try {
project.save();
} catch (IOException e) {
e.printStackTrace(listener.error("Could not save changes to " + name));
e.printStackTrace(listener.error("Could not save changes to " + rawName));
}
}
return;
}
if (!observer.mayCreate(name)) {
listener.getLogger().println("Ignoring duplicate branch project " + name);
if (!observer.mayCreate(encodedName)) {
listener.getLogger().println("Ignoring duplicate branch project " + rawName);
return;
}
project = _factory.newInstance(branch);
if (!project.getName().equals(branch.getName())) {
throw new IllegalStateException("Created project name did not match branch name");
if (!project.getName().equals(encodedName)) {
throw new IllegalStateException("Name of created project " + project + " did not match expected " + encodedName);
}
if (!rawName.equals(encodedName) && project.getDisplayName().equals(encodedName)) {
try {
project.setDisplayName(rawName);
} catch (IOException e) {
e.printStackTrace(listener.error("Could not save changes to " + rawName));
}
}
_factory.decorate(project);
observer.created(project);
Expand Down Expand Up @@ -454,7 +462,7 @@ public String getUrlChildPrefix() {
@Override
@NonNull
public File getRootDirFor(P child) {
File dir = new File(getJobsDir(), /* TODO why? cf. JENKINS-30744 */Util.rawEncode(child.getName()));
File dir = super.getRootDirFor(child);
if (!dir.isDirectory() && !dir.mkdirs()) { // TODO is this really necessary?
LOGGER.log(Level.WARNING, "Could not create directory {0}", dir);
}
Expand Down

0 comments on commit 9c4bd01

Please sign in to comment.