Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit f15481c

Browse files
committed
webhooks: force sync when user is removed from org
1 parent ec56480 commit f15481c

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

webhook_handlers/tests/test_github.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,10 @@ def test_installation_trigger_refresh_with_other_actions(self, refresh_mock):
11271127
repos_affected=[("12321", "R_kgDOG2tZYQ"), ("12343", "R_kgDOG2tABC")],
11281128
)
11291129

1130+
@patch("services.task.TaskService.refresh")
11301131
def test_organization_with_removed_action_removes_user_from_org_and_activated_user_list(
11311132
self,
1133+
mock_refresh,
11321134
):
11331135
org = OwnerFactory(service_id="4321", service=Service.GITHUB.value)
11341136
user = OwnerFactory(
@@ -1149,7 +1151,13 @@ def test_organization_with_removed_action_removes_user_from_org_and_activated_us
11491151
user.refresh_from_db()
11501152
org.refresh_from_db()
11511153

1152-
assert org.ownerid not in user.organizations
1154+
mock_refresh.assert_called_with(
1155+
ownerid=user.ownerid,
1156+
username=user.username,
1157+
sync_teams=True,
1158+
sync_repos=True,
1159+
using_integration=False,
1160+
)
11531161
assert user.ownerid not in org.plan_activated_users
11541162

11551163
def test_organization_member_removed_with_nonexistent_org_doesnt_crash(self):

webhook_handlers/tests/test_github_enterprise.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,8 +899,10 @@ def test_installation_trigger_refresh_with_other_actions(self, refresh_mock):
899899
repos_affected=[("12321", "R_12321CAT"), ("12343", "R_12343DOG")],
900900
)
901901

902+
@patch("services.task.TaskService.refresh")
902903
def test_organization_with_removed_action_removes_user_from_org_and_activated_user_list(
903904
self,
905+
mock_refresh,
904906
):
905907
org = OwnerFactory(service_id="4321", service=Service.GITHUB_ENTERPRISE.value)
906908
user = OwnerFactory(
@@ -923,7 +925,13 @@ def test_organization_with_removed_action_removes_user_from_org_and_activated_us
923925
user.refresh_from_db()
924926
org.refresh_from_db()
925927

926-
assert org.ownerid not in user.organizations
928+
mock_refresh.assert_called_with(
929+
ownerid=user.ownerid,
930+
username=user.username,
931+
sync_teams=True,
932+
sync_repos=True,
933+
using_integration=False,
934+
)
927935
assert user.ownerid not in org.plan_activated_users
928936

929937
def test_organization_member_removed_with_nonexistent_org_doesnt_crash(self):

webhook_handlers/views/github.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,15 @@ def organization(self, request, *args, **kwargs):
635635
data="Attempted to remove non Codecov user from Codecov org failed",
636636
)
637637

638-
try:
639-
if member.organizations:
640-
member.organizations.remove(org.ownerid)
641-
member.save(update_fields=["organizations"])
642-
except ValueError:
643-
pass
638+
# Force a sync for the removed member to remove their access to the
639+
# org and its private repositories.
640+
TaskService().refresh(
641+
ownerid=member.ownerid,
642+
username=member.username,
643+
sync_teams=True,
644+
sync_repos=True,
645+
using_integration=False,
646+
)
644647

645648
try:
646649
if org.plan_activated_users:

0 commit comments

Comments
 (0)