Skip to content

RemoteRepository: Improvements to sync_vcs_data.py script #8017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2021
Merged
Changes from 1 commit
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
37 changes: 36 additions & 1 deletion readthedocs/oauth/management/commands/sync_vcs_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import datetime
import json

from django.utils import timezone
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand

Expand All @@ -21,6 +25,18 @@ def add_arguments(self, parser):
default=[],
help='Re-sync VCS provider data for specific users only.',
)
parser.add_argument(
'--logged-in-days-ago',
type=int,
default=0,
help='Re-sync users logged in in the last days.',
)
parser.add_argument(
'--skip-revoked-users',
action='store_true',
default=False,
help='Skip users who revoked our access token (pulled down from Sentry).',
)
parser.add_argument(
'--skip-users',
nargs='+',
Expand Down Expand Up @@ -49,6 +65,8 @@ def add_arguments(self, parser):

def handle(self, *args, **options):
queue = options.get('queue')
logged_in_days_ago = options.get('logged_in_days_ago')
skip_revoked_users = options.get('skip_revoked_users')
sync_users = options.get('users')
skip_users = options.get('skip_users')
max_users = options.get('max_users')
Expand All @@ -60,6 +78,11 @@ def handle(self, *args, **options):
socialaccount__isnull=False
).distinct()

if logged_in_days_ago:
users = users.filter(
last_login__gte=timezone.now() - datetime.timedelta(days=logged_in_days_ago),
)

if not force_sync:
users = users.filter(
remote_repository_relations__isnull=True
Expand All @@ -77,7 +100,19 @@ def handle(self, *args, **options):
if skip_users:
users = users.exclude(username__in=skip_users)

if sync_users or skip_users:
revoked_users = []
if skip_revoked_users:
# `revoked-users.json` was created by a script pullig down data from Sentry
# https://gist.github.com/humitos/aba1a004abeb3552fd8ef9a741f5dce1
revoked_users = json.load(open('revoked-users.json', 'r'))
users = users.exclude(username__in=revoked_users)
self.stdout.write(
self.style.WARNING(
f'Excluding {len(revoked_users)} revoked users.'
)
)

if sync_users or skip_users or revoked_users:
self.stdout.write(
self.style.SUCCESS(
f'Found {users.count()} user(s) with the given parameters'
Expand Down