Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion codecov_auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from django.http import HttpRequest
from django.shortcuts import redirect, render
from django.utils import timezone
from services.task import TaskService

from django.utils.html import format_html
from shared.django_apps.codecov_auth.models import (
Account,
Expand Down Expand Up @@ -111,6 +113,40 @@ def impersonate_owner(self, request, queryset):
impersonate_owner.short_description = "Impersonate the selected owner"


def refresh_owner(self, request, queryset):
if queryset.count() != 1:
self.message_user(
request, "You must refresh exactly one Owner.", level=messages.ERROR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be trivially possible to also support multiple-select.

)
return

owner = queryset.first()

task_service = TaskService()
task_service.refresh(
ownerid=owner.ownerid,
username=owner.username,
sync_teams=True,
sync_repos=True,
manual_trigger=True,
)

self.message_user(
request,
f"Refresh task triggered for {owner.username} (ownerid: {owner.ownerid})",
level=messages.SUCCESS,
)
History.log(
Owner.objects.get(ownerid=owner.ownerid),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would just be owner, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure, cause there is an OwnerAdmin class (line 593) so I wasn't sure if the queryset is of OwnerAdmins or Owners :E

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SO I copied what impersonate_owner does :P

"Refresh task triggered",
request.user,
)
return


refresh_owner.short_description = "Sync repos and teams for the selected owner"


class AccountsUsersInline(admin.TabularInline):
model = AccountsUsers
max_num = 10
Expand Down Expand Up @@ -559,7 +595,7 @@ class OwnerAdmin(AdminMixin, admin.ModelAdmin):
list_display = ("name", "username", "email", "service")
readonly_fields = []
search_fields = ("name__iregex", "username__iregex", "email__iregex", "ownerid")
actions = [impersonate_owner, extend_trial]
actions = [impersonate_owner, extend_trial, refresh_owner]
autocomplete_fields = ("bot", "account")
inlines = [OrgUploadTokenInline]

Expand Down
40 changes: 40 additions & 0 deletions codecov_auth/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,45 @@ def test_owner_admin_impersonate_owner(self):
str(owner_to_impersonate.pk),
)

@patch("codecov_auth.admin.TaskService.refresh")
def test_owner_admin_refresh_owner(self, mock_refresh):
owner_to_refresh = OwnerFactory(service="github", plan=DEFAULT_FREE_PLAN)
other_owner = OwnerFactory(plan=DEFAULT_FREE_PLAN)

with self.subTest("more than one user selected"):
response = self.client.post(
reverse("admin:codecov_auth_owner_changelist"),
{
"action": "refresh_owner",
ACTION_CHECKBOX_NAME: [
owner_to_refresh.pk,
other_owner.pk,
],
},
follow=True,
)
assert "You must refresh exactly one Owner." in str(response.content)

with self.subTest("one user selected"):
response = self.client.post(
reverse("admin:codecov_auth_owner_changelist"),
{
"action": "refresh_owner",
ACTION_CHECKBOX_NAME: [owner_to_refresh.pk],
},
follow=True,
)
assert f"Refresh task triggered for {owner_to_refresh.username}" in str(
response.content
)
mock_refresh.assert_called_once_with(
ownerid=owner_to_refresh.ownerid,
username=owner_to_refresh.username,
sync_teams=True,
sync_repos=True,
manual_trigger=True,
)

@patch("codecov_auth.admin.TaskService.delete_owner")
def test_delete_queryset(self, delete_mock):
user_to_delete = OwnerFactory(plan=DEFAULT_FREE_PLAN)
Expand Down Expand Up @@ -608,6 +647,7 @@ def test_link_users_to_account(self):
another_owner_with_user = OwnerFactory(user=UserFactory())
self.org_1.plan_activated_users.append(another_owner_with_user.ownerid)
self.org_1.save()

# rerun action to re-sync
res = self.client.post(
reverse("admin:codecov_auth_account_changelist"),
Expand Down
Loading