[JIRA] (JENKINS-61956) ItemGroupMixing#createProject() does not call Jenkins#checkGoodName

0 views
Skip to first unread message

dbeck@cloudbees.com (JIRA)

unread,
Apr 18, 2020, 3:39:03 PM4/18/20
to jenkinsc...@googlegroups.com
Daniel Beck updated an issue
 
Jenkins / Bug JENKINS-61956
ItemGroupMixing#createProject() does not call Jenkins#checkGoodName
Change By: Daniel Beck
Summary: ItemGroupMixing# createProject() and getItemsByFullName() handle nested folders inconsistently does not call Jenkins#checkGoodName
Add Comment Add Comment
 
This message was sent by Atlassian Jira (v7.13.12#713012-sha1:6e07c38)
Atlassian logo

calvinspark@gmail.com (JIRA)

unread,
Apr 18, 2020, 7:05:03 PM4/18/20
to jenkinsc...@googlegroups.com
Calvin Park commented on Bug JENKINS-61956
 
Re: ItemGroupMixing#createProject() does not call Jenkins#checkGoodName

This is what I've got so far - far from a 5 line function. I'd appreciate your comments on how I can improve it

#jinja2: lstrip_blocks: True
#!/usr/bin/env groovy
import com.cloudbees.hudson.plugins.folder.Folder
import org.apache.commons.lang3.StringUtils;

// Create a folder in a parent folder
Folder create_folder(def parent_folder, String folder_name) {
    // Actual create method call
    parent_folder.createProject(Folder.class, folder_name)

    // Below is unfortunately needed because createProject() doesn't return the
    // newly created folder. Therefore need to query the full path again to fetch.
    Jenkins jenkins = Jenkins.instance
    String folder_path
    if (parent_folder == jenkins) {
        folder_path = folder_name
    } else {
        folder_path = parent_folder.fullName + "/" + folder_name
    }

    return jenkins.getItemByFullName(folder_path)
}

// Create a folder and its parent folders
Folder create_folder_recursively(String folder_path) {
    // If the folder already exists, return
    Folder jenkins_folder = Jenkins.instance.getItemByFullName(folder_path)
    if (jenkins_folder)
        return jenkins_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 {
        folder_name = StringUtils.substringAfterLast(folder_path, "/")
        parent_path = StringUtils.substringBeforeLast(folder_path, "/")

        parent_folder = create_folder_recursively(parent_path)
    }

    // Create the folder in the parent folder
    return create_folder(parent_folder, folder_name)
}

// Collect the names of all jobs and credentials
ArrayList<String> names = new ArrayList<String>();
{% for pipeline in pipelines %}
names.add("{{ pipeline.name }}")
{% endfor %}
{% for cred in credential_store %}
names.add("{{ cred.id }}")
{% endfor %}

// Filter to collect the names of all folders
ArrayList<String> folders = new ArrayList<String>();
for (String name : names) {
    if (name.contains("/")) {
        folders.add(StringUtils.substringBeforeLast(name, "/"))
    }
}

// Remove duplicates
folders.unique().sort()

// Create folders and sub-folders
for (String folder_path : folders)
    create_folder_recursively(folder_path)

calvinspark@gmail.com (JIRA)

unread,
Apr 18, 2020, 7:17:02 PM4/18/20
to jenkinsc...@googlegroups.com

and no insults please. I'm trying to improve Jenkins with my limited knowledge.

calvinspark@gmail.com (JIRA)

unread,
Apr 20, 2020, 1:25:03 PM4/20/20
to jenkinsc...@googlegroups.com

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)
} 

calvinspark@gmail.com (JIRA)

unread,
Apr 22, 2020, 12:19:03 AM4/22/20
to jenkinsc...@googlegroups.com

I've tried to contribute to the cloudbees-folder-plugin git repo but mvn hpi:run fails.

cloudbees-folder-plugin master$ mvn hpi:run
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------< org.jenkins-ci.plugins:cloudbees-folder >---------------
[INFO] Building Folders Plugin 6.13-SNAPSHOT
[INFO] --------------------------------[ hpi ]---------------------------------
[INFO] 
[INFO] >>> maven-hpi-plugin:3.10:run (default-cli) > compile @ cloudbees-folder >>>
[...]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ cloudbees-folder ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 45 source files to /Users/calvin/GitRepos/cloudbees-folder-plugin/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] java.nio.file.NoSuchFileException: /Users/calvin/GitRepos/cloudbees-folder-plugin/target/classes/META-INF/annotations/hudson.Extension
[INFO] 1 error
[...]

The github page didn't enable Issues feature so I can't ask for help there https://github.com/jenkinsci/cloudbees-folder-plugin

Any ideas on how to fix the compilation error?

Thank you

calvinspark@gmail.com (JIRA)

unread,
Apr 22, 2020, 3:52:03 PM4/22/20
to jenkinsc...@googlegroups.com

calvinspark@gmail.com (JIRA)

unread,
Apr 24, 2020, 2:20:05 PM4/24/20
to jenkinsc...@googlegroups.com
Calvin Park updated an issue
 
Change By: Calvin Park
Comment:
I've tried to contribute to the {{cloudbees-folder-plugin}} git repo but {{mvn hpi:run}} fails.
{code:java}

cloudbees-folder-plugin master$ mvn hpi:run
[INFO] Scanning for projects...
[INFO]
[INFO] --------------< org.jenkins-ci.plugins:cloudbees-folder >---------------
[INFO] Building Folders Plugin 6.13-SNAPSHOT
[INFO] --------------------------------[ hpi ]---------------------------------
[INFO]
[INFO] >>> maven-hpi-plugin:3.10:run (default-cli) > compile @ cloudbees-folder >>>
[...]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ cloudbees-folder ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 45 source files to /Users/calvin/GitRepos/cloudbees-folder-plugin/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] java.nio.file.NoSuchFileException: /Users/calvin/GitRepos/cloudbees-folder-plugin/target/classes/META-INF/annotations/hudson.Extension
[INFO] 1 error
[...]{code}

The github page didn't enable {{Issues}} feature so I can't ask for help there [https://github.com/jenkinsci/cloudbees-folder-plugin]

Any ideas on how to fix the compilation error?

Thank you

calvinspark@gmail.com (JIRA)

unread,
Apr 29, 2020, 3:34:02 PM4/29/20
to jenkinsc...@googlegroups.com
 
Re: ItemGroupMixing#createProject() does not call Jenkins#checkGoodName

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

Reply all
Reply to author
Forward
0 new messages