Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-41185] Add support for and/not/or of when conditions
- Loading branch information
Showing
29 changed files
with
1,121 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
...c/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTWhenCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import groovy.transform.ToString; | ||
import hudson.model.Describable; | ||
import hudson.model.Descriptor; | ||
import net.sf.json.JSONArray; | ||
import net.sf.json.JSONObject; | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.DescriptorLookupCache; | ||
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator; | ||
import org.jenkinsci.plugins.structs.describable.DescribableModel; | ||
import org.jenkinsci.plugins.structs.describable.DescribableParameter; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Andrew Bayer | ||
*/ | ||
@ToString(includeSuper = true, includeSuperProperties = true) | ||
@SuppressFBWarnings(value = "SE_NO_SERIALVERSIONID") | ||
public class ModelASTWhenCondition extends ModelASTElement implements ModelASTWhenContent { | ||
private String name; | ||
private ModelASTArgumentList args; | ||
private List<ModelASTWhenContent> children = new ArrayList<>(); | ||
|
||
public ModelASTWhenCondition(Object sourceLocation) { | ||
super(sourceLocation); | ||
} | ||
|
||
@Override | ||
public JSONObject toJSON() { | ||
JSONObject o = new JSONObject(); | ||
o.accumulate("name", name); | ||
if (args != null) { | ||
o.accumulate("arguments", args.toJSON()); | ||
} | ||
if (!children.isEmpty()) { | ||
final JSONArray a = new JSONArray(); | ||
for (ModelASTWhenContent child : children) { | ||
a.add(child.toJSON()); | ||
} | ||
o.accumulate("children", a); | ||
} | ||
return o; | ||
} | ||
|
||
@Override | ||
public void validate(@Nonnull ModelValidator validator) { | ||
validator.validateElement(this); | ||
if (args != null) { | ||
args.validate(validator); | ||
} | ||
for (ModelASTWhenContent c : children) { | ||
c.validate(validator); | ||
} | ||
} | ||
|
||
@Override | ||
public String toGroovy() { | ||
|
||
StringBuilder result = new StringBuilder(); | ||
if (!children.isEmpty()) { | ||
result.append(name).append(" {\n"); | ||
for (ModelASTWhenContent child : children) { | ||
result.append(child.toGroovy()).append("\n"); | ||
} | ||
result.append("}\n"); | ||
} else { | ||
result.append(name).append(" ").append(getArgs().toGroovy()); | ||
} | ||
return result.toString(); | ||
} | ||
|
||
@Override | ||
public void removeSourceLocation() { | ||
super.removeSourceLocation(); | ||
if (args != null) { | ||
args.removeSourceLocation(); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public ModelASTArgumentList getArgs() { | ||
return args; | ||
} | ||
|
||
public void setArgs(ModelASTArgumentList args) { | ||
this.args = args; | ||
} | ||
|
||
public List<ModelASTWhenContent> getChildren() { | ||
return children; | ||
} | ||
|
||
public void setChildren(List<ModelASTWhenContent> c) { | ||
this.children = c; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ModelASTWhenCondition{" + | ||
"name='" + name + '\'' + | ||
", args=" + args + | ||
", children=" + children + | ||
"}"; | ||
} | ||
|
||
@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; | ||
} | ||
|
||
ModelASTWhenCondition that = (ModelASTWhenCondition) o; | ||
|
||
if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null) { | ||
return false; | ||
} | ||
if (getChildren() != null ? !getChildren().equals(that.getChildren()) : that.getChildren() != null) { | ||
return false; | ||
} | ||
return getArgs() != null ? getArgs().equals(that.getArgs()) : that.getArgs() == null; | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = super.hashCode(); | ||
result = 31 * result + (getName() != null ? getName().hashCode() : 0); | ||
result = 31 * result + (getArgs() != null ? getArgs().hashCode() : 0); | ||
result = 31 * result + (getChildren() != null ? getChildren().hashCode() : 0); | ||
return result; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/ast/ModelASTWhenContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator; | ||
|
||
/** | ||
* | ||
* @author Andrew Bayer | ||
*/ | ||
public interface ModelASTWhenContent { | ||
String toGroovy(); | ||
|
||
Object toJSON(); | ||
|
||
void validate(ModelValidator validator); | ||
|
||
void removeSourceLocation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.