Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-61956

ItemGroupMixin#createProject() does not call Jenkins#checkGoodName()

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved (View Workflow)
    • Minor
    • Resolution: Fixed
    • core
    • Jenkins 2.164.2
      Folders plugin 6.9
    • Jenkins 2.237

    Description

      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.

      Attachments

        Issue Links

          Activity

            calvinpark Calvin Park added a comment -

            Looked into it more and came up with this.

            // Create a folder and its parent folders
            // mkdir -p folder_path
            Folder create_folder_and_parent_folders(String folder_path) {
                // If the folder already exists, return
                Folder folder = Jenkins.instance.getItemByFullName(folder_path)
                if (folder)
                    return folder
            
                String folder_name, parent_path
                def parent_folder
            
                // If the folder is top level, set parent to Jenkins itself
                if (!folder_path.contains("/")) {
                    parent_folder = Jenkins.instance
                    folder_name = folder_path
                }
                // If the folder is a sub-folder, recurse to create/fetch the parent folder
                else {
                    parent_path = StringUtils.substringBeforeLast(folder_path, "/")
                    parent_folder = create_folder_and_parent_folders(parent_path)
                    folder_name = StringUtils.substringAfterLast(folder_path, "/")
                }
            
                // Create the folder in the parent folder
                return parent_folder.createProject(Folder.class, folder_name)
            } 
            calvinpark Calvin Park added a comment - Looked into it more and came up with this. // Create a folder and its parent folders // mkdir -p folder_path Folder create_folder_and_parent_folders( String folder_path) { // If the folder already exists, return Folder folder = Jenkins.instance.getItemByFullName(folder_path) if (folder) return folder String folder_name, parent_path def parent_folder // If the folder is top level, set parent to Jenkins itself if (!folder_path.contains( "/" )) { parent_folder = Jenkins.instance folder_name = folder_path } // If the folder is a sub-folder, recurse to create/fetch the parent folder else { parent_path = StringUtils.substringBeforeLast(folder_path, "/" ) parent_folder = create_folder_and_parent_folders(parent_path) folder_name = StringUtils.substringAfterLast(folder_path, "/" ) } // Create the folder in the parent folder return parent_folder.createProject(Folder.class, folder_name) }
            calvinpark Calvin Park added a comment - OpenedĀ  https://github.com/jenkinsci/jenkins/pull/4684
            calvinpark Calvin Park added a comment - - edited

            danielbeck Please change the title of this ticket to ItemGroupMixin#createProject() does not call Jenkins#checkGoodName()

            calvinpark Calvin Park added a comment - - edited danielbeck Please change the title of this ticket to ItemGroupMixin#createProject() does not call Jenkins#checkGoodName()
            danielbeck Daniel Beck added a comment -

            calvinpark Done! Note that you can just change it yourself

            danielbeck Daniel Beck added a comment - calvinpark Done! Note that you can just change it yourself
            calvinpark Calvin Park added a comment -

            Ah sorry, I didn't realize the name itself was clickable. Thank you!

            calvinpark Calvin Park added a comment - Ah sorry, I didn't realize the name itself was clickable. Thank you!

            People

              Unassigned Unassigned
              calvinpark Calvin Park
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: