Skip to content

Commit de98932

Browse files
timbrown5timja
andauthored
Add support for nested script stages. (#229)
Co-authored-by: Tim Jacomb <[email protected]>
1 parent 91a48ab commit de98932

File tree

1 file changed

+79
-13
lines changed

1 file changed

+79
-13
lines changed

src/main/java/io/jenkins/plugins/pipelinegraphview/utils/PipelineNodeGraphVisitor.java

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public class PipelineNodeGraphVisitor extends StandardChunkVisitor {
7373

7474
private static final Logger logger = LoggerFactory.getLogger(PipelineNodeGraphVisitor.class);
7575

76-
private final boolean isNodeVisitorDumpEnabled =
77-
Boolean.getBoolean("NODE-DUMP-ENABLED") && logger.isDebugEnabled();
76+
private final boolean isNodeVisitorDumpEnabled = logger.isTraceEnabled();
7877

7978
private final Stack<FlowNode> nestedStages = new Stack<>();
8079
private final Stack<FlowNode> nestedbranches = new Stack<>();
@@ -146,7 +145,6 @@ public void chunkStart(
146145
public void chunkEnd(
147146
@NonNull FlowNode endNode, @CheckForNull FlowNode afterBlock, @NonNull ForkScanner scanner) {
148147
super.chunkEnd(endNode, afterBlock, scanner);
149-
150148
if (isNodeVisitorDumpEnabled) {
151149
dump(
152150
String.format(
@@ -199,7 +197,6 @@ public void chunkEnd(
199197
"chunk.getLastNode() is marked non null but is null sometimes, when JENKINS-40200 is fixed we will remove this check ")
200198
@Override
201199
protected void handleChunkDone(@NonNull MemoryFlowChunk chunk) {
202-
203200
if (isNodeVisitorDumpEnabled) {
204201
dump(
205202
String.format(
@@ -216,14 +213,6 @@ protected void handleChunkDone(@NonNull MemoryFlowChunk chunk) {
216213
parallelNestedStages = true;
217214
}
218215

219-
if (!nestedStages.empty()) {
220-
FlowNode discarded = nestedStages.pop(); // we throw away first nested stage
221-
// nested stages not supported in scripted pipeline.
222-
if (!nestedStages.isEmpty() && !isDeclarative()) {
223-
return;
224-
}
225-
}
226-
227216
TimingInfo times = null;
228217

229218
// TODO: remove chunk.getLastNode() != null check based on how JENKINS-40200 gets resolved
@@ -289,6 +278,17 @@ protected void handleChunkDone(@NonNull MemoryFlowChunk chunk) {
289278
Iterator<FlowNodeWrapper> branches = parallelBranches.descendingIterator();
290279
while (branches.hasNext()) {
291280
FlowNodeWrapper p = branches.next();
281+
if (isNodeVisitorDumpEnabled) {
282+
dump(
283+
String.format(
284+
"handleChunkDone=> found node [id: %s, name: %s, function: %s] is child of [id: %s, name: %s, function: %s] - parallelBranches",
285+
p.getId(),
286+
p.getDisplayName(),
287+
p.getNode().getDisplayFunctionName(),
288+
stage.getId(),
289+
stage.getDisplayName(),
290+
stage.getNode().getDisplayFunctionName()));
291+
}
292292
p.addParent(stage);
293293
stage.addEdge(p);
294294
}
@@ -303,16 +303,41 @@ protected void handleChunkDone(@NonNull MemoryFlowChunk chunk) {
303303
}
304304
}
305305
if (nextStage != null && !parallelNestedStages) {
306+
if (isNodeVisitorDumpEnabled) {
307+
dump(
308+
String.format(
309+
"handleChunkDone=> found node [id: %s, name: %s, function: %s] is child of [id: %s, name: %s, function: %s]",
310+
nextStage.getId(),
311+
nextStage.getDisplayName(),
312+
nextStage.getNode().getDisplayFunctionName(),
313+
stage.getId(),
314+
stage.getDisplayName(),
315+
stage.getNode().getDisplayFunctionName()));
316+
}
306317
nextStage.addParent(stage);
307318
stage.addEdge(nextStage);
308319
}
320+
if (nextStage == null) {
321+
if (isNodeVisitorDumpEnabled) {
322+
dump(
323+
String.format(
324+
"handleChunkDone=> WARNING: nextStage is null! Unable for assign parent stage for stage [id: %s, name: %s, function: %s]",
325+
stage.getId(), stage.getDisplayName(), stage.getNode().getDisplayFunctionName()));
326+
}
327+
}
309328
for (FlowNodeWrapper p : parallelBranches) {
310329
nodes.remove(p);
311330
nodeMap.remove(p.getId(), p);
312331
}
313332
}
314333
parallelBranches.clear();
315334
if (!parallelNestedStages) {
335+
if (isNodeVisitorDumpEnabled) {
336+
dump(
337+
String.format(
338+
"handleChunkDone=> setting nextStage to: [id: %s, name: %s, function: %s]",
339+
stage.getId(), stage.getDisplayName(), stage.getNode().getDisplayFunctionName()));
340+
}
316341
this.nextStage = stage;
317342
}
318343
}
@@ -462,6 +487,17 @@ public void parallelStart(
462487
if (isNodeVisitorDumpEnabled) {
463488
dump("\t\tNested labelling stage detected");
464489
}
490+
if (isNodeVisitorDumpEnabled) {
491+
dump(
492+
String.format(
493+
"parallelStart=> found node [id: %s, name: %s, function: %s] is child of [id: %s, name: %s, function: %s] - declarative",
494+
firstNodeWrapper.getId(),
495+
firstNodeWrapper.getDisplayName(),
496+
firstNodeWrapper.getNode().getDisplayFunctionName(),
497+
branch.getId(),
498+
branch.getDisplayName(),
499+
branch.getNode().getDisplayFunctionName()));
500+
}
465501
branch.addEdge(firstNodeWrapper);
466502
firstNodeWrapper.addParent(branch);
467503
nodes.add(firstNodeWrapper);
@@ -473,6 +509,17 @@ public void parallelStart(
473509
while (!stack.isEmpty()) {
474510
// Grab next, link to prev, add to result
475511
FlowNodeWrapper currentStage = stack.pop();
512+
if (isNodeVisitorDumpEnabled) {
513+
dump(
514+
String.format(
515+
"parallelStart=> found node [id: %s, name: %s, function: %s] is child of [id: %s, name: %s, function: %s]",
516+
currentStage.getId(),
517+
currentStage.getDisplayName(),
518+
currentStage.getNode().getDisplayFunctionName(),
519+
previousNode.getId(),
520+
previousNode.getDisplayName(),
521+
previousNode.getNode().getDisplayFunctionName()));
522+
}
476523
previousNode.addEdge(currentStage);
477524
currentStage.addParent(previousNode);
478525
nodes.add(currentStage);
@@ -629,7 +676,7 @@ public void atomNode(
629676
}
630677

631678
private void dump(String str) {
632-
logger.info(System.identityHashCode(this) + ": " + str);
679+
logger.trace(System.identityHashCode(this) + ": " + str);
633680
}
634681

635682
/**
@@ -705,6 +752,14 @@ private void captureOrphanParallelBranches() {
705752
nodes.push(synStage);
706753
nodeMap.put(synStage.getId(), synStage);
707754
parallelBranches.clear();
755+
if (isNodeVisitorDumpEnabled) {
756+
dump(
757+
String.format(
758+
"captureOrphanParallelBranches=> setting nextStage to: [id: %s, name: %s, function: %s]",
759+
synStage.getId(),
760+
synStage.getDisplayName(),
761+
synStage.getNode().getDisplayFunctionName()));
762+
}
708763
this.nextStage = synStage;
709764
}
710765
}
@@ -799,6 +854,17 @@ protected String getTypeDisplayName() {
799854
Iterator<FlowNodeWrapper> sortedBranches = parallelBranches.descendingIterator();
800855
while (sortedBranches.hasNext()) {
801856
FlowNodeWrapper p = sortedBranches.next();
857+
if (isNodeVisitorDumpEnabled) {
858+
dump(
859+
String.format(
860+
"createParallelSyntheticNode=> found node [id: %s, name: %s, function: %s] is child of [id: %s, name: %s, function: %s]",
861+
p.getId(),
862+
p.getDisplayName(),
863+
p.getNode().getDisplayFunctionName(),
864+
synStage.getId(),
865+
synStage.getDisplayName(),
866+
synStage.getNode().getDisplayFunctionName()));
867+
}
802868
p.addParent(synStage);
803869
synStage.addEdge(p);
804870
}

0 commit comments

Comments
 (0)