Skip to content

Commit 4ab13b8

Browse files
authored
Merge pull request #762 from jglick/closuritis
Reduce depth of `Closure`s being `call`ed
2 parents 338ad30 + 0e39d36 commit 4ab13b8

File tree

11 files changed

+271
-274
lines changed

11 files changed

+271
-274
lines changed

pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy

Lines changed: 169 additions & 209 deletions
Large diffs are not rendered by default.

pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/AnyScript.groovy

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@
2626
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl
2727

2828
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript
29-
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
29+
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2
3030
import org.jenkinsci.plugins.workflow.cps.CpsScript
3131

32-
class AnyScript extends DeclarativeAgentScript<Any> {
32+
class AnyScript extends DeclarativeAgentScript2<Any> {
3333

3434
AnyScript(CpsScript s, Any a) {
3535
super(s, a)
3636
}
3737

3838
@Override
39-
Closure run(Closure body) {
40-
return {
41-
script.node {
42-
CheckoutScript.doCheckout(script, describable, null, body).call()
43-
}
39+
void run(Closure body) {
40+
script.node {
41+
CheckoutScript.doCheckout2(script, describable, null, body)
4442
}
4543
}
4644
}

pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/LabelScript.groovy

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,27 @@ package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl
2727

2828

2929
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript
30-
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
30+
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2
3131
import org.jenkinsci.plugins.workflow.cps.CpsScript
3232

33-
class LabelScript extends DeclarativeAgentScript<Label> {
33+
class LabelScript extends DeclarativeAgentScript2<Label> {
3434

3535
LabelScript(CpsScript s, Label a) {
3636
super(s, a)
3737
}
3838

3939
@Override
40-
Closure run(Closure body) {
41-
Closure run = {
42-
script.node(describable?.label) {
43-
CheckoutScript.doCheckout(script, describable, describable.customWorkspace, body).call()
44-
}
45-
}
40+
void run(Closure body) {
4641
if (describable.retries > 1) {
47-
return {
48-
script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
49-
run.call()
42+
script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
43+
script.node(describable?.label) {
44+
CheckoutScript.doCheckout2(script, describable, describable.customWorkspace, body)
5045
}
5146
}
5247
} else {
53-
run
48+
script.node(describable?.label) {
49+
CheckoutScript.doCheckout2(script, describable, describable.customWorkspace, body)
50+
}
5451
}
5552
}
5653
}

pipeline-model-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/NoneScript.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@
2525

2626
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl
2727

28-
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
28+
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2
2929
import org.jenkinsci.plugins.workflow.cps.CpsScript
3030

31-
class NoneScript extends DeclarativeAgentScript<None> {
31+
class NoneScript extends DeclarativeAgentScript2<None> {
3232

3333
NoneScript(CpsScript s, None a) {
3434
super(s, a)
3535
}
3636

3737
@Override
38-
Closure run(Closure body) {
39-
return body
38+
void run(Closure body) {
39+
body.call()
4040
}
4141
}

pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BasicModelDefTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void setUpAgent() throws Exception {
7878

7979
@Issue("JENKINS-47363")
8080
// Give this a longer timeout
81-
@Test(timeout=5 * 60 * 1000)
81+
@Test(timeout=10 * 60 * 1000)
8282
public void stages300() throws Exception {
8383
assumeFalse("can exceed even 5m timeout", Functions.isWindows());
8484
RuntimeASTTransformer.SCRIPT_SPLITTING_TRANSFORMATION = true;

pipeline-model-definition/src/test/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/LabelAndOtherFieldAgentScript.groovy

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,23 @@
2525

2626
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl
2727

28-
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
28+
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2
2929
import org.jenkinsci.plugins.workflow.cps.CpsScript
3030

31-
class LabelAndOtherFieldAgentScript extends DeclarativeAgentScript<LabelAndOtherFieldAgent> {
31+
class LabelAndOtherFieldAgentScript extends DeclarativeAgentScript2<LabelAndOtherFieldAgent> {
3232

3333
LabelAndOtherFieldAgentScript(CpsScript s, LabelAndOtherFieldAgent a) {
3434
super(s, a)
3535
}
3636

3737
@Override
38-
Closure run(Closure body) {
38+
void run(Closure body) {
3939
script.echo "Running in labelAndOtherField with otherField = ${describable.getOtherField()}"
4040
script.echo "And nested: ${describable.getNested()}"
4141
Label l = (Label) Label.DescriptorImpl.instanceForName("label", [label: describable.label])
4242
l.inStage = describable.inStage
4343
l.doCheckout = describable.doCheckout
4444
LabelScript labelScript = (LabelScript) l.getScript(script)
45-
return labelScript.run {
46-
body.call()
47-
}
45+
labelScript.run(body)
4846
}
4947
}

pipeline-model-extensions/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/DeclarativeAgent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import edu.umd.cs.findbugs.annotations.NonNull;
3030
import hudson.Extension;
31+
import java.util.logging.Logger;
3132
import org.jenkinsci.plugins.pipeline.modeldefinition.options.DeclarativeOption;
3233
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptDescribable;
3334
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
@@ -41,6 +42,9 @@
4142
* @author Andrew Bayer
4243
*/
4344
public abstract class DeclarativeAgent<A extends DeclarativeAgent<A>> extends WithScriptDescribable<A> implements ExtensionPoint {
45+
46+
private static final Logger LOGGER = Logger.getLogger(DeclarativeAgent.class.getName());
47+
4448
protected boolean inStage;
4549
protected boolean doCheckout;
4650
protected String subdirectory;
@@ -53,7 +57,11 @@ public WithScriptScript getScript(CpsScript cpsScript) throws Exception {
5357

5458
cpsScript.getClass().getClassLoader().loadClass("org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript");
5559

56-
return super.getScript(cpsScript);
60+
var script = super.getScript(cpsScript);
61+
if (script instanceof DeclarativeAgentScript) {
62+
LOGGER.warning(() -> script.getClass().getName() + " should implement " + DeclarativeAgentScript2.class.getName());
63+
}
64+
return script;
5765
}
5866

5967
public void setInStage(boolean inStage) {

pipeline-model-extensions/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/DeclarativeAgentScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
2828
import org.jenkinsci.plugins.workflow.cps.CpsScript;
2929

30-
30+
@Deprecated
3131
public abstract class DeclarativeAgentScript<A extends DeclarativeAgent<A>> extends WithScriptScript<A> {
3232

3333
public DeclarativeAgentScript(CpsScript s, A a) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2016, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.jenkinsci.plugins.pipeline.modeldefinition.agent;
26+
27+
import groovy.lang.Closure;
28+
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptScript;
29+
import org.jenkinsci.plugins.workflow.cps.CpsScript;
30+
31+
public abstract class DeclarativeAgentScript2<A extends DeclarativeAgent<A>> extends WithScriptScript<A> {
32+
33+
public DeclarativeAgentScript2(CpsScript s, A a) {
34+
super(s, a);
35+
}
36+
37+
public abstract void run(Closure body);
38+
}

pipeline-model-extensions/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/RetryableDeclarativeAgent.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,18 @@
2828

2929
/**
3030
* A type of {@code agent} option that supports automatic retries.
31-
* Usage from your {@link DeclarativeAgentScript#run} would look something like:
31+
* Usage from your {@link DeclarativeAgentScript2#run} would look something like:
3232
* <pre>{@code
33-
* Closure run = {
34-
* script.node {
35-
* CheckoutScript.doCheckout(script, describable, null, body).call()
36-
* }
37-
* }
3833
* if (describable.retries > 1) {
39-
* return {
40-
* script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
41-
* run.call()
34+
* script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
35+
* script.node {
36+
* CheckoutScript.doCheckout2(script, describable, null, body)
4237
* }
4338
* }
4439
* } else {
45-
* run
40+
* script.node {
41+
* CheckoutScript.doCheckout2(script, describable, null, body)
42+
* }
4643
* }}</pre>
4744
*/
4845
public abstract class RetryableDeclarativeAgent<A extends RetryableDeclarativeAgent<A>> extends DeclarativeAgent<A> {

0 commit comments

Comments
 (0)