Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #70 from abayer/jenkins-40337-mk2
[JENKINS-40337] Add Declarative-specific options.
  • Loading branch information
abayer committed Dec 19, 2016
2 parents f155b96 + e7cb5d8 commit 01ba61a
Show file tree
Hide file tree
Showing 32 changed files with 639 additions and 218 deletions.
11 changes: 6 additions & 5 deletions SYNTAX.md
Expand Up @@ -264,19 +264,20 @@ parameters {
}
```

### Job Properties
* *Description*: Other job properties, such as build discarding, limiting concurrent builds, and more.
### Options
* *Description*: Other options exclusive to Declarative, such as `skipDefaultCheckout`, and traditional `JobProperty`s, such as
build discarding, limiting concurrent builds, and more.
* *Required*: No
* *Allowed In*: Top-level `pipeline` closure only.
* *Parameters*: None
* *Takes a Closure*: Yes
* *Closure Contents*: A sequence of one or more job property configurations, using `@Symbol` names for constructors.
* *Closure Contents*: A sequence of one or more Declarative option or job property configurations, using `@Symbol` names for constructors.
* Note that `[$class: 'Foo', arg1: 'something', ...]` syntax can not be used, only `booleanParam(...)` and the like.
* Note that the `parameters` and `pipelineTriggers` `@Symbol`s cannot be used here directly.
* *Examples*:

```groovy
properties {
options {
buildDiscarder(logRotator(numToKeepStr:'1'))
disableConcurrentBuilds()
}
Expand All @@ -299,5 +300,5 @@ properties {
* Literal list: `[exp,exp,...]`
* Literal map where keys are all constants: `[a:exp, b:exp, ... ]`
* Method calls where the left hand side is a variable reference or a sequence of property references: `x.y.z(...)`
* Method calls (including `@Symbol` constructors like used above in job properties, triggers and build parameters) where there is no left hand side.
* Method calls (including `@Symbol` constructors like used above in options, triggers and build parameters) where there is no left hand side.
* Closure without parameters: `{ ... }`

This file was deleted.

Expand Up @@ -3,12 +3,12 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

/**
* A single job property, corresponding eventually to {@code JobProperty}
* A single job property, corresponding eventually to {@code JobProperty} or DeclarativeOption.
*
* @author Andrew Bayer
*/
public class ModelASTJobProperty extends ModelASTMethodCall {
public ModelASTJobProperty(Object sourceLocation) {
public class ModelASTOption extends ModelASTMethodCall {
public ModelASTOption(Object sourceLocation) {
super(sourceLocation);
}

Expand All @@ -20,7 +20,7 @@ public void validate(final ModelValidator validator) {

@Override
public String toString() {
return "ModelASTJobProperty{"+super.toString()+"}";
return "ModelASTOption{"+super.toString()+"}";
}

@Override
Expand Down
@@ -0,0 +1,95 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

/**
* A container for one or more {@link ModelASTOption}s
*
* @author Andrew Bayer
*/
public final class ModelASTOptions extends ModelASTElement {
private List<ModelASTOption> options = new ArrayList<ModelASTOption>();

public ModelASTOptions(Object sourceLocation) {
super(sourceLocation);
}

@Override
public JSONObject toJSON() {
final JSONArray a = new JSONArray();
for (ModelASTOption option : options) {
a.add(option.toJSON());
}
return new JSONObject().accumulate("options", a);
}

@Override
public void validate(final ModelValidator validator) {
validator.validateElement(this);
for (ModelASTOption option : options) {
option.validate(validator);
}
}

@Override
public String toGroovy() {
StringBuilder result = new StringBuilder("options {\n");
for (ModelASTOption option : options) {
result.append(option.toGroovy()).append("\n");
}
result.append("}\n");
return result.toString();
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();
for (ModelASTOption option : options) {
option.removeSourceLocation();
}
}

public List<ModelASTOption> getOptions() {
return options;
}

public void setOptions(List<ModelASTOption> options) {
this.options = options;
}

@Override
public String toString() {
return "ModelASTOptions{" +
"options=" + options +
"}";
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}

ModelASTOptions that = (ModelASTOptions) o;

return getOptions() != null ? getOptions().equals(that.getOptions()) : that.getOptions() == null;

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getOptions() != null ? getOptions().hashCode() : 0);
return result;
}
}
Expand Up @@ -16,7 +16,7 @@ public final class ModelASTPipelineDef extends ModelASTElement {
private ModelASTEnvironment environment;
private ModelASTAgent agent;
private ModelASTTools tools;
private ModelASTJobProperties properties;
private ModelASTOptions options;
private ModelASTBuildParameters parameters;
private ModelASTTriggers triggers;
private ModelASTWrappers wrappers;
Expand All @@ -33,10 +33,10 @@ public JSONObject toJSON() {
a.put("environment", environment != null ? environment.toJSON() : null);
a.put("agent", agent != null ? agent.toJSON() : null);
a.put("tools", tools != null ? tools.toJSON() : null);
if (properties != null && !properties.getProperties().isEmpty()) {
a.put("properties", properties.toJSON());
if (options != null && !options.getOptions().isEmpty()) {
a.put("options", options.toJSON());
} else {
a.put("properties", null);
a.put("options", null);
}
if (parameters != null && !parameters.getParameters().isEmpty()) {
a.put("parameters", parameters.toJSON());
Expand Down Expand Up @@ -75,8 +75,8 @@ public void validate(ModelValidator validator) {
if (tools != null) {
tools.validate(validator);
}
if (properties != null) {
properties.validate(validator);
if (options != null) {
options.validate(validator);
}
if (parameters != null) {
parameters.validate(validator);
Expand Down Expand Up @@ -108,8 +108,8 @@ public String toGroovy() {
if (postBuild != null) {
result.append(postBuild.toGroovy()).append('\n');
}
if (properties != null && !properties.getProperties().isEmpty()) {
result.append(properties.toGroovy()).append('\n');
if (options != null && !options.getOptions().isEmpty()) {
result.append(options.toGroovy()).append('\n');
}
if (parameters != null && !parameters.getParameters().isEmpty()) {
result.append(parameters.toGroovy()).append('\n');
Expand Down Expand Up @@ -182,8 +182,8 @@ public void removeSourceLocation() {
if (tools != null) {
tools.removeSourceLocation();
}
if (properties != null) {
properties.removeSourceLocation();
if (options != null) {
options.removeSourceLocation();
}
if (parameters != null) {
parameters.removeSourceLocation();
Expand Down Expand Up @@ -240,12 +240,12 @@ public void setTools(ModelASTTools tools) {
this.tools = tools;
}

public ModelASTJobProperties getProperties() {
return properties;
public ModelASTOptions getOptions() {
return options;
}

public void setProperties(ModelASTJobProperties properties) {
this.properties = properties;
public void setOptions(ModelASTOptions options) {
this.options = options;
}

public ModelASTBuildParameters getParameters() {
Expand Down Expand Up @@ -280,7 +280,7 @@ public String toString() {
", environment=" + environment +
", agent=" + agent +
", tools=" + tools +
", properties=" + properties +
", options=" + options +
", parameters=" + parameters +
", triggers=" + triggers +
", wrappers=" + wrappers +
Expand Down Expand Up @@ -318,9 +318,9 @@ public boolean equals(Object o) {
if (getTools() != null ? !getTools().equals(that.getTools()) : that.getTools() != null) {
return false;
}
if (getProperties() != null
? !getProperties().equals(that.getProperties())
: that.getProperties() != null) {
if (getOptions() != null
? !getOptions().equals(that.getOptions())
: that.getOptions() != null) {
return false;
}
if (getParameters() != null ? !getParameters().equals(that.getParameters()) : that.getParameters() != null) {
Expand All @@ -341,7 +341,7 @@ public int hashCode() {
result = 31 * result + (getEnvironment() != null ? getEnvironment().hashCode() : 0);
result = 31 * result + (getAgent() != null ? getAgent().hashCode() : 0);
result = 31 * result + (getTools() != null ? getTools().hashCode() : 0);
result = 31 * result + (getProperties() != null ? getProperties().hashCode() : 0);
result = 31 * result + (getOptions() != null ? getOptions().hashCode() : 0);
result = 31 * result + (getParameters() != null ? getParameters().hashCode() : 0);
result = 31 * result + (getTriggers() != null ? getTriggers().hashCode() : 0);
result = 31 * result + (getWrappers() != null ? getWrappers().hashCode() : 0);
Expand Down
Expand Up @@ -32,8 +32,8 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildParameter;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildParameters;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTEnvironment;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTJobProperties;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTJobProperty;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTOptions;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTOption;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTMethodCall;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPostBuild;
Expand Down Expand Up @@ -72,13 +72,13 @@ public interface ModelValidator {

boolean validateElement(ModelASTMethodCall methodCall);

boolean validateElement(ModelASTJobProperties properties);
boolean validateElement(ModelASTOptions properties);

boolean validateElement(ModelASTTriggers triggers);

boolean validateElement(ModelASTBuildParameters buildParameters);

boolean validateElement(ModelASTJobProperty jobProperty);
boolean validateElement(ModelASTOption jobProperty);

boolean validateElement(ModelASTTrigger trigger);

Expand Down

0 comments on commit 01ba61a

Please sign in to comment.