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