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

Commit 8c6853a

Browse files
feat(admin): add action to trigger sync repos (#1295)
1 parent 18eb985 commit 8c6853a

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

codecov_auth/admin.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from django.http import HttpRequest
1313
from django.shortcuts import redirect, render
1414
from django.utils import timezone
15+
from services.task import TaskService
16+
1517
from django.utils.html import format_html
1618
from shared.django_apps.codecov_auth.models import (
1719
Account,
@@ -111,6 +113,40 @@ def impersonate_owner(self, request, queryset):
111113
impersonate_owner.short_description = "Impersonate the selected owner"
112114

113115

116+
def refresh_owner(self, request, queryset):
117+
if queryset.count() != 1:
118+
self.message_user(
119+
request, "You must refresh exactly one Owner.", level=messages.ERROR
120+
)
121+
return
122+
123+
owner = queryset.first()
124+
125+
task_service = TaskService()
126+
task_service.refresh(
127+
ownerid=owner.ownerid,
128+
username=owner.username,
129+
sync_teams=True,
130+
sync_repos=True,
131+
manual_trigger=True,
132+
)
133+
134+
self.message_user(
135+
request,
136+
f"Refresh task triggered for {owner.username} (ownerid: {owner.ownerid})",
137+
level=messages.SUCCESS,
138+
)
139+
History.log(
140+
Owner.objects.get(ownerid=owner.ownerid),
141+
"Refresh task triggered",
142+
request.user,
143+
)
144+
return
145+
146+
147+
refresh_owner.short_description = "Sync repos and teams for the selected owner"
148+
149+
114150
class AccountsUsersInline(admin.TabularInline):
115151
model = AccountsUsers
116152
max_num = 10
@@ -559,7 +595,7 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin):
559595
list_display = ("name", "username", "email", "service")
560596
readonly_fields = []
561597
search_fields = ("name__iregex", "username__iregex", "email__iregex", "ownerid")
562-
actions = [impersonate_owner, extend_trial]
598+
actions = [impersonate_owner, extend_trial, refresh_owner]
563599
autocomplete_fields = ("bot", "account")
564600
inlines = [OrgUploadTokenInline]
565601

codecov_auth/tests/test_admin.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,45 @@ def test_owner_admin_impersonate_owner(self):
107107
str(owner_to_impersonate.pk),
108108
)
109109

110+
@patch("codecov_auth.admin.TaskService.refresh")
111+
def test_owner_admin_refresh_owner(self, mock_refresh):
112+
owner_to_refresh = OwnerFactory(service="github", plan=DEFAULT_FREE_PLAN)
113+
other_owner = OwnerFactory(plan=DEFAULT_FREE_PLAN)
114+
115+
with self.subTest("more than one user selected"):
116+
response = self.client.post(
117+
reverse("admin:codecov_auth_owner_changelist"),
118+
{
119+
"action": "refresh_owner",
120+
ACTION_CHECKBOX_NAME: [
121+
owner_to_refresh.pk,
122+
other_owner.pk,
123+
],
124+
},
125+
follow=True,
126+
)
127+
assert "You must refresh exactly one Owner." in str(response.content)
128+
129+
with self.subTest("one user selected"):
130+
response = self.client.post(
131+
reverse("admin:codecov_auth_owner_changelist"),
132+
{
133+
"action": "refresh_owner",
134+
ACTION_CHECKBOX_NAME: [owner_to_refresh.pk],
135+
},
136+
follow=True,
137+
)
138+
assert f"Refresh task triggered for {owner_to_refresh.username}" in str(
139+
response.content
140+
)
141+
mock_refresh.assert_called_once_with(
142+
ownerid=owner_to_refresh.ownerid,
143+
username=owner_to_refresh.username,
144+
sync_teams=True,
145+
sync_repos=True,
146+
manual_trigger=True,
147+
)
148+
110149
@patch("codecov_auth.admin.TaskService.delete_owner")
111150
def test_delete_queryset(self, delete_mock):
112151
user_to_delete = OwnerFactory(plan=DEFAULT_FREE_PLAN)
@@ -608,6 +647,7 @@ def test_link_users_to_account(self):
608647
another_owner_with_user = OwnerFactory(user=UserFactory())
609648
self.org_1.plan_activated_users.append(another_owner_with_user.ownerid)
610649
self.org_1.save()
650+
611651
# rerun action to re-sync
612652
res = self.client.post(
613653
reverse("admin:codecov_auth_account_changelist"),

0 commit comments

Comments
 (0)