Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle no stage and Jenkins step error #363

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
private final boolean declarative;

private static final Logger logger = LoggerFactory.getLogger(PipelineNodeTreeScanner.class);
private boolean isDebugEnabled = logger.isDebugEnabled();
private final boolean isDebugEnabled = logger.isDebugEnabled();

public PipelineNodeTreeScanner(@NonNull WorkflowRun run) {
this.run = run;
Expand Down Expand Up @@ -126,6 +126,11 @@
for (String stageId : stageNodeMap.keySet()) {
stageNodeStepMap.put(stageId, getStageSteps(stageId));
}

if (stageNodeStepMap.isEmpty()) {

Check warning on line 130 in src/main/java/io/jenkins/plugins/pipelinegraphview/treescanner/PipelineNodeTreeScanner.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 130 is only partially covered, one branch is missing
stageNodeStepMap.put("(no-stage)", new ArrayList<>(stepNodeMap.values()));

Check warning on line 131 in src/main/java/io/jenkins/plugins/pipelinegraphview/treescanner/PipelineNodeTreeScanner.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 131 is not covered by tests
}
Comment on lines +130 to +132
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't actually effect the issue like I thought, but it does handle pipelines with no stages nicely


return stageNodeStepMap;
}

Expand Down Expand Up @@ -287,11 +292,11 @@
dump("Remapping steps");
Map<String, FlowNodeWrapper> stepMap = this.wrappedNodeMap.entrySet().stream()
.filter(e -> shouldBeInStepMap(e.getValue()))
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Map<String, FlowNodeWrapper> stageMap = this.getStageMapping();
List<FlowNodeWrapper> nodeList = new ArrayList<FlowNodeWrapper>(stepMap.values());
Collections.sort(nodeList, new FlowNodeWrapper.NodeComparator());
nodeList.sort(new FlowNodeWrapper.NodeComparator());
for (FlowNodeWrapper step : nodeList) {
FlowNodeWrapper firstParent = step.getFirstParent();
// Remap parentage of steps that aren't children of stages (e.g. are in Step
Expand Down Expand Up @@ -321,13 +326,15 @@
* Builds a graph from the list of nodes and relationships given to the class.
*/
private void buildGraph() {
List<FlowNode> nodeList = new ArrayList<FlowNode>(nodeMap.values());
Collections.sort(nodeList, new FlowNodeWrapper.FlowNodeComparator());
List<FlowNode> nodeList = new ArrayList<>(nodeMap.values());
nodeList.sort(new FlowNodeWrapper.FlowNodeComparator());
// If the Pipeline ended with an unhandled exception, then we want to catch the
// node which threw it.
BlockEndNode<?> nodeThatThrewException = null;
if (!nodeList.isEmpty()) {
nodeThatThrewException = getUnhandledException(nodeList.get(nodeList.size() - 1));
boolean hasStage = nodeList.stream().anyMatch(PipelineNodeUtil::isStage);

nodeThatThrewException = getUnhandledException(nodeList.get(nodeList.size() - 1), hasStage);
}
for (FlowNode node : nodeList) {
if (nodeThatThrewException == node) {
Expand All @@ -352,12 +359,15 @@
* Returns the origin of any unhandled exception for this node, or null if none
* found.
*/
private @CheckForNull BlockEndNode<?> getUnhandledException(@NonNull FlowNode node) {
private @CheckForNull BlockEndNode<?> getUnhandledException(@NonNull FlowNode node, boolean hasStage) {
// Check for an unhandled exception.
ErrorAction errorAction = node.getAction(ErrorAction.class);
// If this is a Jenkins failure exception, then we don't need to add a new node
// - it will come from an existing step.
if (errorAction != null && !PipelineNodeUtil.isJenkinsFailureException(errorAction.getError())) {
if (errorAction != null) {
// If this is a Jenkins failure exception, then we don't need to add a new node
// - it will come from an existing step if there is a stage for the step to be part of.
if (hasStage && PipelineNodeUtil.isJenkinsFailureException(errorAction.getError())) {
return null;
}
dump(
"getUnhandledException => Found unhandled exception: %s",
errorAction.getError().getMessage());
Expand Down