Skip to content

Commit 8959b75

Browse files
authored
Revert "Return root stages if they have child steps." (#380)
1 parent 8d6eedb commit 8959b75

File tree

1 file changed

+16
-44
lines changed

1 file changed

+16
-44
lines changed

src/main/java/io/jenkins/plugins/pipelinegraphview/treescanner/PipelineNodeTreeScanner.java

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class PipelineNodeTreeScanner {
4848
private final boolean declarative;
4949

5050
private static final Logger logger = LoggerFactory.getLogger(PipelineNodeTreeScanner.class);
51-
private final boolean isDebugEnabled = logger.isDebugEnabled();
51+
private boolean isDebugEnabled = logger.isDebugEnabled();
5252

5353
public PipelineNodeTreeScanner(@NonNull WorkflowRun run) {
5454
this.run = run;
@@ -126,7 +126,6 @@ public Map<String, List<FlowNodeWrapper>> getAllSteps() {
126126
for (String stageId : stageNodeMap.keySet()) {
127127
stageNodeStepMap.put(stageId, getStageSteps(stageId));
128128
}
129-
130129
return stageNodeStepMap;
131130
}
132131

@@ -208,23 +207,16 @@ protected List<FlowNodeWrapper> getNodes() {
208207
return this.wrappedStageMap;
209208
}
210209
dump("Remapping stages");
211-
// Find any root stages (ones without parents) that have steps - we want to return these.
212-
// This should only be FlowNodeStart nodes, but I want to be flexible - if a Stage has steps then the
213-
// step view should display it.
214-
// NOTE: In instances where this isn't the FlowNodeStart, this might add an unexpected FlowNode to the graph
215-
// view.
216-
List<FlowNodeWrapper> rootStagesWithSteps = getStagesWithChildSteps().stream()
217-
.filter(s -> s.getFirstParent() == null)
218-
.collect(Collectors.toList());
219210
Map<String, FlowNodeWrapper> stageMap = this.wrappedNodeMap.entrySet().stream()
220-
.filter(e -> rootStagesWithSteps.contains(e.getValue()) || shouldBeInStageMap(e.getValue()))
211+
.filter(e -> shouldBeInStageMap(e.getValue()))
221212
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
222213
List<FlowNodeWrapper> nodeList = new ArrayList<FlowNodeWrapper>(stageMap.values());
223214
Collections.sort(nodeList, new FlowNodeWrapper.NodeComparator());
224215
for (FlowNodeWrapper stage : nodeList) {
225216
FlowNodeWrapper firstParent = stage.getFirstParent();
226217
// Remap parentage of stages that aren't children of stages (e.g. allocate node
227218
// step).
219+
dump("Stages has %s parents", stage.getParents().size());
228220
dump("First parent of stage %s: %s", stage.getId(), firstParent);
229221
if (firstParent != null) {
230222
dump("Parent exists in stage map: %s", stageMap.containsKey(firstParent.getId()));
@@ -237,24 +229,6 @@ protected List<FlowNodeWrapper> getNodes() {
237229
return this.wrappedStageMap;
238230
}
239231

240-
/* Filter wrappedNodes to get list of steps.
241-
*/
242-
private Map<String, FlowNodeWrapper> getSteps() {
243-
return this.wrappedNodeMap.entrySet().stream()
244-
.filter(e -> shouldBeInStepMap(e.getValue()))
245-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
246-
}
247-
248-
/* Filter wrappedNodes to get list of steps.
249-
*/
250-
private List<FlowNodeWrapper> getStagesWithChildSteps() {
251-
return getSteps().entrySet().stream()
252-
.map(e -> e.getValue().getFirstParent())
253-
.filter(p -> p != null)
254-
.map(p -> wrappedNodeMap.get(p.getId()))
255-
.collect(Collectors.toList());
256-
}
257-
258232
private boolean shouldBeInStageMap(FlowNodeWrapper n) {
259233
// We also want to drop steps blocks - as the front-end doesn't expect them.
260234
// For the future: Adding Step Blocks as stages might be a good way to handle them in the
@@ -311,10 +285,13 @@ private boolean isSuperfluousStartNode(FlowNodeWrapper n) {
311285
}
312286

313287
dump("Remapping steps");
314-
Map<String, FlowNodeWrapper> stepMap = getSteps();
288+
Map<String, FlowNodeWrapper> stepMap = this.wrappedNodeMap.entrySet().stream()
289+
.filter(e -> shouldBeInStepMap(e.getValue()))
290+
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
291+
315292
Map<String, FlowNodeWrapper> stageMap = this.getStageMapping();
316293
List<FlowNodeWrapper> nodeList = new ArrayList<FlowNodeWrapper>(stepMap.values());
317-
nodeList.sort(new FlowNodeWrapper.NodeComparator());
294+
Collections.sort(nodeList, new FlowNodeWrapper.NodeComparator());
318295
for (FlowNodeWrapper step : nodeList) {
319296
FlowNodeWrapper firstParent = step.getFirstParent();
320297
// Remap parentage of steps that aren't children of stages (e.g. are in Step
@@ -344,15 +321,13 @@ private boolean isExceptionStep(FlowNodeWrapper n) {
344321
* Builds a graph from the list of nodes and relationships given to the class.
345322
*/
346323
private void buildGraph() {
347-
List<FlowNode> nodeList = new ArrayList<>(nodeMap.values());
348-
nodeList.sort(new FlowNodeWrapper.FlowNodeComparator());
324+
List<FlowNode> nodeList = new ArrayList<FlowNode>(nodeMap.values());
325+
Collections.sort(nodeList, new FlowNodeWrapper.FlowNodeComparator());
349326
// If the Pipeline ended with an unhandled exception, then we want to catch the
350327
// node which threw it.
351328
BlockEndNode<?> nodeThatThrewException = null;
352329
if (!nodeList.isEmpty()) {
353-
boolean hasStage = nodeList.stream().anyMatch(PipelineNodeUtil::isStage);
354-
355-
nodeThatThrewException = getUnhandledException(nodeList.get(nodeList.size() - 1), hasStage);
330+
nodeThatThrewException = getUnhandledException(nodeList.get(nodeList.size() - 1));
356331
}
357332
for (FlowNode node : nodeList) {
358333
if (nodeThatThrewException == node) {
@@ -377,15 +352,12 @@ private void buildGraph() {
377352
* Returns the origin of any unhandled exception for this node, or null if none
378353
* found.
379354
*/
380-
private @CheckForNull BlockEndNode<?> getUnhandledException(@NonNull FlowNode node, boolean hasStage) {
355+
private @CheckForNull BlockEndNode<?> getUnhandledException(@NonNull FlowNode node) {
381356
// Check for an unhandled exception.
382357
ErrorAction errorAction = node.getAction(ErrorAction.class);
383-
if (errorAction != null) {
384-
// If this is a Jenkins failure exception, then we don't need to add a new node
385-
// - it will come from an existing step if there is a stage for the step to be part of.
386-
if (hasStage && PipelineNodeUtil.isJenkinsFailureException(errorAction.getError())) {
387-
return null;
388-
}
358+
// If this is a Jenkins failure exception, then we don't need to add a new node
359+
// - it will come from an existing step.
360+
if (errorAction != null && !PipelineNodeUtil.isJenkinsFailureException(errorAction.getError())) {
389361
dump(
390362
"getUnhandledException => Found unhandled exception: %s",
391363
errorAction.getError().getMessage());
@@ -400,7 +372,7 @@ private void buildGraph() {
400372
/*
401373
* This is a corner case for trivial graphs - ones that only have one action. In
402374
* this case the error can be thrown by a the FlowStartNode, which would mean a
403-
* single node needs to be a stage and a step. Rather than adding a fake node
375+
* single nodes needs to be a stage and a step. Rather than adding a fake node
404376
* to the graph, we use the end node that we were given to act as the step
405377
* - this might need additional logic when getting the log for the exception.
406378
*/

0 commit comments

Comments
 (0)