Skip to content

Commit 5ba85b8

Browse files
authored
fix(pkg/jenkins): fix handling for fast-failing Jenkins jobs (#30)
Handle fast-failing Jenkins jobs by setting appropriate state and description based on build result (success, failure, aborted, or unknown). Set pending time and mark job as complete. Added corresponding test cases for each state.
1 parent dc47abc commit 5ba85b8

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

pkg/jenkins/controller.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,39 @@ func (c *Controller) syncTriggeredJob(pj prowapi.ProwJob, reports chan<- prowapi
461461
pj.Status.State = prowapi.PendingState
462462
pj.Status.Description = "Jenkins job running."
463463

464+
// Construct the status URL that will be used in reports.
465+
pj.Status.PodName = pj.ObjectMeta.Name
466+
pj.Status.BuildID = jb.BuildID()
467+
pj.Status.JenkinsBuildID = strconv.Itoa(jb.Number)
468+
var b bytes.Buffer
469+
if err := c.config().JobURLTemplate.Execute(&b, &pj); err != nil {
470+
c.log.WithFields(pjutil.ProwJobFields(&pj)).Errorf("error executing URL template: %v", err)
471+
} else {
472+
pj.Status.URL = b.String()
473+
}
474+
} else {
475+
// Build has already completed (fast-failing case). Handle the result immediately.
476+
pj.SetComplete()
477+
if pj.Status.PendingTime == nil {
478+
now := metav1.NewTime(c.clock.Now())
479+
pj.Status.PendingTime = &now
480+
}
481+
482+
switch {
483+
case jb.IsSuccess():
484+
pj.Status.State = prowapi.SuccessState
485+
pj.Status.Description = "Jenkins job succeeded."
486+
case jb.IsFailure():
487+
pj.Status.State = prowapi.FailureState
488+
pj.Status.Description = "Jenkins job failed."
489+
case jb.IsAborted():
490+
pj.Status.State = prowapi.AbortedState
491+
pj.Status.Description = "Jenkins job aborted."
492+
default:
493+
pj.Status.State = prowapi.ErrorState
494+
pj.Status.Description = "Jenkins job completed with unknown result."
495+
}
496+
464497
// Construct the status URL that will be used in reports.
465498
pj.Status.PodName = pj.ObjectMeta.Name
466499
pj.Status.BuildID = jb.BuildID()

pkg/jenkins/controller_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (f *fghc) EditCommentWithContext(_ context.Context, org, repo string, ID in
189189

190190
func TestSyncTriggeredJobs(t *testing.T) {
191191
fakeClock := clocktesting.NewFakeClock(time.Now().Truncate(1 * time.Second))
192+
referenceTime := fakeClock.Now()
192193

193194
var testcases = []struct {
194195
name string
@@ -295,6 +296,75 @@ func TestSyncTriggeredJobs(t *testing.T) {
295296
expectedEnqueued: true,
296297
expectedPendingTime: nil,
297298
},
299+
{
300+
name: "fast-failing job - failure state",
301+
pj: prowapi.ProwJob{
302+
ObjectMeta: metav1.ObjectMeta{
303+
Name: "fast-fail",
304+
Namespace: "prowjobs",
305+
},
306+
Spec: prowapi.ProwJobSpec{
307+
Type: prowapi.PostsubmitJob,
308+
Job: "test-job",
309+
},
310+
Status: prowapi.ProwJobStatus{
311+
State: prowapi.TriggeredState,
312+
},
313+
},
314+
builds: map[string]Build{
315+
"fast-fail": {Result: pState(failure), Number: 42},
316+
},
317+
expectedState: prowapi.FailureState,
318+
expectedComplete: true,
319+
expectedReport: true,
320+
expectedPendingTime: &metav1.Time{Time: referenceTime},
321+
},
322+
{
323+
name: "fast-failing job - success state",
324+
pj: prowapi.ProwJob{
325+
ObjectMeta: metav1.ObjectMeta{
326+
Name: "fast-success",
327+
Namespace: "prowjobs",
328+
},
329+
Spec: prowapi.ProwJobSpec{
330+
Type: prowapi.PostsubmitJob,
331+
Job: "test-job",
332+
},
333+
Status: prowapi.ProwJobStatus{
334+
State: prowapi.TriggeredState,
335+
},
336+
},
337+
builds: map[string]Build{
338+
"fast-success": {Result: pState(success), Number: 43},
339+
},
340+
expectedState: prowapi.SuccessState,
341+
expectedComplete: true,
342+
expectedReport: true,
343+
expectedPendingTime: &metav1.Time{Time: referenceTime},
344+
},
345+
{
346+
name: "fast-failing job - aborted state",
347+
pj: prowapi.ProwJob{
348+
ObjectMeta: metav1.ObjectMeta{
349+
Name: "fast-abort",
350+
Namespace: "prowjobs",
351+
},
352+
Spec: prowapi.ProwJobSpec{
353+
Type: prowapi.PostsubmitJob,
354+
Job: "test-job",
355+
},
356+
Status: prowapi.ProwJobStatus{
357+
State: prowapi.TriggeredState,
358+
},
359+
},
360+
builds: map[string]Build{
361+
"fast-abort": {Result: pState(aborted), Number: 44},
362+
},
363+
expectedState: prowapi.AbortedState,
364+
expectedComplete: true,
365+
expectedReport: true,
366+
expectedPendingTime: &metav1.Time{Time: referenceTime},
367+
},
298368
}
299369
for _, tc := range testcases {
300370
totServ := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)