@@ -47,6 +47,7 @@ def __getitem__(self, key):
4747
4848WEBHOOK_SECRET = b"testixik8qdauiab1yiffydimvi72ekq"
4949DEFAULT_APP_ID = 1234
50+ AI_FEATURES_GH_APP_ID = 9999
5051
5152
5253class GithubWebhookHandlerTests (APITestCase ):
@@ -58,16 +59,23 @@ def inject_mocker(request, mocker):
5859 def mock_webhook_secret (self , mocker ):
5960 mock_config_helper (mocker , configs = {"github.webhook_secret" : WEBHOOK_SECRET })
6061
62+ @pytest .fixture (autouse = True )
63+ def mock_ai_features_app_id (self , mocker ):
64+ mock_config_helper (
65+ mocker , configs = {"github.ai_features_app_id" : AI_FEATURES_GH_APP_ID }
66+ )
67+
6168 @pytest .fixture (autouse = True )
6269 def mock_default_app_id (self , mocker ):
6370 mock_config_helper (mocker , configs = {"github.integration.id" : DEFAULT_APP_ID })
6471
65- def _post_event_data (self , event , data = {}):
72+ def _post_event_data (self , event , data = {}, app_id = DEFAULT_APP_ID ):
6673 return self .client .post (
6774 reverse ("github-webhook" ),
6875 ** {
6976 GitHubHTTPHeaders .EVENT : event ,
7077 GitHubHTTPHeaders .DELIVERY_TOKEN : uuid .UUID (int = 5 ),
78+ GitHubHTTPHeaders .HOOK_INSTALLATION_TARGET_ID : app_id ,
7179 GitHubHTTPHeaders .SIGNATURE_256 : "sha256="
7280 + hmac .new (
7381 WEBHOOK_SECRET ,
@@ -1420,3 +1428,98 @@ def test_repo_creation_doesnt_crash_for_forked_repo(self):
14201428 )
14211429
14221430 assert owner .repository_set .filter (name = "testrepo" ).exists ()
1431+
1432+ def test_check_codecov_ai_auto_enabled_reviews_enabled (self ):
1433+ # Create an organization with AI PR review enabled
1434+ org_with_ai_enabled = OwnerFactory (
1435+ service = Service .GITHUB .value , yaml = {"ai_pr_review" : {"auto_review" : True }}
1436+ )
1437+
1438+ response = self ._post_event_data (
1439+ event = GitHubWebhookEvents .PULL_REQUEST ,
1440+ data = {
1441+ "action" : "pull_request" ,
1442+ "repository" : {
1443+ "id" : 506003 ,
1444+ "name" : "testrepo" ,
1445+ "private" : False ,
1446+ "default_branch" : "main" ,
1447+ "owner" : {"id" : org_with_ai_enabled .service_id },
1448+ "fork" : True ,
1449+ "parent" : {
1450+ "name" : "mainrepo" ,
1451+ "language" : "python" ,
1452+ "id" : 7940284 ,
1453+ "private" : False ,
1454+ "default_branch" : "main" ,
1455+ "owner" : {"id" : 8495712939 , "login" : "alogin" },
1456+ },
1457+ },
1458+ },
1459+ app_id = AI_FEATURES_GH_APP_ID ,
1460+ )
1461+ assert response .data == {"auto_review_enabled" : True }
1462+
1463+ def test_check_codecov_ai_auto_enabled_reviews_disabled (self ):
1464+ # Test with AI PR review disabled
1465+ org_with_ai_disabled = OwnerFactory (
1466+ service = Service .GITHUB .value , yaml = {"ai_pr_review" : {"auto_review" : False }}
1467+ )
1468+
1469+ response = self ._post_event_data (
1470+ event = GitHubWebhookEvents .PULL_REQUEST ,
1471+ data = {
1472+ "action" : "pull_request" ,
1473+ "repository" : {
1474+ "id" : 506004 ,
1475+ "name" : "testrepo2" ,
1476+ "private" : False ,
1477+ "default_branch" : "main" ,
1478+ "owner" : {"id" : org_with_ai_disabled .service_id },
1479+ },
1480+ },
1481+ app_id = AI_FEATURES_GH_APP_ID ,
1482+ )
1483+ assert response .data == {"auto_review_enabled" : False }
1484+
1485+ def test_check_codecov_ai_auto_enabled_reviews_no_config (self ):
1486+ # Test with no yaml config
1487+ org_with_no_config = OwnerFactory (service = Service .GITHUB .value , yaml = {})
1488+
1489+ response = self ._post_event_data (
1490+ event = GitHubWebhookEvents .PULL_REQUEST ,
1491+ data = {
1492+ "action" : "pull_request" ,
1493+ "repository" : {
1494+ "id" : 506005 ,
1495+ "name" : "testrepo3" ,
1496+ "private" : False ,
1497+ "default_branch" : "main" ,
1498+ "owner" : {"id" : org_with_no_config .service_id },
1499+ },
1500+ },
1501+ app_id = AI_FEATURES_GH_APP_ID ,
1502+ )
1503+ assert response .data == {"auto_review_enabled" : False }
1504+
1505+ def test_check_codecov_ai_auto_enabled_reviews_partial_config (self ):
1506+ # Test with partial yaml config
1507+ org_with_partial_config = OwnerFactory (
1508+ service = Service .GITHUB .value , yaml = {"ai_pr_review" : {}}
1509+ )
1510+
1511+ response = self ._post_event_data (
1512+ event = GitHubWebhookEvents .PULL_REQUEST ,
1513+ data = {
1514+ "action" : "pull_request" ,
1515+ "repository" : {
1516+ "id" : 506006 ,
1517+ "name" : "testrepo4" ,
1518+ "private" : False ,
1519+ "default_branch" : "main" ,
1520+ "owner" : {"id" : org_with_partial_config .service_id },
1521+ },
1522+ },
1523+ app_id = AI_FEATURES_GH_APP_ID ,
1524+ )
1525+ assert response .data == {"auto_review_enabled" : False }
0 commit comments