jenkinsapi icon indicating copy to clipboard operation
jenkinsapi copied to clipboard

Add support for folders

Open sodul opened this issue 4 years ago • 11 comments

ISSUE TYPE
  • Feature Idea
Jenkinsapi 0.3.11
Jenkins 2.235.5
[email protected]
SUMMARY

When creating a folder with create_job() it works fine, but then since this is not actually a job an exception is raised when jenkinsapi tries to get the job object back. It would need to be able to make the distinction between folders and jobs.

Next issue is that to create a folder, or a job that is inside the folder the API url should be changed.

For example for job foo/bar the url that jenkinsapi tries to use is http://host/createItem?name=foo%2Fbar but the url should be http://host/job/foo/createItem?name=bar. If the job is foo/bar/baz then the url would be http://host/job/foo/job/bar/createItem?name=baz.

EXPECTED RESULTS

The folders and jobs in subfolders should be created without a glitch.

ACTUAL RESULTS

We get a JenkinsAPIException: Operation failed. error, and the job is not created.

USEFUL INFORMATION

To reproduce install the Cloudbees Folder plugin: https://plugins.jenkins.io/cloudbees-folder/

Create folders and jobs under the folders.

This is a workaround to massage the baseurl:

        if '/' in name:
            folder, name = name.rsplit('/', 1)
            server = copy.deepcopy(server)
            folder_path = '/'.join([f'job/{x}' for x in folder.split('/')])
            server.baseurl = server.baseurl + '/' + folder_path

sodul avatar Sep 10 '20 00:09 sodul

I am happy to accept pull request for this.

lechat avatar Sep 12 '20 11:09 lechat

Bummer, we're also using folders, meaning I can't fetch the job status of the actual jobs inside the folder 😢

BramDriesen avatar Dec 11 '20 20:12 BramDriesen

@BramDriesen try my workaround, it works well enough for us.

sodul avatar Dec 11 '20 22:12 sodul

@sodul Yes, your workaround works fine :) but an native supported function would have been better ;)

BramDriesen avatar Dec 12 '20 17:12 BramDriesen

I took a look at the code and it would take a bit of effort to add full support. Most importantly there are several design decisions that would need to be addressed:

  • I believe '/' is not going to be allowed in names, but we should make sure that nobody would have such a job.
  • The 'jobs' are currently tracked as a flat dictionary, if we want to add them, it means that we either need to recursively fill the structure proactively, or maybe have folders as a separate group of items. The users will have to perform the recursion. I favor this approach as it is closer to the API and would scale better, as well as make it easier to scope changes to a specific folder.

sodul avatar Dec 12 '20 17:12 sodul

Nevertheless nobody submitted pull request to add support for this. Why not?

lechat avatar Dec 13 '20 01:12 lechat

I'm looking into it, but no promises. I have not worked with python 2 in several years so I'll have to get used to make things backward compatible if I do implement something.

Would you know the plans to deprecate 2.7 support so the codebase can be cleaned up?

sodul avatar Dec 13 '20 02:12 sodul

@sodul maybe you can try https://api4jenkins.readthedocs.io/en/latest/user/example.html#folder

joelee2012 avatar Jan 07 '21 07:01 joelee2012

After submitting 2 PRs 3 weeks ago with no response I am switching to the python-jenkins library instead.

sodul avatar Jan 07 '21 17:01 sodul

@sodul Care to send a link? 😄

BramDriesen avatar Jan 10 '21 19:01 BramDriesen

@BramDriesen: https://pypi.org/project/python-jenkins/

sodul avatar Jan 11 '21 07:01 sodul