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

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

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Minor
    • Resolution: Fixed
    • Component/s: core
    • Environment:
      Jenkins 2.164.2
      Folders plugin 6.9
    • Similar Issues:
    • Released As:
      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

            Hide
            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)
            } 
            Show
            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) }
            Hide
            calvinpark Calvin Park added a comment -
            Show
            calvinpark Calvin Park added a comment - OpenedĀ  https://github.com/jenkinsci/jenkins/pull/4684
            Hide
            calvinpark Calvin Park added a comment - - edited

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

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

            Calvin Park Done! Note that you can just change it yourself

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

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

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

              People

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

                Dates

                Created:
                Updated:
                Resolved: