Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -11,6 +11,10 @@ class MultiJobStepContext extends StepContext {
'SUCCESSFUL', 'UNSTABLE', 'COMPLETED', 'FAILURE', 'ALWAYS'
]

private static final List<String> VALID_EXECUTION_TYPES = [
'PARALLEL', 'SEQUENTIALLY'
]

MultiJobStepContext(JobManagement jobManagement, Item item) {
super(jobManagement, item)
}
Expand All @@ -19,14 +23,14 @@ class MultiJobStepContext extends StepContext {
* Adds a MultiJob phase.
*/
void phase(@DslContext(PhaseContext) Closure phaseContext) {
phase(null, 'SUCCESSFUL', phaseContext)
phase(null, 'SUCCESSFUL', 'PARALLEL', phaseContext)
}

/**
* Adds a MultiJob phase.
*/
void phase(String phaseName, @DslContext(PhaseContext) Closure phaseContext = null) {
phase(phaseName, 'SUCCESSFUL', phaseContext)
phase(phaseName, 'SUCCESSFUL', 'PARALLEL', phaseContext)
}

/**
Expand All @@ -35,20 +39,29 @@ class MultiJobStepContext extends StepContext {
* {@code continuationCondition} must be one of {@code 'SUCCESSFUL'}, {@code 'UNSTABLE'}, {@code 'COMPLETED'} or
* {@code 'FAILURE'}. When version 1.16 or later of the MultiJob plugin is installed, {@code continuationCondition}
* can also be set to {@code 'ALWAYS'}.
* {@code executionType} must be one of {@code 'PARALLEL'} or {@code 'SEQUENTIALLY'}
*/
void phase(String name, String continuationCondition, @DslContext(PhaseContext) Closure phaseClosure) {
PhaseContext phaseContext = new PhaseContext(jobManagement, item, name, continuationCondition)
void phase(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break compatibility as it changes a signature. Leave the signature as it is, just adding a new method to PhaseContext is sufficient.

String name, String continuationCondition, String executionType,
@DslContext(PhaseContext) Closure phaseClosure
) {
PhaseContext phaseContext = new PhaseContext(jobManagement, item, name, continuationCondition, executionType)
ContextHelper.executeInContext(phaseClosure, phaseContext)

Preconditions.checkNotNullOrEmpty(phaseContext.phaseName, 'A phase needs a name')
Preconditions.checkArgument(
VALID_CONTINUATION_CONDITIONS.contains(phaseContext.continuationCondition),
"Continuation Condition needs to be one of these values: ${VALID_CONTINUATION_CONDITIONS.join(', ')}"
)
Preconditions.checkArgument(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this check to the executionType method in PhaseContext.

VALID_EXECUTION_TYPES.contains(phaseContext.executionType),
"Execution Type needs to be one of these values: ${VALID_EXECUTION_TYPES.join(', ')}"
)

stepNodes << new NodeBuilder().'com.tikal.jenkins.plugins.multijob.MultiJobBuilder' {
phaseName phaseContext.phaseName
delegate.continuationCondition(phaseContext.continuationCondition)
delegate.executionType(phaseContext.executionType)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be wrapped in an if block testing jobManagement.isMinimumPluginVersionInstalled(...) so that the new element only gets generated if the minimum required plugin version is installed.

phaseJobs {
phaseContext.jobsInPhase.each { PhaseJobContext jobInPhase ->
Node phaseJobNode = 'com.tikal.jenkins.plugins.multijob.PhaseJobsConfig' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class PhaseContext extends AbstractContext {

String phaseName
String continuationCondition
String executionType

List<PhaseJobContext> jobsInPhase = []

PhaseContext(JobManagement jobManagement, Item item, String phaseName, String continuationCondition) {
PhaseContext(
JobManagement jobManagement, Item item, String phaseName, String continuationCondition,
String executionType
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add another constructor parameter here. Set the field to the default value.

) {
super(jobManagement)
this.item = item
this.phaseName = phaseName
this.continuationCondition = continuationCondition
this.executionType = executionType
}

/**
Expand All @@ -35,6 +40,13 @@ class PhaseContext extends AbstractContext {
this.continuationCondition = continuationCondition
}

/**
* Defines how to run the whole MultiJob phase.
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a @since tag which points to the next version.

void executionType(String executionType) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature need version 1.22 of the MultiJob plugin. Add a @RequiresPlugin annotation.

this.executionType = executionType
}

/**
* Adds a job to the phase.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class MultiJobStepContextSpec extends Specification {
then:
with(context.stepNodes[0]) {
name() == 'com.tikal.jenkins.plugins.multijob.MultiJobBuilder'
children().size() == 3
children().size() == 4
phaseName[0].value() == 'First'
continuationCondition[0].value() == 'SUCCESSFUL'
executionType[0].value() == 'PARALLEL'
phaseJobs[0].value().empty
}

Expand All @@ -32,9 +33,10 @@ class MultiJobStepContextSpec extends Specification {
then:
with(context.stepNodes[1]) {
name() == 'com.tikal.jenkins.plugins.multijob.MultiJobBuilder'
children().size() == 3
children().size() == 4
phaseName[0].value() == 'Second'
continuationCondition[0].value() == 'SUCCESSFUL'
executionType[0].value() == 'PARALLEL'
phaseJobs[0].children().size() == 1
with(phaseJobs[0].'com.tikal.jenkins.plugins.multijob.PhaseJobsConfig'[0]) {
children().size() == 7
Expand All @@ -60,9 +62,10 @@ class MultiJobStepContextSpec extends Specification {
then:
with(context.stepNodes[0]) {
name() == 'com.tikal.jenkins.plugins.multijob.MultiJobBuilder'
children().size() == 3
children().size() == 4
phaseName[0].value() == 'Third'
continuationCondition[0].value() == 'SUCCESSFUL'
executionType[0].value() == 'PARALLEL'
phaseJobs[0].children().size() == 3
phaseJobs[0].'com.tikal.jenkins.plugins.multijob.PhaseJobsConfig'[0].jobName[0].value() == 'JobA'
phaseJobs[0].'com.tikal.jenkins.plugins.multijob.PhaseJobsConfig'[1].jobName[0].value() == 'JobB'
Expand Down Expand Up @@ -100,9 +103,10 @@ class MultiJobStepContextSpec extends Specification {

then:
with(context.stepNodes[0]) {
children().size() == 3
children().size() == 4
phaseName[0].value() == 'Fourth'
continuationCondition[0].value() == 'SUCCESSFUL'
executionType[0].value() == 'PARALLEL'
phaseJobs[0].children().size() == 1
with(phaseJobs[0].'com.tikal.jenkins.plugins.multijob.PhaseJobsConfig'[0]) {
children().size() == 8
Expand Down Expand Up @@ -188,9 +192,10 @@ class MultiJobStepContextSpec extends Specification {
then:
with(context.stepNodes[0]) {
name() == 'com.tikal.jenkins.plugins.multijob.MultiJobBuilder'
children().size() == 3
children().size() == 4
phaseName[0].value() == 'Second'
continuationCondition[0].value() == 'SUCCESSFUL'
executionType[0].value() == 'PARALLEL'
phaseJobs[0].children().size() == 1
with(phaseJobs[0].'com.tikal.jenkins.plugins.multijob.PhaseJobsConfig'[0]) {
children().size() == 7
Expand Down Expand Up @@ -220,7 +225,16 @@ class MultiJobStepContextSpec extends Specification {

def 'call phase with unsupported condition'() {
when:
context.phase('test', 'FOO') {
context.phase('test', 'FOO', 'PARALLEL') {
}

then:
thrown(DslScriptException)
}

def 'call phase with unsupported execution type'() {
when:
context.phase('test', 'SUCCESSFUL', 'BAR') {
}

then:
Expand All @@ -232,22 +246,45 @@ class MultiJobStepContextSpec extends Specification {
jobManagement.isMinimumPluginVersionInstalled('jenkins-multijob-plugin', '1.16') >> true

when:
context.phase('test', condition) {
context.phase('test', condition, 'PARALLEL') {
}

then:
with(context.stepNodes[0]) {
name() == 'com.tikal.jenkins.plugins.multijob.MultiJobBuilder'
children().size() == 3
children().size() == 4
phaseName[0].value() == 'test'
continuationCondition[0].value() == condition
executionType[0].value() == 'PARALLEL'
phaseJobs[0].value().empty
}

where:
condition << ['FAILURE', 'ALWAYS']
}

def 'call phase with supported execution type'(String execution) {
setup:
jobManagement.isMinimumPluginVersionInstalled('jenkins-multijob-plugin', '1.16') >> true

when:
context.phase('test', 'SUCCESSFUL', execution) {
}

then:
with(context.stepNodes[0]) {
name() == 'com.tikal.jenkins.plugins.multijob.MultiJobBuilder'
children().size() == 4
phaseName[0].value() == 'test'
continuationCondition[0].value() == 'SUCCESSFUL'
executionType[0].value() == execution
phaseJobs[0].value().empty
}

where:
execution << ['SEQUENTIALLY']
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not use a where section if there is only one option, but you could also test both supported options here

Copy link
Copy Markdown
Author

@sanicheev sanicheev Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good option to modify codenarcTest to catch such conditions

}

def 'phase works inside conditionalSteps'() {
when:
context.conditionalSteps {
Expand Down