Permalink
Browse files

jobgen uses template composition and changing create to sync task

  • Loading branch information...
1 parent 3fcd9e4 commit 9b7fcb09839d6b52bc4b644f7ef78b4ec65a1d0f @narkisr committed Jan 4, 2013
View
2 src/main/groovy/com/narkisr/jenkins/jobs/ClasspathReader.groovy
@@ -2,6 +2,6 @@ package com.narkisr.jenkins.jobs
class ClasspathReader {
def read(name) {
- this.class.getClassLoader().getResourceAsStream(name)
+ this.class.classLoader.getResourceAsStream(name)
}
}
View
4 src/main/groovy/com/narkisr/jenkins/jobs/JobGen.groovy
@@ -4,12 +4,12 @@ import groovy.xml.StreamingMarkupBuilder
class JobGen {
- def template
def mappings
def process(input) {
- def xml = new XmlSlurper().parse(template)
def model = new JobParser().parse(input)
+ def template = new TemplateComposition().compose(model)
+ def xml = new XmlSlurper().parse(template)
mappings = new Mappings(model)
def result = applySlave(model,applyStep(model, applyScm(model,xml)))
def name = model.name
View
28 src/main/groovy/com/narkisr/jenkins/plugin/JenkinsPlugin.groovy
@@ -5,39 +5,47 @@ import org.kar.hudson.api.JobJSONApi
import static java.net.HttpURLConnection.HTTP_OK
import com.narkisr.jenkins.jobs.*
import org.gradle.api.*
+import static groovy.io.FileType.*
@Mixin(ClasspathReader)
class JenkinsPlugin implements Plugin<Project>{
- def rootUrl = 'http://192.168.1.15:8080/'
void apply(Project project) {
- createJob(project)
+ project.extensions.create("jenkins",JenkinsPluginExtension )
+ syncJobs(project)
deleteJob(project)
listJobs(project)
}
- def createJob(project) {
- project.task('createJob') << {
- def job = new File("src/main/groovy/jobs/${project.job}").newInputStream()
- def jobName = new JobGen(template : read('gradle_base.xml')).process(job)
- def api = new JobJSONApi()
- assert HTTP_OK == api.createJob(rootUrl, new File('build/liquibase.xml').text, jobName)
+ def syncJobs(project) {
+ project.task('syncJobs') {
+ description = 'Sync all defined jobs into jenkins'
+ doLast {
+ def parent = "src/main/groovy/${project.group.replaceAll('\\.','/')}/jobs"
+ new File(parent).eachFileMatch FILES, ~/.*\.groovy/, { job ->
+ project.buildDir.mkdirs()
+ def jobName = new JobGen().process(job.newInputStream())
+ println "synching ${jobName} into ${project.jenkins.url} from ${job.name} "
+ def api = new JobJSONApi()
+ assert HTTP_OK == api.createJob(project.jenkins.url, new File("build/${jobName}.xml").text, jobName)
+ }
+ }
}
}
def deleteJob(project) {
project.task('deleteJob') << {
def jobApi = new JobJSONApi()
def model = new JobParser().parse(job)
- assert HTTP_OK == jobApi.deleteJob("${rootUrl}/job/${model.name}/")
+ assert HTTP_OK == jobApi.deleteJob("${project.jenkins.url}/job/${model.name}/")
}
}
def listJobs(project) {
project.task('listjobs') << {
def api = new JSONApi()
- def apiResult = api.inspectApi(rootUrl)
+ def apiResult = api.inspectApi(project.jenkins.url)
apiResult.jobs.each {
println it
}
View
3 src/main/groovy/com/narkisr/jenkins/plugin/JenkinsPluginExtension.groovy
@@ -0,0 +1,3 @@
+class JenkinsPluginExtension {
+ def url = ""
+}
View
7 src/test/groovy/com/narkisr/jenkins/jobs/JobTest.groovy
@@ -27,9 +27,8 @@ class JobTest {
@Test
def void defaultGeneration() {
- def base = new TemplateComposition().compose([scm:[:]])
def job = read(jobFile)
- def gen = new JobGen(template : base)
+ def gen = new JobGen()
def name = gen.process(job)
assertEquals(name,'liquibase')
def xml = new XmlSlurper().parse(new File('build/liquibase.xml'))
@@ -38,13 +37,11 @@ class JobTest {
@Test
def void mercurialGeneration() {
- def base = new TemplateComposition().compose([scm:[type:'mercurial']])
def job = read('Mercurial.groovy')
- def gen = new JobGen(template : base)
+ def gen = new JobGen()
def name = gen.process(job)
assertEquals(name,'mercurial-based')
def xml = new XmlSlurper().parse(new File('build/mercurial-based.xml'))
- println xml.scm.source.class
assertEquals('http://mercurial.com',xml.scm.source.text())
assertEquals('foo',xml.scm.branch.text())
}

0 comments on commit 9b7fcb0

Please sign in to comment.