@@ -24,6 +24,8 @@ import (
24
24
"github.com/openshift-pipelines/pipelines-as-code/pkg/params"
25
25
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/clients"
26
26
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
27
+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
28
+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype"
27
29
testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
28
30
ghtesthelper "github.com/openshift-pipelines/pipelines-as-code/pkg/test/github"
29
31
"github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
@@ -1304,3 +1306,187 @@ func TestCreateComment(t *testing.T) {
1304
1306
})
1305
1307
}
1306
1308
}
1309
+
1310
+ func TestSkipPushEventForPRCommits (t * testing.T ) {
1311
+ iid := int64 (1234 )
1312
+ tests := []struct {
1313
+ name string
1314
+ pacInfoEnabled bool
1315
+ pushEvent * github.PushEvent
1316
+ mockAPIs map [string ]func (rw http.ResponseWriter , r * http.Request )
1317
+ isPartOfPR bool
1318
+ wantErr bool
1319
+ wantErrContains string
1320
+ skipWarnLogContains string
1321
+ }{
1322
+ {
1323
+ name : "skip push event when commit is part of an open PR" ,
1324
+ pacInfoEnabled : true ,
1325
+ pushEvent : & github.PushEvent {
1326
+ Repo : & github.PushEventRepository {
1327
+ Name : github .Ptr ("testRepo" ),
1328
+ Owner : & github.User {Login : github .Ptr ("testOrg" )},
1329
+ },
1330
+ HeadCommit : & github.HeadCommit {
1331
+ ID : github .Ptr ("abc123" ),
1332
+ },
1333
+ },
1334
+ mockAPIs : map [string ]func (rw http.ResponseWriter , r * http.Request ){
1335
+ "/repos/testOrg/testRepo/commits/abc123/pulls" : func (rw http.ResponseWriter , r * http.Request ) {
1336
+ assert .Equal (t , r .Method , http .MethodGet )
1337
+ fmt .Fprint (rw , `[{"number": 42, "state": "open"}]` )
1338
+ },
1339
+ },
1340
+ isPartOfPR : true ,
1341
+ wantErr : true ,
1342
+ wantErrContains : "commit abc123 is part of pull request #42, skipping push event" ,
1343
+ },
1344
+ {
1345
+ name : "continue processing push event when commit is not part of PR" ,
1346
+ pacInfoEnabled : true ,
1347
+ pushEvent : & github.PushEvent {
1348
+ Repo : & github.PushEventRepository {
1349
+ Name : github .Ptr ("testRepo" ),
1350
+ Owner : & github.User {Login : github .Ptr ("testOrg" )},
1351
+ DefaultBranch : github .Ptr ("main" ),
1352
+ HTMLURL : github .Ptr ("https://github.com/testOrg/testRepo" ),
1353
+ ID : github .Ptr (iid ),
1354
+ },
1355
+ HeadCommit : & github.HeadCommit {
1356
+ ID : github .Ptr ("abc123" ),
1357
+ URL : github .Ptr ("https://github.com/testOrg/testRepo/commit/abc123" ),
1358
+ Message : github .Ptr ("Test commit message" ),
1359
+ },
1360
+ Ref : github .Ptr ("refs/heads/main" ),
1361
+ Sender : & github.User {Login : github .Ptr ("testUser" )},
1362
+ },
1363
+ mockAPIs : map [string ]func (rw http.ResponseWriter , r * http.Request ){
1364
+ "/repos/testOrg/testRepo/pulls" : func (rw http.ResponseWriter , r * http.Request ) {
1365
+ assert .Equal (t , r .Method , http .MethodGet )
1366
+ assert .Equal (t , r .URL .Query ().Get ("state" ), "open" )
1367
+ fmt .Fprint (rw , `[{"number": 42}]` )
1368
+ },
1369
+ "/repos/testOrg/testRepo/pulls/42/commits" : func (rw http.ResponseWriter , r * http.Request ) {
1370
+ assert .Equal (t , r .Method , http .MethodGet )
1371
+ fmt .Fprint (rw , `[{"sha": "def456"}, {"sha": "xyz789"}]` )
1372
+ },
1373
+ },
1374
+ isPartOfPR : false ,
1375
+ wantErr : false ,
1376
+ },
1377
+ {
1378
+ name : "continue when skip feature is disabled" ,
1379
+ pacInfoEnabled : false ,
1380
+ pushEvent : & github.PushEvent {
1381
+ Repo : & github.PushEventRepository {
1382
+ Name : github .Ptr ("testRepo" ),
1383
+ Owner : & github.User {Login : github .Ptr ("testOrg" )},
1384
+ DefaultBranch : github .Ptr ("main" ),
1385
+ HTMLURL : github .Ptr ("https://github.com/testOrg/testRepo" ),
1386
+ ID : github .Ptr (iid ),
1387
+ },
1388
+ HeadCommit : & github.HeadCommit {
1389
+ ID : github .Ptr ("abc123" ),
1390
+ URL : github .Ptr ("https://github.com/testOrg/testRepo/commit/abc123" ),
1391
+ Message : github .Ptr ("Test commit message" ),
1392
+ },
1393
+ Ref : github .Ptr ("refs/heads/main" ),
1394
+ Sender : & github.User {Login : github .Ptr ("testUser" )},
1395
+ },
1396
+ isPartOfPR : false , // This should not be checked when feature is disabled
1397
+ wantErr : false ,
1398
+ },
1399
+ {
1400
+ name : "log warning when API error occurs" ,
1401
+ pacInfoEnabled : true ,
1402
+ pushEvent : & github.PushEvent {
1403
+ Repo : & github.PushEventRepository {
1404
+ Name : github .Ptr ("testRepo" ),
1405
+ Owner : & github.User {Login : github .Ptr ("testOrg" )},
1406
+ },
1407
+ HeadCommit : & github.HeadCommit {
1408
+ ID : github .Ptr ("1234" ),
1409
+ },
1410
+ },
1411
+ mockAPIs : map [string ]func (rw http.ResponseWriter , r * http.Request ){
1412
+ "/repos/testOrg/testRepo/pulls" : func (rw http.ResponseWriter , _ * http.Request ) {
1413
+ rw .WriteHeader (http .StatusInternalServerError )
1414
+ fmt .Fprint (rw , `{"message": "API error"}` )
1415
+ },
1416
+ },
1417
+ isPartOfPR : false ,
1418
+ wantErr : false ,
1419
+ skipWarnLogContains : "Error checking if push commit is part of PR" ,
1420
+ },
1421
+ }
1422
+
1423
+ for _ , tt := range tests {
1424
+ t .Run (tt .name , func (t * testing.T ) {
1425
+ ctx , _ := rtesting .SetupFakeContext (t )
1426
+ fakeclient , mux , _ , teardown := ghtesthelper .SetupGH ()
1427
+ defer teardown ()
1428
+
1429
+ // Register API endpoints
1430
+ for pattern , handler := range tt .mockAPIs {
1431
+ mux .HandleFunc (pattern , handler )
1432
+ }
1433
+
1434
+ // Create a logger that captures logs
1435
+ observer , logs := zapobserver .New (zap .InfoLevel )
1436
+ logger := zap .New (observer ).Sugar ()
1437
+
1438
+ // Create provider with the test configuration
1439
+ provider := & Provider {
1440
+ ghClient : fakeclient ,
1441
+ Logger : logger ,
1442
+ pacInfo : & info.PacOpts {
1443
+ Settings : settings.Settings {
1444
+ SkipPushEventForPRCommits : tt .pacInfoEnabled ,
1445
+ },
1446
+ },
1447
+ }
1448
+
1449
+ // Create event with the right trigger type
1450
+ event := info .NewEvent ()
1451
+ event .TriggerTarget = triggertype .Push
1452
+
1453
+ // Process the event
1454
+ result , err := provider .processEvent (ctx , event , tt .pushEvent )
1455
+
1456
+ // Check errors if expected
1457
+ if tt .wantErr {
1458
+ assert .Assert (t , err != nil )
1459
+ if tt .wantErrContains != "" {
1460
+ assert .ErrorContains (t , err , tt .wantErrContains )
1461
+ }
1462
+ assert .Assert (t , result == nil , "Expected nil result when error occurs" )
1463
+ return
1464
+ }
1465
+
1466
+ // If no error expected, check the result
1467
+ assert .NilError (t , err )
1468
+ assert .Assert (t , result != nil , "Expected non-nil result when no error occurs" )
1469
+
1470
+ // Check event fields were properly processed
1471
+ if ! tt .pacInfoEnabled || ! tt .isPartOfPR {
1472
+ assert .Equal (t , result .Organization , tt .pushEvent .GetRepo ().GetOwner ().GetLogin ())
1473
+ assert .Equal (t , result .Repository , tt .pushEvent .GetRepo ().GetName ())
1474
+ assert .Equal (t , result .SHA , tt .pushEvent .GetHeadCommit ().GetID ())
1475
+ assert .Equal (t , result .Sender , tt .pushEvent .GetSender ().GetLogin ())
1476
+ }
1477
+
1478
+ // Check for warning logs if applicable
1479
+ if tt .skipWarnLogContains != "" {
1480
+ // Look for warning logs
1481
+ found := false
1482
+ for _ , logEntry := range logs .All () {
1483
+ if strings .Contains (logEntry .Message , tt .skipWarnLogContains ) {
1484
+ found = true
1485
+ break
1486
+ }
1487
+ }
1488
+ assert .Assert (t , found , "Expected warning log containing: %s" , tt .skipWarnLogContains )
1489
+ }
1490
+ })
1491
+ }
1492
+ }
0 commit comments