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 functionality to use freestyle templates for matrix jobs #595

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import javaposse.jobdsl.dsl.helpers.toplevel.NotificationContext
import javaposse.jobdsl.dsl.helpers.toplevel.ThrottleConcurrentBuildsContext
import javaposse.jobdsl.dsl.helpers.triggers.TriggerContext
import javaposse.jobdsl.dsl.helpers.wrapper.WrapperContext
import javaposse.jobdsl.dsl.coercion.Template

import static javaposse.jobdsl.dsl.Preconditions.checkArgument
import static javaposse.jobdsl.dsl.Preconditions.checkNotNull
Expand Down Expand Up @@ -592,7 +593,11 @@ abstract class Job extends Item {
Node emptyTemplateNode = executeEmptyTemplate()

if (emptyTemplateNode.name() != templateNode.name()) {
throw new JobTypeMismatchException(name, templateName)
//maybe we can coerce the template into our required type
templateNode = new Template().coerce(templateNode, emptyTemplateNode.name())
if (emptyTemplateNode.name() != templateNode.name()) {
throw new JobTypeMismatchException(name, templateName)
}
}

templateNode
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package javaposse.jobdsl.dsl.coercion

/**
*
*/
abstract class AbstractCoercer {

abstract coerce(Node original)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package javaposse.jobdsl.dsl.coercion

/**
* class to coerce templates from one job type to another
* coercion helper classes need to be called javaposse.jobdsl.dsl.coercion.<from>.To
* and inherit from {@link AbstractCoercer}
*
* hyphens need to be swallowed
*
* e.g. javaposse.jobdsl.dsl.coercion.project.MatrixProject
* for the class to coerce project to matrix-project
*/
class Template {

final Node coerce(Node existing, String to) {

String toCamel = to.capitalize().replaceAll(/-\w/) { it[1].toUpperCase() }
String fromClean = existing.name().replaceAll(/-/) { '' }

try {
String coercer = "javaposse.jobdsl.dsl.coercion.${fromClean}.${toCamel}"
AbstractCoercer instance = this.class.classLoader.loadClass(coercer)?.newInstance()
instance.coerce(existing)
} catch (ClassNotFoundException ex) {
//there is no class to coerce from to to
existing
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package javaposse.jobdsl.dsl.coercion.matrixproject

import javaposse.jobdsl.dsl.coercion.AbstractCoercer

/**
* convert a matrix-project to a project
*/
class Project extends AbstractCoercer {

@Override
Node coerce(Node existing) {

existing.name = 'project'
existing.attributes().remove('plugin')

existing.findAll { node ->
node.name() =~ /(axes|executionStrategy|childCustomWorkspace|combinationFilter)/ }.each {
existing.remove(it)
}

existing
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package javaposse.jobdsl.dsl.coercion.project

import javaposse.jobdsl.dsl.coercion.AbstractCoercer

/**
* convert a project to a matrixproject
*/
class MatrixProject extends AbstractCoercer {

@Override
@SuppressWarnings('UnusedObject')
Node coerce(Node existing) {
//Node coerced = existing.clone()

existing.name = 'matrix-project'
existing.attributes().remove('plugin')

new Node(existing, 'axes')

Node es = new Node(existing, 'executionStrategy', [class: 'hudson.matrix.DefaultMatrixExecutionStrategyImpl'])
Node rs = new Node(es, 'runSequentially' )
rs.setValue('false')

existing
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package javaposse.jobdsl.dsl

import javaposse.jobdsl.dsl.jobs.FreeStyleJob
import javaposse.jobdsl.dsl.jobs.MatrixJob
import org.custommonkey.xmlunit.XMLUnit
import spock.lang.Specification

class CoerceSpec extends Specification {
private final resourcesDir = new File(getClass().getResource('/simple.dsl').toURI()).parentFile
private final JobManagement jm = new FileJobManagement(resourcesDir)

def setup() {
XMLUnit.setIgnoreWhitespace(true)
}

def 'Matrix job coerces to FreeStyleJob'() {
setup:
JobManagement jm = new FileJobManagement(resourcesDir)
FreeStyleJob job = new FreeStyleJob(jm)

when:
job.using('matrix')
String project = job.xml

then:
project.contains('<project>')
! project.contains('<matrix-project>')
! project.contains('<axes>')
! project.contains('executionStrategy')
! project.contains('<childCustomWorkspace>')
! project.contains('<combinationFilter>')
}

def 'FreeStyle job coerces to MatrixJob'() {
setup:
JobManagement jm = new FileJobManagement(resourcesDir)
MatrixJob job = new MatrixJob(jm)

when:
job.using('job')
String project = job.xml

then:
project.contains('<matrix-project>')
! project.contains('<project>')
project.contains('<axes>')
project.contains('executionStrategy')
}
}
61 changes: 61 additions & 0 deletions job-dsl-core/src/test/resources/matrix.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<matrix-project plugin="[email protected]">
<actions/>
<description>Build and test the app.</description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers/>
<concurrentBuild>false</concurrentBuild>
<customWorkspace>matrix</customWorkspace>
<axes>
<hudson.matrix.TextAxis>
<name>ddd</name>
<values>
<string>a</string>
<string>b</string>
<string>c</string>
</values>
</hudson.matrix.TextAxis>
<hudson.matrix.TextAxis>
<name>eee</name>
<values>
<string>a</string>
<string>b</string>
<string>c</string>
</values>
</hudson.matrix.TextAxis>
</axes>
<combinationFilter>eee=='a'||ddd=='c'</combinationFilter>
<builders>
<hudson.tasks.Shell>
<command>ps</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>env |sort</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>ps</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>env |sort</command>
</hudson.tasks.Shell>
</builders>
<publishers/>
<buildWrappers/>
<executionStrategy class="hudson.matrix.DefaultMatrixExecutionStrategyImpl">
<runSequentially>true</runSequentially>
<touchStoneCombinationFilter>eee=='b'</touchStoneCombinationFilter>
<touchStoneResultCondition>
<name>SUCCESS</name>
<ordinal>0</ordinal>
<color>BLUE</color>
<completeBuild>true</completeBuild>
</touchStoneResultCondition>
</executionStrategy>
<childCustomWorkspace>matrix2</childCustomWorkspace>
</matrix-project>