Skip to content

Commit

Permalink
allow .. path segments in job, folder and view names
Browse files Browse the repository at this point in the history
[FIXES JENKINS-40732]
  • Loading branch information
daspilker committed May 8, 2017
1 parent 8569f5e commit 25ed646
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/Home.md
Expand Up @@ -43,6 +43,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
([JENKINS-43991](https://issues.jenkins-ci.org/browse/JENKINS-43991))
* Improved error handling and fail if an item or view could not be created or updated
([JENKINS-43991](https://issues.jenkins-ci.org/browse/JENKINS-43991))
* Allow `..` path segments in job, folder and view names.
([JENKINS-40732](https://issues.jenkins-ci.org/browse/JENKINS-40732))
* Added a switch to the command line runner to put the script's directory on the classpath, see
[User-Power-Moves](User-Power-Moves#run-a-dsl-script-locally)
([JENKINS-42299](https://issues.jenkins-ci.org/browse/JENKINS-42299))
Expand Down
Expand Up @@ -4,6 +4,8 @@
import hudson.model.ItemGroup;
import jenkins.model.Jenkins;

import java.net.URI;

/**
* A JobLookupStrategy encapsulates where a seed job will look for existing jobs
* and where it will create new jobs. This matters when you use relative names in
Expand All @@ -14,11 +16,6 @@ public enum LookupStrategy {
* Using this naming strategy jobs with relative path names are absolute names.
*/
JENKINS_ROOT("Jenkins Root") {
@Override
public <T extends Item> T getItem(Item seedJob, String path, Class<T> type) {
return Jenkins.getInstance().getItemByFullName(path, type);
}

@Override
protected ItemGroup getContext(Item seedJob) {
return Jenkins.getInstance();
Expand All @@ -30,12 +27,6 @@ protected ItemGroup getContext(Item seedJob) {
* to the seed job's parent folder.
*/
SEED_JOB("Seed Job") {
@Override
public <T extends Item> T getItem(Item seedJob, String path, Class<T> type) {
String fullName = path.startsWith("/") ? path : seedJob.getParent().getFullName() + "/" + path;
return Jenkins.getInstance().getItemByFullName(fullName, type);
}

@Override
protected ItemGroup getContext(Item seedJob) {
return seedJob.getParent();
Expand All @@ -55,7 +46,10 @@ protected ItemGroup getContext(Item seedJob) {
* @param <T> the type of the item
* @return the item for the given path
*/
public abstract <T extends Item> T getItem(Item seedJob, String path, Class<T> type);
public <T extends Item> T getItem(Item seedJob, String path, Class<T> type) {
String fullName = path.startsWith("/") ? path : getContext(seedJob).getFullName() + "/" + path;
return Jenkins.getInstance().getItemByFullName(normalizePath(fullName), type);
}

/**
* Get the context in which new items should be created for the given seed job.
Expand Down Expand Up @@ -85,8 +79,7 @@ public ItemGroup getParent(Item seedJob, String path) {

int i = absolutePath.lastIndexOf('/');
if (i > -1) {
Item item = jenkins.getItemByFullName(absolutePath.substring(0, i));
return item instanceof ItemGroup ? (ItemGroup) item : null;
return getItemGroup(absolutePath.substring(0, i));
} else {
return jenkins;
}
Expand All @@ -97,4 +90,18 @@ public String getDisplayName() {
}

private final String displayName;

private static ItemGroup getItemGroup(String path) {
Jenkins instance = Jenkins.getInstance();
String normalizedPath = normalizePath(path);
if (normalizedPath.isEmpty() || normalizedPath.equals("/")) {
return instance;
}
Item item = instance.getItemByFullName(normalizedPath);
return item instanceof ItemGroup ? (ItemGroup) item : null;
}

private static String normalizePath(String path) {
return URI.create(path).normalize().getPath();
}
}
Expand Up @@ -60,6 +60,19 @@ class LookupStrategySpec extends Specification {
SEED_JOB | 'folder/job'
}

def 'getItem not normalized'(LookupStrategy lookupStrategy, String expectedFullName) {
when:
Item result = lookupStrategy.getItem(seedJob, '../job', Item)

then:
result?.fullName == expectedFullName

where:
lookupStrategy | expectedFullName
JENKINS_ROOT | null
SEED_JOB | 'job'
}

def 'getContext'(LookupStrategy lookupStrategy, String expectedFullName) {
when:
ItemGroup result = lookupStrategy.getContext(seedJob)
Expand Down Expand Up @@ -92,4 +105,21 @@ class LookupStrategySpec extends Specification {
JENKINS_ROOT | '/folder/foo' | 'folder'
SEED_JOB | '/folder/foo' | 'folder'
}

def 'getParent not normalized'(LookupStrategy lookupStrategy, String path, String expectedFullName) {
when:
ItemGroup result = lookupStrategy.getParent(seedJob, path)

then:
result?.fullName == expectedFullName

where:
lookupStrategy | path | expectedFullName
JENKINS_ROOT | '../folder/job' | null
SEED_JOB | '../folder/job' | 'folder'
JENKINS_ROOT | '/folder/../job' | ''
SEED_JOB | '/folder/../job' | ''
JENKINS_ROOT | 'folder/../job' | ''
SEED_JOB | 'folder/../job' | 'folder'
}
}

0 comments on commit 25ed646

Please sign in to comment.