Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to update jobs, if they exist. #23

Merged
merged 2 commits into from Oct 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions autojenkins/jobs.py
Expand Up @@ -249,14 +249,14 @@ def create(self, jobname, config_file, **context):
headers={'Content-Type': 'application/xml'}
)

def create_copy(self, jobname, template_job, enable=True, **context):
def create_copy(self, jobname, template_job, enable=True, _force=False, **context):
"""
Create a job from a template job.
"""
if not self.job_exists(template_job):
raise JobInexistent("Template job '%s' doesn't exists" % template_job)

if self.job_exists(jobname):
if not _force and self.job_exists(jobname):
raise JobExists("Another job with the name '%s'already exists"
% jobname)

Expand All @@ -273,10 +273,13 @@ def create_copy(self, jobname, template_job, enable=True, **context):
config = config.replace('<disabled>true</disabled>',
'<disabled>false</disabled>')

return self._build_post(NEWJOB,
data=config,
params={'name': jobname},
headers={'Content-Type': 'application/xml'})
if _force:
return self.set_config_xml(jobname, config)
else:
return self._build_post(NEWJOB,
data=config,
params={'name': jobname},
headers={'Content-Type': 'application/xml'})

def transfer(self, jobname, to_server):
"""
Expand Down
15 changes: 15 additions & 0 deletions autojenkins/tests/test_unit_jobs.py
Expand Up @@ -182,6 +182,21 @@ def test_create_copy(self, job_exists, requests):
proxies={},
verify=True)

@patch('autojenkins.jobs.Jenkins.job_exists')
def test_create_copy_forced(self, job_exists, requests):
job_exists.side_effect = side_effect_job_exists
requests.get.return_value = mock_response('create_copy.txt')
requests.post.return_value = mock_response()
self.jenkins.create_copy('name', 'template', _force=True, value='2')
CFG = "<value>2</value><disabled>false</disabled>"
requests.post.assert_called_once_with(
'http://jenkins/job/name/config.xml',
headers={'Content-Type': 'application/xml'},
data=CFG,
auth=None,
proxies={},
verify=True)

def test_transfer(self, requests):
requests.get.return_value = mock_response('transfer.txt')
requests.post.return_value = mock_response()
Expand Down