Skip to content
Permalink
Browse files
Merge pull request #250 from abayer/directive-generator
[JENKINS-49893] Add a Declarative directive snippet generator
  • Loading branch information
abayer committed Apr 5, 2018
2 parents fa8ed8b + 34485e8 commit a9fbdce7fe51171b2106ec183cd1381b3266b1ef
Showing with 4,797 additions and 53 deletions.
  1. +9 −5 ...del-api/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTPipelineDef.java
  2. +7 −0 ...model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/Any.java
  3. +17 −0 ...ition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipeline.java
  4. +5 −0 .../java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineFromDockerfile.java
  5. +18 −0 ...del-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/Label.java
  6. +7 −0 ...odel-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/None.java
  7. +43 −0 ...ion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AbstractDirective.java
  8. +117 −0 ...nition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AgentDirective.java
  9. +73 −0 ...n/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveDescriptor.java
  10. +193 −0 ...on/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator.java
  11. +113 −0 .../src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/EnvironmentDirective.java
  12. +169 −0 ...nition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/InputDirective.java
  13. +128 −0 ...on/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/LibrariesDirective.java
  14. +152 −0 ...tion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/OptionsDirective.java
  15. +91 −0 ...n/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ParametersDirective.java
  16. +107 −0 ...inition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/PostDirective.java
  17. +149 −0 ...nition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/StageDirective.java
  18. +150 −0 ...nition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ToolsDirective.java
  19. +92 −0 ...ion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/TriggersDirective.java
  20. +120 −0 ...inition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/WhenDirective.java
  21. +7 −0 ...main/java/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl/CheckoutToSubdirectory.java
  22. +7 −0 ...rc/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl/SkipDefaultCheckout.java
  23. +7 −1 ...ain/java/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl/SkipStagesAfterUnstable.java
  24. +3 −0 ...ava/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AbstractConditionalWithChildren.java
  25. +7 −0 ...tion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AllOfConditional.java
  26. +7 −0 ...tion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AnyOfConditional.java
  27. +7 −0 ...ion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditional.java
  28. +16 −3 .../src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeLogConditional.java
  29. +7 −0 .../src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional.java
  30. +7 −0 ...rc/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EnvironmentConditional.java
  31. +13 −3 ...ion/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EqualsConditional.java
  32. +14 −0 ...src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ExpressionConditional.java
  33. +15 −0 ...nition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/NotConditional.java
  34. +14 −15 ...-definition/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/Messages.properties
  35. +28 −0 ...ion/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/Any/config.jelly
  36. +52 −0 ...n/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipeline/config.jelly
  37. +55 −0 ...g/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/DockerPipelineFromDockerfile/config.jelly
  38. +31 −0 ...n/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/Label/config.jelly
  39. +28 −0 ...on/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/agent/impl/None/config.jelly
  40. +42 −0 ...in/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AgentDirective/config.jelly
  41. +26 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AgentDirective/help-agent.html
  42. +26 −0 .../main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/AgentDirective/help.html
  43. +102 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/index.jelly
  44. +146 −0 .../jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/symbol-hetero-list.jelly
  45. 0 ...main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGenerator/taglib
  46. +49 −0 ...ources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/EnvironmentDirective/config.jelly
  47. +26 −0 ...urces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/EnvironmentDirective/help-env.html
  48. +27 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/EnvironmentDirective/help-name.html
  49. +27 −0 ...ces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/EnvironmentDirective/help-value.html
  50. +26 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/EnvironmentDirective/help.html
  51. +33 −0 ...in/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/InputDirective/config.jelly
  52. +26 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/InputDirective/help-input.html
  53. +26 −0 .../main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/InputDirective/help.html
  54. +49 −0 ...esources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/LibrariesDirective/config.jelly
  55. +26 −0 ...ources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/LibrariesDirective/help-libs.html
  56. +27 −0 ...ources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/LibrariesDirective/help-name.html
  57. +27 −0 ...ces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/LibrariesDirective/help-version.html
  58. +26 −0 ...n/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/LibrariesDirective/help.html
  59. +28 −0 ...n/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/Messages.properties
  60. +33 −0 .../resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/OptionsDirective/config.jelly
  61. +26 −0 ...urces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/OptionsDirective/help-options.html
  62. +27 −0 ...ain/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/OptionsDirective/help.html
  63. +33 −0 ...sources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ParametersDirective/config.jelly
  64. +26 −0 ...org/jenkinsci/plugins/pipeline/modeldefinition/generator/ParametersDirective/help-parameters.html
  65. +28 −0 .../resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ParametersDirective/help.html
  66. +38 −0 ...ain/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/PostDirective/config.jelly
  67. +27 −0 ...c/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/PostDirective/help.html
  68. +42 −0 ...in/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/StageDirective/config.jelly
  69. +26 −0 ...ces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/StageDirective/help-contentType.html
  70. +26 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/StageDirective/help-directives.html
  71. +26 −0 .../resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/StageDirective/help-name.html
  72. +26 −0 .../main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/StageDirective/help.html
  73. +95 −0 ...in/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ToolsDirective/config.jelly
  74. +26 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ToolsDirective/help-tools.html
  75. +26 −0 .../main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/ToolsDirective/help.html
  76. +33 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/TriggersDirective/config.jelly
  77. +26 −0 ...ces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/TriggersDirective/help-triggers.html
  78. +26 −0 ...in/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/TriggersDirective/help.html
  79. +45 −0 ...ain/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/WhenDirective/config.jelly
  80. +26 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/WhenDirective/help-beforeAgent.html
  81. +27 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/generator/WhenDirective/help-conditional.html
  82. +27 −0 ...c/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/generator/WhenDirective/help.html
  83. +3 −4 ...ain/resources/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Messages.properties
  84. +31 −0 ...s/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl/CheckoutToSubdirectory/config.jelly
  85. +31 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/options/impl/SkipDefaultCheckout/config.jelly
  86. +28 −0 .../org/jenkinsci/plugins/pipeline/modeldefinition/options/impl/SkipStagesAfterUnstable/config.jelly
  87. +3 −3 ...n/src/main/resources/org/jenkinsci/plugins/pipeline/modeldefinition/validator/Messages.properties
  88. +35 −0 .../resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AllOfConditional/config.jelly
  89. +28 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AllOfConditional/help-children.html
  90. +35 −0 .../resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AnyOfConditional/config.jelly
  91. +28 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/AnyOfConditional/help-children.html
  92. +31 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditional/config.jelly
  93. +27 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/BranchConditional/help-compare.html
  94. +31 −0 ...ources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeLogConditional/config.jelly
  95. +27 −0 ...s/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeLogConditional/help-compare.html
  96. +2 −2 ...ources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeLogConditionalScript.groovy
  97. +34 −0 ...ources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ChangeSetConditional/config.jelly
  98. +37 −0 ...rces/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EnvironmentConditional/config.jelly
  99. +34 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/EqualsConditional/config.jelly
  100. +31 −0 ...urces/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/ExpressionConditional/config.jelly
  101. +39 −0 ...in/resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/NotConditional/config.jelly
  102. +24 −0 ...resources/org/jenkinsci/plugins/pipeline/modeldefinition/when/impl/NotConditional/help-child.html
  103. +4 −3 ...definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/AbstractModelDefTest.java
  104. +598 −0 ...rc/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/generator/DirectiveGeneratorTest.java
  105. +7 −0 ...tensions/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/DeclarativeAgent.java
  106. +6 −2 ...rc/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/agent/DeclarativeAgentDescriptor.java
  107. +6 −2 ...main/java/org/jenkinsci/plugins/pipeline/modeldefinition/options/DeclarativeOptionDescriptor.java
  108. +15 −0 ...rc/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/when/DeclarativeStageConditional.java
  109. +19 −3 ...va/org/jenkinsci/plugins/pipeline/modeldefinition/when/DeclarativeStageConditionalDescriptor.java
  110. +12 −7 pom.xml
@@ -135,13 +135,17 @@ public String toGroovy() {
* @return An indented string of Groovy, suitable for use in a Jenkinsfile.
*/
public String toPrettyGroovy() {
return toIndentedGroovy(toGroovy());
}

public static String toIndentedGroovy(@Nonnull String orig) {
StringBuilder result = new StringBuilder();

int indentCount = 0;
boolean tripleSingleQuotedString = false;

boolean first = true;
for (String r : toGroovy().split("\n")) {
for (String r : orig.split("\n")) {
if (first) {
first = false;
} else {
@@ -153,12 +157,12 @@ public String toPrettyGroovy() {
if (r.startsWith("}") || r.startsWith(")") || r.startsWith("]")) {
indentCount--;
}

result.append(indent(indentCount)).append(r);
if (!StringUtils.isEmpty(r)) {
result.append(indent(indentCount)).append(r);
}
if (r.endsWith("{") || r.endsWith("(") || r.endsWith("[")) {
indentCount++;
}

}

int index = r.indexOf("\'\'\'");
@@ -200,7 +204,7 @@ public void removeSourceLocation() {
}
}

private String indent(int count) {
private static String indent(int count) {
return StringUtils.repeat(" ", count);
}

@@ -30,6 +30,8 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;


public class Any extends DeclarativeAgent<Any> {

@@ -39,5 +41,10 @@ public Any() {

@Extension(ordinal = -900) @Symbol("any")
public static class DescriptorImpl extends DeclarativeAgentDescriptor<Any> {
@Override
@Nonnull
public String getDisplayName() {
return "Run on any agent";
}
}
}
@@ -25,11 +25,15 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl;

import hudson.Extension;
import hudson.Util;
import hudson.util.FormValidation;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.AbstractDockerAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.Nonnull;

@@ -57,5 +61,18 @@ public boolean isAlwaysPull() {

@Extension(ordinal = 1000) @Symbol("docker")
public static class DescriptorImpl extends DeclarativeAgentDescriptor<DockerPipeline> {
@Override
@Nonnull
public String getDisplayName() {
return "Run inside a Docker container";
}

public FormValidation doCheckImage(@QueryParameter String image) {
if (StringUtils.isEmpty(Util.fixEmptyAndTrim(image))) {
return FormValidation.error("Image is required.");
} else {
return FormValidation.ok();
}
}
}
}
@@ -106,5 +106,10 @@ public String getDockerfileAsString() {

@Extension(ordinal = 999) @Symbol("dockerfile")
public static class DescriptorImpl extends DeclarativeAgentDescriptor<DockerPipelineFromDockerfile> {
@Override
@Nonnull
public String getDisplayName() {
return "Build a Dockerfile and run in a container using that image";
}
}
}
@@ -25,13 +25,18 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl;

import hudson.Extension;
import hudson.Util;
import hudson.util.FormValidation;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Label extends DeclarativeAgent<Label> {
@@ -60,5 +65,18 @@ public void setCustomWorkspace(String customWorkspace) {

@Extension(ordinal = -800) @Symbol({"label","node"})
public static class DescriptorImpl extends DeclarativeAgentDescriptor<Label> {
@Override
@Nonnull
public String getDisplayName() {
return "Run on an agent matching a label";
}

public FormValidation doCheckLabel(@QueryParameter String label) {
if (StringUtils.isEmpty(Util.fixEmptyAndTrim(label))) {
return FormValidation.error("Label is required.");
} else {
return FormValidation.ok();
}
}
}
}
@@ -30,6 +30,8 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;


public class None extends DeclarativeAgent<None> {

@@ -40,5 +42,10 @@ public None() {

@Extension(ordinal = -1000) @Symbol("none")
public static class DescriptorImpl extends DeclarativeAgentDescriptor<None> {
@Override
@Nonnull
public String getDisplayName() {
return "Don't run on an agent";
}
}
}
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2018, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.generator;

import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;

public abstract class AbstractDirective<T extends AbstractDirective<T>> extends AbstractDescribableImpl<T> {
public final String toGroovy(boolean indent) {
Descriptor d = getDescriptor();
if (d instanceof DirectiveDescriptor) {
if (indent) {
return ((DirectiveDescriptor) d).toIndentedGroovy(this);
} else {
return ((DirectiveDescriptor) d).toGroovy(this);
}
} else {
return "// Not a valid descriptor";
}
}
}
@@ -0,0 +1,117 @@
/*
* The MIT License
*
* Copyright (c) 2018, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.pipeline.modeldefinition.generator;

import hudson.Extension;
import hudson.model.Descriptor;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
import org.jenkinsci.plugins.structs.SymbolLookup;
import org.jenkinsci.plugins.structs.describable.DescribableModel;
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable;
import org.jenkinsci.plugins.workflow.cps.Snippetizer;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

public class AgentDirective extends AbstractDirective<AgentDirective> {
private DeclarativeAgent agent;

@DataBoundConstructor
public AgentDirective(DeclarativeAgent agent) {
this.agent = agent;
}

public DeclarativeAgent getAgent() {
return agent;
}

@Extension
public static class DescriptorImpl extends DirectiveDescriptor<AgentDirective> {
@Override
@Nonnull
public String getName() {
return "agent";
}

@Override
@Nonnull
public String getDisplayName() {
return "Agent";
}

@Override
@Nonnull
public List<Descriptor> getDescriptors() {
List<Descriptor> descriptors = new ArrayList<>();
for (DeclarativeAgentDescriptor td : DeclarativeAgentDescriptor.all()) {
if (!SymbolLookup.getSymbolValue(td).isEmpty()) {
descriptors.add(td);
}
}

return descriptors;
}

@Override
@Nonnull
public String toGroovy(@Nonnull AgentDirective directive) {
if (directive.agent != null) {
DeclarativeAgentDescriptor desc = directive.agent.getDescriptor();

UninstantiatedDescribable ud = UninstantiatedDescribable.from(directive.agent);
DescribableModel model = ud.getModel();
if (model != null) {
StringBuilder result = new StringBuilder();
if (DeclarativeAgentDescriptor.zeroArgModels().containsKey(desc.getName())) {
// agent none or agent any
result.append("agent ").append(desc.getName());
} else {
result.append("agent {\n");
if (DeclarativeAgentDescriptor.noRequiredArgsModels().containsKey(desc.getName()) &&
ud.getArguments().entrySet().stream().allMatch(e -> e.getValue() == null
|| (e.getValue() instanceof String && e.getValue().equals("")))) {
// agent { dockerfile true }
result.append(desc.getName()).append(" true\n");
} else if (model.hasSingleRequiredParameter() && ud.getArguments().size() == 1) {
// agent { label 'foo' } or agent { docker 'image' }
result.append(Snippetizer.object2Groovy(ud)).append("\n");
} else {
// Multiple arguments etc
result.append(desc.getName()).append(" ").append(DirectiveGenerator.mapToClosure(ud.getArguments()));
}
result.append("}");
}
result.append("\n");
return result.toString();
}
}

return "// No valid agent defined\n";
}
}
}

0 comments on commit a9fbdce

Please sign in to comment.