-
Bug
-
Resolution: Fixed
-
Minor
-
Jenkins 2.164.2
Folders plugin 6.9
-
-
Jenkins 2.237
String nested = "one/two" // Creates a single folder named "one/two" with / embedded in the name createProject(Folder.class, nested) // Looks for a subfolder named "two" inside a folder named "one" getItemByFullName(nested)
As a result, this code fails
import com.cloudbees.hudson.plugins.folder.Folder String folder = "nested/folders" create_folder_if_missing(folder) create_folder_if_missing(folder) void create_folder_if_missing(String folder) { def item = Jenkins.instance.getItemByFullName(folder) if (item == null) { println(folder + " not found. Creating.") Jenkins.instance.createProject(Folder.class, folder) println("Created " + folder) } else { println(folder + " found. Skipping.") } }
with the result
nested/folders not found. Creating. Created nested/folders nested/folders not found. Creating. java.lang.IllegalArgumentException: nested/folders already exists at hudson.model.Items.verifyItemDoesNotAlreadyExist(Items.java:641)
I think createProject(Folder.class, nested) should change its behavior to look for slashes and create subfolders, rather than allowing a slash in a folder name. At this point it's impossible to search for a folder with a slash in the name using getItemByFullName() function.
- links to
[JENKINS-61956] ItemGroupMixin#createProject() does not call Jenkins#checkGoodName()
Description |
Original:
{code:java} String nested = "one/two" // Creates a single folder named "one/two" with / embedded in the name createProject(Folder.class, nested) // Looks for a subfolder named "two" inside a folder named "one" getItemByFullName(nested) {code} As a result, this code fails {code:java} import com.cloudbees.hudson.plugins.folder.Folder String folder = "nested/folders" create_folder_if_missing(folder) create_folder_if_missing(folder) void create_folder_if_missing(String folder) { def item = Jenkins.instance.getItemByFullName(folder) if (item == null) { println(folder + " not found. Creating.") Jenkins.instance.createProject(Folder.class, folder) println("Created " + folder) } else { println(folder + " found. Skipping.") } } {code} with the result {code:java} nested/folders not found. Creating. Created nested/folders nested/folders not found. Creating. java.lang.IllegalArgumentException: nested/folders already exists at hudson.model.Items.verifyItemDoesNotAlreadyExist(Items.java:641) {code} I think {{createProject(Folder.class, nested)}} should change its behavior to look for slashes and create subfolders, rather than allowing a slash in a folder name. |
New:
{code:java} String nested = "one/two" // Creates a single folder named "one/two" with / embedded in the name createProject(Folder.class, nested) // Looks for a subfolder named "two" inside a folder named "one" getItemByFullName(nested) {code} As a result, this code fails {code:java} import com.cloudbees.hudson.plugins.folder.Folder String folder = "nested/folders" create_folder_if_missing(folder) create_folder_if_missing(folder) void create_folder_if_missing(String folder) { def item = Jenkins.instance.getItemByFullName(folder) if (item == null) { println(folder + " not found. Creating.") Jenkins.instance.createProject(Folder.class, folder) println("Created " + folder) } else { println(folder + " found. Skipping.") } } {code} with the result {code:java} nested/folders not found. Creating. Created nested/folders nested/folders not found. Creating. java.lang.IllegalArgumentException: nested/folders already exists at hudson.model.Items.verifyItemDoesNotAlreadyExist(Items.java:641) {code} I think {{createProject(Folder.class, nested)}} should change its behavior to look for slashes and create subfolders, rather than allowing a slash in a folder name. At this point it's impossible to search for a folder with a slash in the name using {{getItemByFullName()}} function. |
Summary | Original: createProject() and getItemsByFullName() handle nested folders inconsistently | New: ItemGroupMixing#createProject() does not call Jenkins#checkGoodName |
Looks like createProject is missing a call to Jenkins#checkGoodName but that's about it.
While it would be more convenient to have a "createByFullName" that splits at the last /, finds the folder for the first part and inserts the item named the second part, that's a quick utility method.