From 64b590490f3e22c6c6aa381785c1f7ed10669eee Mon Sep 17 00:00:00 2001 From: Ted Naleid Date: Wed, 13 Jun 2012 21:17:29 -0500 Subject: [PATCH] fixes #8, replacing job config now can have arbitrary remote name or no remote name (previously required "origin/" prefix) --- .../com/entagen/jenkins/JenkinsApi.groovy | 13 +++++++++-- .../entagen/jenkins/JenkinsApiTests.groovy | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy index 045018e9..1eb4bf6e 100644 --- a/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy +++ b/src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy @@ -33,15 +33,24 @@ class JenkinsApi { } void cloneJobForBranch(ConcreteJob missingJob, List templateJobs) { + String missingJobConfig = configForMissingJob(missingJob, templateJobs) + post('createItem', missingJobConfig, [name: missingJob.jobName], ContentType.XML) + } + + String configForMissingJob(ConcreteJob missingJob, List templateJobs) { TemplateJob templateJob = missingJob.templateJob String config = getJobConfig(templateJob.jobName) - config = config.replaceAll(">origin/${templateJob.templateBranchName}<", ">origin/${missingJob.branchName}<") + // should work if there's a remote ("origin/master") or no remote (just "master") + config = config.replaceAll("([>/])(${templateJob.templateBranchName})<") { fullMatch, prefix, branchName -> + return "$prefix${missingJob.branchName}<" + } + // this is in case there are other down-stream jobs that this job calls, we want to be sure we're replacing their names as well templateJobs.each { config = config.replaceAll(it.jobName, it.jobNameForBranch(missingJob.branchName)) } - post('createItem', config, [name: missingJob.jobName], ContentType.XML) + return config } void deleteJob(String jobName) { diff --git a/src/test/groovy/com/entagen/jenkins/JenkinsApiTests.groovy b/src/test/groovy/com/entagen/jenkins/JenkinsApiTests.groovy index 694a3015..48161e97 100644 --- a/src/test/groovy/com/entagen/jenkins/JenkinsApiTests.groovy +++ b/src/test/groovy/com/entagen/jenkins/JenkinsApiTests.groovy @@ -70,6 +70,28 @@ class JenkinsApiTests extends GroovyTestCase { } } + @Test public void testConfigForMissingJob_worksWithRemote() { + JenkinsApi api = new JenkinsApi() + api.metaClass.getJobConfig = { String jobName -> + "origin/master" + } + + TemplateJob templateJob = new TemplateJob(templateBranchName: "master") + ConcreteJob missingJob = new ConcreteJob(branchName: "new/branch", templateJob: templateJob) + assert "origin/new/branch" == api.configForMissingJob(missingJob, []) + } + + @Test public void testConfigForMissingJob_worksWithoutRemote() { + JenkinsApi api = new JenkinsApi() + api.metaClass.getJobConfig = { String jobName -> + "master" + } + + TemplateJob templateJob = new TemplateJob(templateBranchName: "master") + ConcreteJob missingJob = new ConcreteJob(branchName: "new/branch", templateJob: templateJob) + assert "new/branch" == api.configForMissingJob(missingJob, []) + } + public void withJsonResponse(Map toJson, Closure closure) { JSON json = toJson as JSONObject MockFor mockRESTClient = new MockFor(RESTClient)