Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-19833] support (mostly) absolute path for job name
i.e. job name can be fully qualified but not start with "/"
required for backward compatibility
  • Loading branch information
ndeloof committed Oct 11, 2013
1 parent 201f613 commit 5153c27
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/main/java/hudson/plugins/copyartifact/CopyArtifact.java
Expand Up @@ -456,22 +456,28 @@ private void add(ItemGroup ctx, String projectName, int buildNumber) {
Integer.toString(buildNumber));
}

private Job getProject(ItemGroup ctx, String projectName) {
String[] parts = projectName.split("/");
if (projectName.startsWith("/")) ctx = Jenkins.getInstance();
for (String part : parts) {
/**
* Retrieve root Job identified by this projectPath. For legacy reason, projectPath uses '/' as separator for
* job name and parameters or matrix axe, so can't just use {@link Jenkins#getItemByFullName(String)}.
* As a workaround, we split the path into parts and retrieve the item(group)s up to a Job.
*/
private Job getProject(ItemGroup ctx, String projectPath) {
String[] parts = projectPath.split("/");
if (projectPath.startsWith("/")) ctx = Jenkins.getInstance();
for (int i =0; i<parts.length; i++) {
String part = parts[i];
if (part.length() == 0) continue;
if (part.equals("..")) {
ctx = ((Item) ctx).getParent();
continue;
}
Item i = ctx.getItem(part);
if (i == null) {
// not a relative job name, fall back to "classic" interpretation
return Jenkins.getInstance().getItemByFullName(part, Job.class);
Item item = ctx.getItem(part);
if (item == null && i == 0) {
// not a relative job name, fall back to "classic" interpretation to consider absolute
item = Jenkins.getInstance().getItem(part);
}
if (i instanceof Job) return (Job) i;
ctx = (ItemGroup) i;
if (item instanceof Job) return (Job) item;
ctx = (ItemGroup) item;
}
return null;
}
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/hudson/plugins/copyartifact/CopyArtifactTest.java
Expand Up @@ -988,7 +988,6 @@ public void testRelative() throws Exception {
assertFile(true, "foo.txt", b);
}

@Bug(19833)
public void testAbsolute() throws Exception {
MockFolder folder = jenkins.createProject(MockFolder.class, "folder");
FreeStyleProject other = folder.createProject(FreeStyleProject.class, "foo");
Expand All @@ -1003,6 +1002,25 @@ public void testAbsolute() throws Exception {
assertFile(true, "foo.txt", b);
}

@Bug(19833)
public void testMostlyAbsolute() throws Exception {
MockFolder folder = jenkins.createProject(MockFolder.class, "folder");
FreeStyleProject other = folder.createProject(FreeStyleProject.class, "foo");
other.getBuildersList().add(new ArtifactBuilder());
other.getPublishersList().add(new ArtifactArchiver("**", "", false, false));

MockFolder folder2 = jenkins.createProject(MockFolder.class, "other");
FreeStyleProject p = folder2.createProject(FreeStyleProject.class, "bar");

// "folder/foo" should be resolved as "/folder/foo" even from "/other/bar", for backward compatibility
p.getBuildersList().add(new CopyArtifact("folder/foo", null, new StatusBuildSelector(true), "", "", false, false));

assertBuildStatusSuccess(other.scheduleBuild2(0, new UserCause()).get());
FreeStyleBuild b = p.scheduleBuild2(0, new UserCause()).get();
assertBuildStatusSuccess(b);
assertFile(true, "foo.txt", b);
}

public void testAbsoluteFromFolder() throws Exception {
FreeStyleProject other = jenkins.createProject(FreeStyleProject.class, "foo");
other.getBuildersList().add(new ArtifactBuilder());
Expand Down

2 comments on commit 5153c27

@jglick
Copy link
Member

@jglick jglick commented on 5153c27 Oct 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you not read my comment?

@ndeloof
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I indeed didn't saw it while implementing this fix.
Looks like this can be significantly simplified then

Please sign in to comment.