Skip to content

Commit

Permalink
fixes entagen#8, replacing job config now can have arbitrary remote n…
Browse files Browse the repository at this point in the history
…ame or no remote name (previously required "origin/" prefix)
  • Loading branch information
tednaleid authored and Jens Hausherr committed Jun 15, 2012
1 parent 8cc60cf commit 64b5904
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/groovy/com/entagen/jenkins/JenkinsApi.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,24 @@ class JenkinsApi {
}

void cloneJobForBranch(ConcreteJob missingJob, List<TemplateJob> templateJobs) {
String missingJobConfig = configForMissingJob(missingJob, templateJobs)
post('createItem', missingJobConfig, [name: missingJob.jobName], ContentType.XML)
}

String configForMissingJob(ConcreteJob missingJob, List<TemplateJob> 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) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/groovy/com/entagen/jenkins/JenkinsApiTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ class JenkinsApiTests extends GroovyTestCase {
}
}

@Test public void testConfigForMissingJob_worksWithRemote() {
JenkinsApi api = new JenkinsApi()
api.metaClass.getJobConfig = { String jobName ->
"<name>origin/master</name>"
}

TemplateJob templateJob = new TemplateJob(templateBranchName: "master")
ConcreteJob missingJob = new ConcreteJob(branchName: "new/branch", templateJob: templateJob)
assert "<name>origin/new/branch</name>" == api.configForMissingJob(missingJob, [])
}

@Test public void testConfigForMissingJob_worksWithoutRemote() {
JenkinsApi api = new JenkinsApi()
api.metaClass.getJobConfig = { String jobName ->
"<name>master</name>"
}

TemplateJob templateJob = new TemplateJob(templateBranchName: "master")
ConcreteJob missingJob = new ConcreteJob(branchName: "new/branch", templateJob: templateJob)
assert "<name>new/branch</name>" == api.configForMissingJob(missingJob, [])
}

public void withJsonResponse(Map toJson, Closure closure) {
JSON json = toJson as JSONObject
MockFor mockRESTClient = new MockFor(RESTClient)
Expand Down

0 comments on commit 64b5904

Please sign in to comment.