Skip to content
Permalink
Browse files

[JENKINS-38110] Add a libraries section

Note that this is very preliminary at this point - like, it doesn't
actually call the library step yet, since we don't yet have a release
of workflow-cps-global-lib-plugin with that included to depend on. We
also don't have parsing tests or any of that jazz. But that's ok.
  • Loading branch information
abayer committed Mar 1, 2017
1 parent faa4b12 commit 91d361bb30134b79fdf6ed4089e7cd110008c289
@@ -0,0 +1,120 @@
/*
* The MIT License
*
* Copyright (c) 2017, 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.ast;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;

import java.util.ArrayList;
import java.util.List;

/**
* A container for one or more library strings
*
* @author Andrew Bayer
*/
public final class ModelASTLibraries extends ModelASTElement {
private List<ModelASTValue> libs = new ArrayList<>();

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

@Override
public JSONObject toJSON() {
final JSONArray a = new JSONArray();
for (ModelASTValue v : libs) {
a.add(v.toJSON());
}
return new JSONObject().accumulate("libraries", a);
}

@Override
public void validate(final ModelValidator validator) {
validator.validateElement(this);
for (ModelASTValue v : libs) {
v.validate(validator);
}
}

@Override
public String toGroovy() {
StringBuilder result = new StringBuilder("libraries {\n");
for (ModelASTValue v : libs) {
result.append("lib(").append(v.toGroovy()).append(")\n");
}
result.append("}\n");
return result.toString();
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation();
for (ModelASTValue v : libs) {
v.removeSourceLocation();
}
}

public List<ModelASTValue> getLibs() {
return libs;
}

public void setLibs(List<ModelASTValue> libs) {
this.libs = libs;
}

@Override
public String toString() {
return "ModelASTLibraries{" +
"libs=" + libs +
"}";
}

@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;
}

ModelASTLibraries that = (ModelASTLibraries) o;

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

}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getLibs() != null ? getLibs().hashCode() : 0);
return result;
}
}
@@ -19,6 +19,7 @@
private ModelASTOptions options;
private ModelASTBuildParameters parameters;
private ModelASTTriggers triggers;
private ModelASTLibraries libraries;

public ModelASTPipelineDef(Object sourceLocation) {
super(sourceLocation);
@@ -47,6 +48,11 @@ public JSONObject toJSON() {
} else {
a.put("triggers", null);
}
if (libraries != null && !libraries.getLibs().isEmpty()) {
a.put("libraries", libraries.toJSON());
} else {
a.put("libraries", null);
}
return new JSONObject().accumulate("pipeline", a);
}

@@ -78,6 +84,9 @@ public void validate(ModelValidator validator) {
if (triggers != null) {
triggers.validate(validator);
}
if (libraries != null) {
libraries.validate(validator);
}
}

@Override
@@ -87,6 +96,9 @@ public String toGroovy() {
if (agent != null) {
result.append(agent.toGroovy());
}
if (libraries != null) {
result.append(libraries.toGroovy());
}
if (stages != null) {
result.append(stages.toGroovy());
}
@@ -161,6 +173,9 @@ public void removeSourceLocation() {
if (stages != null) {
stages.removeSourceLocation();
}
if (libraries != null) {
libraries.removeSourceLocation();
}
if (postBuild != null) {
postBuild.removeSourceLocation();
}
@@ -193,6 +208,14 @@ public void setStages(ModelASTStages stages) {
this.stages = stages;
}

public ModelASTLibraries getLibraries() {
return libraries;
}

public void setLibraries(ModelASTLibraries libraries) {
this.libraries = libraries;
}

public ModelASTPostBuild getPostBuild() {
return postBuild;
}
@@ -261,6 +284,7 @@ public String toString() {
", options=" + options +
", parameters=" + parameters +
", triggers=" + triggers +
", libraries=" + libraries +
"}";
}

@@ -303,6 +327,9 @@ public boolean equals(Object o) {
if (getParameters() != null ? !getParameters().equals(that.getParameters()) : that.getParameters() != null) {
return false;
}
if (getLibraries() != null ? !getLibraries().equals(that.getLibraries()) : that.getLibraries() != null) {
return false;
}
return getTriggers() != null ? getTriggers().equals(that.getTriggers()) : that.getTriggers() == null;

}
@@ -318,6 +345,7 @@ public int hashCode() {
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 + (getLibraries() != null ? getLibraries().hashCode() : 0);
return result;
}
}
@@ -34,6 +34,7 @@
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBuildParameters;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTClosureMap;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTEnvironment;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTLibraries;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTOptions;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTOption;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTMethodCall;
@@ -90,5 +91,7 @@

boolean validateElement(ModelASTStages stages);

boolean validateElement(ModelASTLibraries libraries);

boolean validateWhenCondition(ModelASTStep condition);
}
@@ -156,6 +156,14 @@
}
]
},
"libraries": {
"description": "One or more shared library identifiers to load",
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
},
"options": {
"description": "One or more options (including job properties, wrappers, and options specific to Declarative Pipelines)",
"type": "object",
@@ -440,6 +448,9 @@
},
"parameters": {
"$ref": "#/definitions/parameters"
},
"libraries": {
"$ref": "#/definitions/libraries"
}
},
"required": [
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright (c) 2017, 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.model

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString


/**
* A container for one or more library identifiers, within the build, in the order they're declared.
*
* @author Andrew Bayer
*/
@ToString
@EqualsAndHashCode
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public class Libraries implements Serializable {
List<String> libs = []

Libraries libs(List<String> s) {
this.libs = s
return this
}

List<String> getLibs() {
return libs
}
}
@@ -55,6 +55,8 @@ public class Root implements NestedModel, Serializable {

Parameters parameters

Libraries libraries

Root stages(Stages s) {
this.stages = s
return this
@@ -95,6 +97,11 @@ public class Root implements NestedModel, Serializable {
return this
}

Root libraries(Libraries l) {
this.libraries = l
return this
}

/**
* Helper method for translating the key/value pairs in the {@link Environment} into a list of "key=value" strings
* suitable for use with the withEnv step.
@@ -116,6 +116,9 @@ class JSONParser implements Parser {
case 'parameters':
pipelineDef.parameters = parseBuildParameters(pipelineJson.append(JsonPointer.of("parameters")))
break
case 'libraries':
pipelineDef.libraries = parseLibraries(pipelineJson.append(JsonPointer.of("libraries")))
break
default:
errorCollector.error(pipelineDef, Messages.Parser_UndefinedSection(sectionName))
}
@@ -201,6 +204,18 @@ class JSONParser implements Parser {
return when
}

public @CheckForNull ModelASTLibraries parseLibraries(JsonTree j) {
ModelASTLibraries l = new ModelASTLibraries(j)

JsonTree libsTree = j.append(JsonPointer.of("libraries"))
libsTree.node.eachWithIndex { JsonNode entry, int i ->
JsonTree thisNode = libsTree.append(JsonPointer.of(i))
l.libs.add(ModelASTValue.fromConstant(thisNode.node.asText(), thisNode))
}

return l
}

public @CheckForNull ModelASTOptions parseOptions(JsonTree j) {
ModelASTOptions options = new ModelASTOptions(j)

0 comments on commit 91d361b

Please sign in to comment.
You can’t perform that action at this time.