Skip to content

Commit

Permalink
[FIXED JENKINS-46809] Add sequential groups of parallel stages
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed Mar 28, 2018
1 parent 18983eb commit 78500b3
Show file tree
Hide file tree
Showing 42 changed files with 2,048 additions and 123 deletions.
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
Expand All @@ -17,40 +18,43 @@
* @see ModelASTPipelineDef
*/
public final class ModelASTStage extends ModelASTElement {
private String name;
private ModelASTAgent agent;
protected String name;
protected ModelASTAgent agent;
protected ModelASTPostStage post;
protected ModelASTWhen when;
protected ModelASTTools tools;
protected ModelASTEnvironment environment;
private ModelASTStages stages;
private List<ModelASTBranch> branches = new ArrayList<>();
private ModelASTPostStage post;
private ModelASTWhen when;
private ModelASTTools tools;
private ModelASTEnvironment environment;
private Boolean failFast;
private ModelASTStages parallel;
private ModelASTOptions options;
private ModelASTStageInput input;

@Deprecated
private transient ModelASTStages parallel;
private List<ModelASTStage> parallelContent = new ArrayList<>();

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

protected Object readResolve() throws IOException {
// If there's already a set of parallel stages defined, add that to the parallel content instead.
if (this.parallel != null) {
if (this.parallelContent == null) {
this.parallelContent = new ArrayList<>();
}
this.parallelContent.addAll(this.parallel.getStages());
this.parallel = null;
}
return this;
}

@Override
public JSONObject toJSON() {
JSONObject o = new JSONObject();
o.accumulate("name", name);

if (branches.isEmpty() && parallel != null) {
o.accumulate("parallel", parallel.toJSON());
} else {
final JSONArray a = new JSONArray();
for (ModelASTBranch branch : branches) {
a.add(branch.toJSON());
}
o.accumulate("branches", a);
}

if (failFast != null) {
o.accumulate("failFast", failFast);
}
if (agent != null) {
o.accumulate("agent", agent.toJSON());
}
Expand All @@ -75,6 +79,28 @@ public JSONObject toJSON() {
if (input != null) {
o.accumulate("input", input.toJSON());
}
if (stages != null) {
o.accumulate("stages", stages.toJSON());
}
if (branches.isEmpty()) {
if (!parallelContent.isEmpty()) {
final JSONArray a = new JSONArray();
for (ModelASTStage content : parallelContent) {
a.add(content.toJSON());
}
o.accumulate("parallel", a);
}
} else {
final JSONArray a = new JSONArray();
for (ModelASTBranch branch : branches) {
a.add(branch.toJSON());
}
o.accumulate("branches", a);
}

if (failFast != null) {
o.accumulate("failFast", failFast);
}

return o;
}
Expand All @@ -86,12 +112,7 @@ public void validate(@Nonnull final ModelValidator validator) {

public void validate(final ModelValidator validator, boolean isNested) {
validator.validateElement(this, isNested);
for (ModelASTBranch branch : branches) {
branch.validate(validator);
}
if (parallel != null) {
parallel.validate(validator, true);
}

if (agent != null) {
agent.validate(validator);
}
Expand All @@ -113,6 +134,15 @@ public void validate(final ModelValidator validator, boolean isNested) {
if (input != null) {
input.validate(validator);
}
if (stages != null) {
stages.validate(validator, true);
}
for (ModelASTBranch branch : branches) {
branch.validate(validator);
}
for (ModelASTStage content: parallelContent) {
content.validate(validator, true);
}
}

@Override
Expand All @@ -138,13 +168,25 @@ public String toGroovy() {
if (input != null) {
result.append(input.toGroovy());
}
if (branches.isEmpty() && parallel != null) {
if (post != null) {
result.append(post.toGroovy());
}
if (stages != null) {
result.append("stages {\n");
result.append(stages.toGroovy());
result.append("}\n");
}
if (branches.isEmpty()) {
if (failFast != null && failFast) {
result.append("failFast true\n");
}
result.append("parallel {\n");
result.append(parallel.toGroovy());
result.append("}\n");
if (!parallelContent.isEmpty()) {
result.append("parallel {\n");
for (ModelASTStage content : parallelContent) {
result.append(content.toGroovy());
}
result.append("}\n");
}
} else {
result.append("steps {\n");
if (branches.size() > 1) {
Expand Down Expand Up @@ -173,10 +215,6 @@ public String toGroovy() {
result.append("}\n");
}

if (post != null) {
result.append(post.toGroovy());
}

result.append("}\n");

return result.toString();
Expand All @@ -185,15 +223,9 @@ public String toGroovy() {
@Override
public void removeSourceLocation() {
super.removeSourceLocation();
for (ModelASTBranch branch: branches) {
branch.removeSourceLocation();
}
if (agent != null) {
agent.removeSourceLocation();
}
if (parallel != null) {
parallel.removeSourceLocation();
}
if (when != null) {
when.removeSourceLocation();
}
Expand All @@ -212,6 +244,18 @@ public void removeSourceLocation() {
if (input != null) {
input.removeSourceLocation();
}
if (stages != null) {
stages.removeSourceLocation();
}
for (ModelASTBranch branch: branches) {
branch.removeSourceLocation();
}
if (parallel != null) {
parallel.removeSourceLocation();
}
for (ModelASTStage content : parallelContent) {
content.removeSourceLocation();
}
}

public String getName() {
Expand All @@ -230,14 +274,6 @@ public void setAgent(ModelASTAgent agent) {
this.agent = agent;
}

public List<ModelASTBranch> getBranches() {
return branches;
}

public void setBranches(List<ModelASTBranch> branches) {
this.branches = branches;
}

public ModelASTPostStage getPost() {
return post;
}
Expand Down Expand Up @@ -270,6 +306,22 @@ public void setEnvironment(ModelASTEnvironment environment) {
this.environment = environment;
}

public ModelASTStages getStages() {
return stages;
}

public void setStages(ModelASTStages stages) {
this.stages = stages;
}

public List<ModelASTBranch> getBranches() {
return branches;
}

public void setBranches(List<ModelASTBranch> branches) {
this.branches = branches;
}

public Boolean getFailFast() {
return failFast;
}
Expand All @@ -278,10 +330,12 @@ public void setFailFast(Boolean f) {
this.failFast = f;
}

@Deprecated
public ModelASTStages getParallel() {
return parallel;
}

@Deprecated
public void setParallel(ModelASTStages s) {
this.parallel = s;
}
Expand All @@ -302,20 +356,30 @@ public void setInput(ModelASTStageInput input) {
this.input = input;
}

public List<ModelASTStage> getParallelContent() {
return parallelContent;
}

public void setParallelContent(List<ModelASTStage> parallelContent) {
this.parallelContent = parallelContent;
}

@Override
public String toString() {
return "ModelASTStage{" +
"name='" + name + '\'' +
", agent=" + agent +
", when=" + when +
", branches=" + branches +
", post=" + post +
", tools=" + tools +
", environment=" + environment +
", stages=" + stages +
", branches=" + branches +
", failFast=" + failFast +
", parallel=" + parallel +
", options=" + options +
", input=" + input +
", parallelContent=" + parallelContent +
"}";
}

Expand Down Expand Up @@ -357,12 +421,19 @@ public boolean equals(Object o) {
if (getInput() != null ? !getInput().equals(that.getInput()) : that.getInput() != null) {
return false;
}
if (getStages() != null ? !getStages().equals(that.getStages()) : that.getStages() != null) {
return false;
}
if (getFailFast() != null ? !getFailFast().equals(that.getFailFast()) : that.getFailFast() != null) {
return false;
}
if (getParallel() != null ? !getParallel().equals(that.getParallel()) : that.getParallel() != null) {
return false;
}
if (getParallelContent() != null ? !getParallelContent().equals(that.getParallelContent())
: that.getParallelContent() != null) {
return false;
}
return getBranches() != null ? getBranches().equals(that.getBranches()) : that.getBranches() == null;

}
Expand All @@ -372,15 +443,17 @@ public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getAgent() != null ? getAgent().hashCode() : 0);
result = 31 * result + (getBranches() != null ? getBranches().hashCode() : 0);
result = 31 * result + (getWhen() != null ? getWhen().hashCode() : 0);
result = 31 * result + (getPost() != null ? getPost().hashCode() : 0);
result = 31 * result + (getTools() != null ? getTools().hashCode() : 0);
result = 31 * result + (getEnvironment() != null ? getEnvironment().hashCode() : 0);
result = 31 * result + (getStages() != null ? getStages().hashCode() : 0);
result = 31 * result + (getBranches() != null ? getBranches().hashCode() : 0);
result = 31 * result + (getFailFast() != null ? getFailFast().hashCode() : 0);
result = 31 * result + (getParallel() != null ? getParallel().hashCode() : 0);
result = 31 * result + (getOptions() != null ? getOptions().hashCode() : 0);
result = 31 * result + (getInput() != null ? getInput().hashCode() : 0);
result = 31 * result + (getParallelContent() != null ? getParallelContent().hashCode() : 0);
return result;
}
}
3 changes: 3 additions & 0 deletions pipeline-model-api/src/main/resources/ast-schema.json
Expand Up @@ -519,6 +519,9 @@
},
"parallel": {
"$ref": "#/definitions/stages"
},
"stages": {
"$ref": "#/definitions/stages"
}
},
"required": [
Expand Down

0 comments on commit 78500b3

Please sign in to comment.