gvalkov / jenkins-webapi

Python library for interacting with jenkins ci
http://jenkins-webapi.rtfd.org/
Other
16 stars 14 forks source link

Support for jobs in folders #5

Open sschwarzer opened 10 years ago

sschwarzer commented 10 years ago

Hello, thanks for your library. :-)

This issue is for jenkins-webapi version 0.3.0.

When I tried to create a new job inside a folder (created with the Cloudbees Folder Plugin, https://wiki.jenkins-ci.org/display/JENKINS/CloudBees+Folders+Plugin ) with

job2 = server.job_create("TestFolder/TestJob2", job_config)

(where server is a Jenkins instance and job.config is the XML configuration of an already existing job) I got the following traceback:

Traceback (most recent call last):
  File "D2SG/jenkins/jenkins_webapi_test.py", line 27, in <module>
    main()
  File "D2SG/jenkins/jenkins_webapi_test.py", line 23, in main
    job2 = server.job_create("TestFolder/TestJob2", job_config)
  File "/home/schwste/lib/python/jenkins.py", line 436, in job_create
    return Job.create(name, config, self.server)
  File "/home/schwste/lib/python/jenkins.py", line 158, in create
    raise JenkinsError('create "%s" failed' % name)
jenkins.JenkinsError: create "TestFolder/TestJob2" failed

It seems that the library tries to use /createItem?name=TestFolder/TestJob2 as the URL path instead of /job/TestFolder?createItem?name=TestJob2.

Stefan

sschwarzer commented 10 years ago

By the way, adding support for folders (to create the folders for the jobs to begin with) seems to be trivial. If you create a "job", but use the config.xml of an existing folder, the REST API creates a new folder. I assume you'll need the folders plugin (see above) to be installed.

gvalkov commented 9 years ago

Hello, and sorry for the late reply.

The Folder plugin actually breaks a lot of things. For example:

>>> j.jobs
[Job('RealJob'), Job('FolderName')]
>>>  j.jobs[0].buildnumbers
[1, 2, 3]
>>> j.jobs[1].buildnumbers
KeyError: 'builds'  # because it's not really a Job

I'm not too sure about adding special handling for plugins to jenkins-webapi - I'll give it some more though. Please consider the following workaround in the meantime:

>>> j = Jenkins('http://jenkins.sschwarzer.com/FolderName')  
>>> j.jobs  # all jobs in this folder
>>> j.create_job('new')  # creates a job in the folder
KimonHoffmann commented 5 years ago

Well I'm a bit late to the party, but I want to note that the suggested workaround does not work for Jenkins instances that are configured with CSRF protection enabled, because unfortunately it is not possible to request the crumb on the folder level.