Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-41900] Expose doCheckout on DeclarativeAgent
Reduces dependency load for other plugins extending DeclarativeAgent.
  • Loading branch information
abayer committed Feb 15, 2017
1 parent 736b3c4 commit 32dff60
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Expand Up @@ -26,6 +26,7 @@

import hudson.ExtensionPoint;
import org.jenkinsci.plugins.pipeline.modeldefinition.withscript.WithScriptDescribable;
import org.jenkinsci.plugins.workflow.cps.CpsScript;
import org.kohsuke.stapler.DataBoundSetter;

/**
Expand All @@ -35,6 +36,7 @@
*/
public abstract class DeclarativeAgent<A extends DeclarativeAgent<A>> extends WithScriptDescribable<A> implements ExtensionPoint {
protected Object context;
protected boolean doCheckout;

@DataBoundSetter
public void setContext(Object context) {
Expand All @@ -44,4 +46,22 @@ public void setContext(Object context) {
public Object getContext() {
return context;
}

@DataBoundSetter
public void setDoCheckout(boolean doCheckout) {
this.doCheckout = doCheckout;
}

public boolean isDoCheckout() {
return doCheckout;
}

public boolean hasScmContext(CpsScript script) {
try {
script.getProperty("scm");
return true;
} catch (Exception e) {
return false;
}
}
}
Expand Up @@ -26,9 +26,12 @@ package org.jenkinsci.plugins.pipeline.modeldefinition.model
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.None
import org.jenkinsci.plugins.pipeline.modeldefinition.options.impl.SkipDefaultCheckout
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable


Expand Down Expand Up @@ -63,6 +66,15 @@ public class Agent extends MappedClosure<Object,Agent> implements Serializable {

DeclarativeAgent a = DeclarativeAgentDescriptor.instanceForDescriptor(foundDescriptor, argMap)
a.setContext(context)
boolean doCheckout = false
if (context instanceof Root) {
Root root = (Root)context
SkipDefaultCheckout skip = (SkipDefaultCheckout)root.options?.options?.get("skipDefaultCheckout")
if (!skip?.isSkipDefaultCheckout()) {
doCheckout = true
}
}
a.setDoCheckout(doCheckout)
return a
} else {
return null
Expand Down
Expand Up @@ -45,13 +45,9 @@ public class LabelScript extends DeclarativeAgentScript<Label> {
return {
try {
script.node(describable?.label) {
if (describable.context instanceof Root) {
Root root = (Root)describable.context
SkipDefaultCheckout skip = (SkipDefaultCheckout)root.options?.options?.get("skipDefaultCheckout")
if (!skip?.isSkipDefaultCheckout() && Utils.hasScmContext(script)) {
script.stage(SyntheticStageNames.checkout()) {
script.checkout script.scm
}
if (describable.isDoCheckout() && describable.hasScmContext(script)) {
script.stage(SyntheticStageNames.checkout()) {
script.checkout script.scm
}
}
body.call()
Expand Down

0 comments on commit 32dff60

Please sign in to comment.