Skip to content

Commit 32dc9db

Browse files
author
Willem Borgesius
committed
Share initEnvironment and resetEnvironment logic to use in multiple Declarations
1 parent 6984bd9 commit 32dc9db

File tree

2 files changed

+37
-59
lines changed

2 files changed

+37
-59
lines changed

src/main/groovy/com/lesfurets/jenkins/unit/declarative/GenericPipelineDeclaration.groovy

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,43 @@ abstract class GenericPipelineDeclaration {
5757
closure.call()
5858
}
5959

60-
def stage(String name,
60+
def stage(String stageName,
6161
@DelegatesTo(strategy = DELEGATE_FIRST, value = StageDeclaration) Closure closure) {
62-
this.stages.put(name, createComponent(StageDeclaration, closure).with { it.name = name; it })
62+
this.stages.put(stageName, createComponent(StageDeclaration, closure).with { it.name = stageName; it })
6363
}
6464

6565
def execute(Object delegate) {
66+
Map envValuestoRestore = [:]
67+
6668
// set environment
6769
if (this.environment) {
68-
Binding subBinding = new Binding()
69-
subBinding.metaClass.invokeMissingProperty = { propertyName ->
70-
delegate.getProperty(propertyName)
71-
}
72-
subBinding.metaClass.setProperty = { String propertyName, Object newValue ->
73-
(delegate.env as Map).put(propertyName, newValue)
70+
envValuestoRestore = initEnvironment(this.environment, delegate)
71+
}
72+
resetEnvironment(envValuestoRestore, delegate)
73+
}
74+
75+
public static Map initEnvironment(Closure environment, Object delegate) {
76+
Map envValuestoRestore = [:]
77+
Binding subBinding = new Binding()
78+
subBinding.metaClass.invokeMissingProperty = { propertyName ->
79+
delegate.getProperty(propertyName)
80+
}
81+
subBinding.metaClass.setProperty = { String propertyName, Object newValue ->
82+
if (delegate.hasProperty(propertyName)) {
83+
envValuestoRestore.put(propertyName, delegate.getProperty(propertyName))
7484
}
75-
def envClosure = this.environment.rehydrate(subBinding, delegate, this)
76-
envClosure.resolveStrategy = DELEGATE_FIRST
77-
envClosure.call()
85+
(delegate.env as Map).put(propertyName, newValue)
7886
}
87+
def envClosure = environment.rehydrate(subBinding, delegate, this)
88+
envClosure.resolveStrategy = DELEGATE_FIRST
89+
envClosure.call()
90+
return envValuestoRestore
7991
}
8092

93+
public static resetEnvironment(LinkedHashMap envValuestoRestore, delegate) {
94+
envValuestoRestore.entrySet().forEach { entry ->
95+
def envMap = delegate.env as Map
96+
envMap.put(entry.getKey(), entry.getValue())
97+
}
98+
}
8199
}
Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,26 @@
11
package com.lesfurets.jenkins.unit.declarative
22

33

4-
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.createComponent
54
import static com.lesfurets.jenkins.unit.declarative.GenericPipelineDeclaration.executeWith
6-
import static groovy.lang.Closure.*
5+
import static groovy.lang.Closure.DELEGATE_FIRST
76

8-
class StageDeclaration {
7+
class StageDeclaration extends GenericPipelineDeclaration {
98

10-
AgentDeclaration agent
11-
Closure environment
129
String name
1310
Closure steps
1411
WhenDeclaration when
1512
ParallelDeclaration parallel
16-
PostDeclaration post
1713
boolean failFast = false
1814
List<Closure> options = []
19-
Map<String, StageDeclaration> stages = [:]
2015

2116
StageDeclaration(String name) {
2217
this.name = name
2318
}
2419

25-
def agent(Object o) {
26-
this.agent = new AgentDeclaration().with { it.label = o; it }
27-
}
28-
29-
def agent(@DelegatesTo(strategy = DELEGATE_FIRST, value = AgentDeclaration) Closure closure) {
30-
this.agent = createComponent(AgentDeclaration, closure)
31-
}
32-
33-
def environment(Closure closure) {
34-
this.environment = closure
35-
}
36-
37-
def post(@DelegatesTo(strategy = DELEGATE_FIRST, value = PostDeclaration) Closure closure) {
38-
this.post = createComponent(PostDeclaration, closure)
39-
}
40-
4120
def steps(Closure closure) {
4221
this.steps = closure
4322
}
4423

45-
def stages(@DelegatesTo(DeclarativePipeline) Closure closure) {
46-
closure.call()
47-
}
48-
4924
def failFast(boolean failFast) {
5025
this.failFast = failFast
5126
}
@@ -63,8 +38,6 @@ class StageDeclaration {
6338
}
6439

6540
def execute(Object delegate) {
66-
Map envValuestoRestore = [:]
67-
6841
String name = this.name
6942
this.options.each {
7043
executeWith(delegate, it)
@@ -79,26 +52,16 @@ class StageDeclaration {
7952
}
8053

8154
if (!when || when.execute(delegate)) {
82-
// Set environment for stage
55+
Map envValuestoRestore = [:]
56+
57+
// set environment
8358
if (this.environment) {
84-
Binding subBinding = new Binding()
85-
subBinding.metaClass.invokeMissingProperty = { propertyName ->
86-
delegate.getProperty(propertyName)
87-
}
88-
subBinding.metaClass.setProperty = { String propertyName, Object newValue ->
89-
if(delegate.hasProperty(propertyName)){
90-
envValuestoRestore.put(propertyName, delegate.getProperty(propertyName))
91-
}
92-
(delegate.env as Map).put(propertyName, newValue)
93-
}
94-
def envClosure = this.environment.rehydrate(subBinding, delegate, this)
95-
envClosure.resolveStrategy = DELEGATE_FIRST
96-
envClosure.call()
59+
envValuestoRestore = initEnvironment(this.environment, delegate)
9760
}
9861

9962
// TODO handle credentials
10063
this.stages.entrySet().forEach { stageEntry ->
101-
stageEntry.getValue().execute(delegate)
64+
stageEntry.value.execute(delegate)
10265
}
10366
if(steps) {
10467
Closure stageBody = { agent?.execute(delegate) } >> steps.rehydrate(delegate, this, this)
@@ -108,13 +71,10 @@ class StageDeclaration {
10871
if (post) {
10972
this.post.execute(delegate)
11073
}
74+
resetEnvironment(envValuestoRestore, delegate)
11175
} else {
11276
executeWith(delegate, { echo "Skipping stage $name" })
11377
}
114-
envValuestoRestore.entrySet().forEach { entry ->
115-
def envMap = delegate.env as Map
116-
envMap.put(entry.getKey(), entry.getValue())
117-
}
11878
}
11979

12080
}

0 commit comments

Comments
 (0)