Skip to content

Commit 38a8758

Browse files
authored
Look at the current build for resubmit instead of the previous (#508)
1 parent eb64798 commit 38a8758

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/main/java/com/amazon/jenkins/ec2fleet/EC2FleetAutoResubmitComputerLauncher.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,17 @@ public void afterDisconnect(final SlaveComputer computer, final TaskListener lis
102102

103103
final List<Action> actions = new ArrayList<>();
104104
if (task instanceof WorkflowJob) {
105-
final WorkflowRun lastUnsuccessfulBuild = ((WorkflowJob) task).getLastUnsuccessfulBuild();
106-
actions.addAll(lastUnsuccessfulBuild.getActions(ParametersAction.class));
105+
// Try to get the current running build first (which would be from the executable)
106+
WorkflowRun currentBuild = null;
107+
if (executable instanceof WorkflowRun) {
108+
currentBuild = (WorkflowRun) executable;
109+
} else {
110+
// Fallback to getting the last build (most recent)
111+
currentBuild = ((WorkflowJob) task).getLastBuild();
112+
}
113+
if (currentBuild != null) {
114+
actions.addAll(currentBuild.getActions(ParametersAction.class));
115+
}
107116
}
108117
if (executable instanceof Actionable) {
109118
actions.addAll(((Actionable) executable).getAllActions());

src/test/java/com/amazon/jenkins/ec2fleet/EC2FleetAutoResubmitComputerLauncherTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
@ExtendWith(MockitoExtension.class)
4242
@MockitoSettings(strictness = Strictness.LENIENT)
4343
class EC2FleetAutoResubmitComputerLauncherTest {
44+
@Test
45+
void taskCompleted_should_resubmit_task_with_current_workflowrun_executable() {
46+
// Setup: the current executable is a WorkflowRun, and the task is a WorkflowJob
47+
when(executor1.getCurrentExecutable()).thenReturn(workflowRun);
48+
when(workflowRun.getParent()).thenReturn(workflowJob);
49+
when(workflowJob.getOwnerTask()).thenReturn(workflowJob);
50+
when(workflowRun.getActions(any())).thenReturn(Collections.singletonList(action1));
51+
when(computer.getExecutors()).thenReturn(Arrays.asList(executor1));
52+
53+
new EC2FleetAutoResubmitComputerLauncher(baseComputerLauncher)
54+
.afterDisconnect(computer, taskListener);
55+
56+
verify(queue).schedule2(eq(workflowJob), anyInt(), eq(Arrays.asList(action1)));
57+
verify(workflowRun, times(1)).getActions(any());
58+
}
4459

4560
private MockedStatic<Jenkins> mockedJenkins;
4661

@@ -202,8 +217,8 @@ void taskCompleted_should_resubmit_task_with_actions() {
202217

203218
@Test
204219
void taskCompleted_should_resubmit_task_with_build_actions() {
205-
when(subTask1.getOwnerTask()).thenReturn(workflowJob);
206-
when(workflowJob.getLastUnsuccessfulBuild()).thenReturn(workflowRun);
220+
when(subTask1.getOwnerTask()).thenReturn(workflowJob);
221+
when(workflowJob.getLastBuild()).thenReturn(workflowRun);
207222
when(workflowRun.getActions(any())).thenReturn((Collections.singletonList(action1)));
208223
when(computer.getExecutors()).thenReturn(Arrays.asList(executor1));
209224
new EC2FleetAutoResubmitComputerLauncher(baseComputerLauncher)

0 commit comments

Comments
 (0)