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 5, 2018
1 parent a85f7c5 commit e080af7
Show file tree
Hide file tree
Showing 43 changed files with 2,050 additions and 125 deletions.
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.ast; package org.jenkinsci.plugins.pipeline.modeldefinition.ast;


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


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

public ModelASTStage(Object sourceLocation) { public ModelASTStage(Object sourceLocation) {
super(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 @Override
public JSONObject toJSON() { public JSONObject toJSON() {
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
o.accumulate("name", name); 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) { if (agent != null) {
o.accumulate("agent", agent.toJSON()); o.accumulate("agent", agent.toJSON());
} }
Expand All @@ -75,6 +79,28 @@ public JSONObject toJSON() {
if (input != null) { if (input != null) {
o.accumulate("input", input.toJSON()); 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; return o;
} }
Expand All @@ -86,12 +112,7 @@ public void validate(@Nonnull final ModelValidator validator) {


public void validate(final ModelValidator validator, boolean isNested) { public void validate(final ModelValidator validator, boolean isNested) {
validator.validateElement(this, isNested); validator.validateElement(this, isNested);
for (ModelASTBranch branch : branches) {
branch.validate(validator);
}
if (parallel != null) {
parallel.validate(validator, true);
}
if (agent != null) { if (agent != null) {
agent.validate(validator); agent.validate(validator);
} }
Expand All @@ -113,6 +134,15 @@ public void validate(final ModelValidator validator, boolean isNested) {
if (input != null) { if (input != null) {
input.validate(validator); 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 @Override
Expand All @@ -138,13 +168,25 @@ public String toGroovy() {
if (input != null) { if (input != null) {
result.append(input.toGroovy()); 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) { if (failFast != null && failFast) {
result.append("failFast true\n"); result.append("failFast true\n");
} }
result.append("parallel {\n"); if (!parallelContent.isEmpty()) {
result.append(parallel.toGroovy()); result.append("parallel {\n");
result.append("}\n"); for (ModelASTStage content : parallelContent) {
result.append(content.toGroovy());
}
result.append("}\n");
}
} else { } else {
result.append("steps {\n"); result.append("steps {\n");
if (branches.size() > 1) { if (branches.size() > 1) {
Expand Down Expand Up @@ -173,10 +215,6 @@ public String toGroovy() {
result.append("}\n"); result.append("}\n");
} }


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

result.append("}\n"); result.append("}\n");


return result.toString(); return result.toString();
Expand All @@ -185,15 +223,9 @@ public String toGroovy() {
@Override @Override
public void removeSourceLocation() { public void removeSourceLocation() {
super.removeSourceLocation(); super.removeSourceLocation();
for (ModelASTBranch branch: branches) {
branch.removeSourceLocation();
}
if (agent != null) { if (agent != null) {
agent.removeSourceLocation(); agent.removeSourceLocation();
} }
if (parallel != null) {
parallel.removeSourceLocation();
}
if (when != null) { if (when != null) {
when.removeSourceLocation(); when.removeSourceLocation();
} }
Expand All @@ -212,6 +244,18 @@ public void removeSourceLocation() {
if (input != null) { if (input != null) {
input.removeSourceLocation(); 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() { public String getName() {
Expand All @@ -230,14 +274,6 @@ public void setAgent(ModelASTAgent agent) {
this.agent = agent; this.agent = agent;
} }


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

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

public ModelASTPostStage getPost() { public ModelASTPostStage getPost() {
return post; return post;
} }
Expand Down Expand Up @@ -270,6 +306,22 @@ public void setEnvironment(ModelASTEnvironment environment) {
this.environment = 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() { public Boolean getFailFast() {
return failFast; return failFast;
} }
Expand All @@ -278,10 +330,12 @@ public void setFailFast(Boolean f) {
this.failFast = f; this.failFast = f;
} }


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


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


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

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

@Override @Override
public String toString() { public String toString() {
return "ModelASTStage{" + return "ModelASTStage{" +
"name='" + name + '\'' + "name='" + name + '\'' +
", agent=" + agent + ", agent=" + agent +
", when=" + when + ", when=" + when +
", branches=" + branches +
", post=" + post + ", post=" + post +
", tools=" + tools + ", tools=" + tools +
", environment=" + environment + ", environment=" + environment +
", stages=" + stages +
", branches=" + branches +
", failFast=" + failFast + ", failFast=" + failFast +
", parallel=" + parallel + ", parallel=" + parallel +
", options=" + options + ", options=" + options +
", input=" + input + ", 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) { if (getInput() != null ? !getInput().equals(that.getInput()) : that.getInput() != null) {
return false; return false;
} }
if (getStages() != null ? !getStages().equals(that.getStages()) : that.getStages() != null) {
return false;
}
if (getFailFast() != null ? !getFailFast().equals(that.getFailFast()) : that.getFailFast() != null) { if (getFailFast() != null ? !getFailFast().equals(that.getFailFast()) : that.getFailFast() != null) {
return false; return false;
} }
if (getParallel() != null ? !getParallel().equals(that.getParallel()) : that.getParallel() != null) { if (getParallel() != null ? !getParallel().equals(that.getParallel()) : that.getParallel() != null) {
return false; return false;
} }
if (getParallelContent() != null ? !getParallelContent().equals(that.getParallelContent())
: that.getParallelContent() != null) {
return false;
}
return getBranches() != null ? getBranches().equals(that.getBranches()) : that.getBranches() == null; return getBranches() != null ? getBranches().equals(that.getBranches()) : that.getBranches() == null;


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

0 comments on commit e080af7

Please sign in to comment.