Skip to content

Commit b5b2029

Browse files
authored
Fix tree representation for scripted parallel stages (#73)
1 parent 5c5ed67 commit b5b2029

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ public PipelineGraph createTree() {
177177
// If a node encloses another node, it means it's a tree parent, so the first ancestor
178178
// ID we find
179179
// which matches an enclosing node then it's the stages tree parent.
180+
List<String> enclosingIds = stageNode.getAllEnclosingIds();
180181
for (String ancestorId : ancestors) {
181-
if (stageNode.getAllEnclosingIds().contains(ancestorId)) {
182+
if (enclosingIds.contains(ancestorId)) {
182183
treeParentId = ancestorId;
183184
break;
184185
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -735,18 +735,20 @@ private void captureOrphanParallelBranches() {
735735
firstBranch.getId(), (parallel == null ? "(none)" : parallel.getId())));
736736
}
737737

738-
String firstNodeId = firstBranch.getId();
738+
String syntheticNodeId =
739+
firstBranch.getNode().getParents().stream()
740+
.map((node) -> node.getId())
741+
.findFirst()
742+
.orElseGet(
743+
() -> createSyntheticStageId(firstBranch.getId(), PARALLEL_SYNTHETIC_STAGE_NAME));
739744
List<FlowNode> parents;
740745
if (parallel != null) {
741746
parents = parallel.getNode().getParents();
742747
} else {
743748
parents = new ArrayList<>();
744749
}
745750
FlowNode syntheticNode =
746-
new FlowNode(
747-
firstBranch.getNode().getExecution(),
748-
createSyntheticStageId(firstNodeId, PARALLEL_SYNTHETIC_STAGE_NAME),
749-
parents) {
751+
new FlowNode(firstBranch.getNode().getExecution(), syntheticNodeId, parents) {
750752
@Override
751753
public void save() throws IOException {
752754
// no-op to avoid JENKINS-45892 violations from serializing the synthetic FlowNode.

src/test/java/io/jenkins/plugins/pipelinegraphview/utils/PipelineGraphApiTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,18 @@ public void createTree_complexSmokes() throws Exception {
132132
"Skipped stage,",
133133
"Parallel Stage 2[Branch A,Branch B,Branch C[Nested 1,Nested 2]]")));
134134
}
135+
136+
@Test
137+
public void createTree_scriptedParallel() throws Exception {
138+
WorkflowRun run =
139+
TestUtils.createAndRunJob(
140+
j, "scriptedParallel", "scriptedParallel.jenkinsfile", Result.SUCCESS);
141+
PipelineGraphApi api = new PipelineGraphApi(run);
142+
PipelineGraph graph = api.createTree();
143+
144+
List<PipelineStage> stages = graph.getStages();
145+
146+
String stagesString = TestUtils.collectStagesAsString(stages, PipelineStage::getName);
147+
assertThat(stagesString, is("A,Parallel[B[BA,BB],C[CA,CB]],D[E[EA,EB],F[FA,FB]],G"));
148+
}
135149
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
stage('A') {
2+
echo ''
3+
}
4+
parallel(
5+
B: {
6+
stage('BA') {
7+
echo ''
8+
}
9+
stage('BB') {
10+
echo ''
11+
}
12+
},
13+
C: {
14+
stage('CA') {
15+
echo ''
16+
}
17+
stage('CB') {
18+
echo ''
19+
}
20+
},
21+
)
22+
stage('D') {
23+
parallel(
24+
E: {
25+
stage('EA') {
26+
echo ''
27+
}
28+
stage('EB') {
29+
echo ''
30+
}
31+
},
32+
F: {
33+
stage('FA') {
34+
echo ''
35+
}
36+
stage('FB') {
37+
echo ''
38+
}
39+
},
40+
)
41+
}
42+
stage('G') {
43+
echo ''
44+
}

0 commit comments

Comments
 (0)