@@ -299,6 +299,189 @@ func TestUpdatePipelineRunState(t *testing.T) {
299
299
300
300
assert .Equal (t , updatedPR .Annotations [keys .State ], tt .state )
301
301
assert .Equal (t , updatedPR .Spec .Status , tektonv1 .PipelineRunSpecStatus ("" ))
302
+
303
+ // Test SCMReportingPLRStarted annotation for started state
304
+ if tt .state == kubeinteraction .StateStarted {
305
+ scmStarted , exists := updatedPR .GetAnnotations ()[keys .SCMReportingPLRStarted ]
306
+ assert .Assert (t , exists , "SCMReportingPLRStarted annotation should exist when state is started" )
307
+ assert .Equal (t , scmStarted , "true" , "SCMReportingPLRStarted should be 'true'" )
308
+ } else {
309
+ _ , exists := updatedPR .GetAnnotations ()[keys .SCMReportingPLRStarted ]
310
+ assert .Assert (t , ! exists , "SCMReportingPLRStarted annotation should not exist for non-started states" )
311
+ }
312
+ })
313
+ }
314
+ }
315
+
316
+ func TestReconcileKind_SCMReportingLogic (t * testing.T ) {
317
+ observer , _ := zapobserver .New (zap .InfoLevel )
318
+ _ = zap .New (observer ).Sugar ()
319
+
320
+ tests := []struct {
321
+ name string
322
+ pipelineRun * tektonv1.PipelineRun
323
+ shouldCallUpdateToInProgress bool
324
+ description string
325
+ }{
326
+ {
327
+ name : "Running reason without SCMReportingPLRStarted - should call updatePipelineRunToInProgress" ,
328
+ pipelineRun : & tektonv1.PipelineRun {
329
+ ObjectMeta : metav1.ObjectMeta {
330
+ Namespace : "test" ,
331
+ Name : "test-pr" ,
332
+ Annotations : map [string ]string {
333
+ keys .State : kubeinteraction .StateQueued ,
334
+ keys .Repository : "test-repo" ,
335
+ },
336
+ },
337
+ Spec : tektonv1.PipelineRunSpec {},
338
+ Status : tektonv1.PipelineRunStatus {
339
+ Status : knativeduckv1.Status {
340
+ Conditions : knativeduckv1.Conditions {
341
+ {
342
+ Type : knativeapi .ConditionSucceeded ,
343
+ Status : corev1 .ConditionUnknown ,
344
+ Reason : string (tektonv1 .PipelineRunReasonRunning ),
345
+ },
346
+ },
347
+ },
348
+ },
349
+ },
350
+ shouldCallUpdateToInProgress : true ,
351
+ description : "PipelineRun is Running but no SCM reporting done yet" ,
352
+ },
353
+ {
354
+ name : "Running reason with SCMReportingPLRStarted=true - should NOT call updatePipelineRunToInProgress" ,
355
+ pipelineRun : & tektonv1.PipelineRun {
356
+ ObjectMeta : metav1.ObjectMeta {
357
+ Namespace : "test" ,
358
+ Name : "test-pr" ,
359
+ Annotations : map [string ]string {
360
+ keys .State : kubeinteraction .StateStarted ,
361
+ keys .Repository : "test-repo" ,
362
+ keys .SCMReportingPLRStarted : "true" ,
363
+ },
364
+ },
365
+ Spec : tektonv1.PipelineRunSpec {},
366
+ Status : tektonv1.PipelineRunStatus {
367
+ Status : knativeduckv1.Status {
368
+ Conditions : knativeduckv1.Conditions {
369
+ {
370
+ Type : knativeapi .ConditionSucceeded ,
371
+ Status : corev1 .ConditionUnknown ,
372
+ Reason : string (tektonv1 .PipelineRunReasonRunning ),
373
+ },
374
+ },
375
+ },
376
+ },
377
+ },
378
+ shouldCallUpdateToInProgress : false ,
379
+ description : "PipelineRun is Running and SCM reporting already done" ,
380
+ },
381
+ {
382
+ name : "Non-Running reason - should NOT call updatePipelineRunToInProgress" ,
383
+ pipelineRun : & tektonv1.PipelineRun {
384
+ ObjectMeta : metav1.ObjectMeta {
385
+ Namespace : "test" ,
386
+ Name : "test-pr" ,
387
+ Annotations : map [string ]string {
388
+ keys .State : kubeinteraction .StateQueued ,
389
+ keys .Repository : "test-repo" ,
390
+ },
391
+ },
392
+ Spec : tektonv1.PipelineRunSpec {
393
+ Status : tektonv1 .PipelineRunSpecStatusPending ,
394
+ },
395
+ Status : tektonv1.PipelineRunStatus {
396
+ Status : knativeduckv1.Status {
397
+ Conditions : knativeduckv1.Conditions {
398
+ {
399
+ Type : knativeapi .ConditionSucceeded ,
400
+ Status : corev1 .ConditionUnknown ,
401
+ Reason : string (tektonv1 .PipelineRunReasonPending ),
402
+ },
403
+ },
404
+ },
405
+ },
406
+ },
407
+ shouldCallUpdateToInProgress : false ,
408
+ description : "PipelineRun is not in Running state" ,
409
+ },
410
+ }
411
+
412
+ for _ , tt := range tests {
413
+ t .Run (tt .name , func (t * testing.T ) {
414
+ ctx , _ := rtesting .SetupFakeContext (t )
415
+
416
+ testRepo := & v1alpha1.Repository {
417
+ ObjectMeta : metav1.ObjectMeta {
418
+ Name : "test-repo" ,
419
+ Namespace : tt .pipelineRun .GetNamespace (),
420
+ },
421
+ Spec : v1alpha1.RepositorySpec {
422
+ URL : randomURL ,
423
+ },
424
+ }
425
+
426
+ testData := testclient.Data {
427
+ Repositories : []* v1alpha1.Repository {testRepo },
428
+ PipelineRuns : []* tektonv1.PipelineRun {tt .pipelineRun },
429
+ }
430
+ stdata , informers := testclient .SeedTestData (t , ctx , testData )
431
+
432
+ // Track if updatePipelineRunToInProgress was called by checking state changes
433
+ originalState := tt .pipelineRun .GetAnnotations ()[keys .State ]
434
+
435
+ r := & Reconciler {
436
+ repoLister : informers .Repository .Lister (),
437
+ run : & params.Run {
438
+ Clients : clients.Clients {
439
+ Tekton : stdata .Pipeline ,
440
+ },
441
+ Info : info.Info {
442
+ Pac : & info.PacOpts {
443
+ Settings : settings.Settings {},
444
+ },
445
+ },
446
+ },
447
+ }
448
+
449
+ err := r .ReconcileKind (ctx , tt .pipelineRun )
450
+
451
+ // For test cases that should call updatePipelineRunToInProgress,
452
+ // we expect no error and the state should be updated
453
+ if tt .shouldCallUpdateToInProgress {
454
+ assert .NilError (t , err , tt .description )
455
+
456
+ // Get the updated PipelineRun to check if state was changed
457
+ updatedPR , getErr := stdata .Pipeline .TektonV1 ().PipelineRuns (tt .pipelineRun .GetNamespace ()).Get (ctx , tt .pipelineRun .GetName (), metav1.GetOptions {})
458
+ assert .NilError (t , getErr )
459
+
460
+ // Should have been updated to started state with SCMReportingPLRStarted annotation
461
+ assert .Equal (t , updatedPR .GetAnnotations ()[keys .State ], kubeinteraction .StateStarted )
462
+ scmStarted , exists := updatedPR .GetAnnotations ()[keys .SCMReportingPLRStarted ]
463
+ assert .Assert (t , exists , "SCMReportingPLRStarted should be set" )
464
+ assert .Equal (t , scmStarted , "true" )
465
+ } else {
466
+ // For cases that should NOT call updatePipelineRunToInProgress,
467
+ // the state should remain unchanged
468
+ updatedPR , getErr := stdata .Pipeline .TektonV1 ().PipelineRuns (tt .pipelineRun .GetNamespace ()).Get (ctx , tt .pipelineRun .GetName (), metav1.GetOptions {})
469
+ assert .NilError (t , getErr )
470
+
471
+ // State should be unchanged
472
+ assert .Equal (t , updatedPR .GetAnnotations ()[keys .State ], originalState , tt .description )
473
+
474
+ // Check SCMReportingPLRStarted annotation based on original state
475
+ scmStarted , exists := updatedPR .GetAnnotations ()[keys .SCMReportingPLRStarted ]
476
+ if originalState == kubeinteraction .StateStarted {
477
+ // If original state was already 'started', SCMReportingPLRStarted should exist and be "true"
478
+ assert .Assert (t , exists , "SCMReportingPLRStarted should exist when original state is started" )
479
+ assert .Equal (t , scmStarted , "true" , "SCMReportingPLRStarted should be 'true'" )
480
+ } else {
481
+ // If original state was not 'started', SCMReportingPLRStarted should not exist
482
+ assert .Assert (t , ! exists , "SCMReportingPLRStarted should not exist when original state is not started" )
483
+ }
484
+ }
302
485
})
303
486
}
304
487
}
0 commit comments