Skip to content

Commit 2565e27

Browse files
committed
Re-sync GitHub (RemoteRepository and RemoteOrganization) on webhook
Add `member` event when creating the Read the Docs' webhook on each repository imported under Read the Docs. This webhook will communicate back to us when a collaborator was added/removed/changed from that repository in particular. This allow us to trigger the re-sync method for the GitHub service on that user and create/delete/change `RemoteRepository` objects for that user. Examples, * user is removed from having access to a repository -> remove RemoteRepository * user is added access to a repository -> create RemoteRepository * user is removed admin access but keep read access -> `.admin=False`
1 parent 92e2e7b commit 2565e27

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

readthedocs/api/v2/views/integrations.py

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import logging
77
import re
88

9+
from allauth.socialaccount.providers.github.provider import GitHubProvider
10+
from allauth.socialaccount.models import SocialAccount
11+
912
from django.shortcuts import get_object_or_404
1013
from rest_framework import permissions, status
1114
from rest_framework.exceptions import NotFound, ParseError
@@ -27,13 +30,15 @@
2730
build_external_version,
2831
)
2932
from readthedocs.integrations.models import HttpExchange, Integration
33+
from readthedocs.oauth.services.github import GitHubService
3034
from readthedocs.projects.models import Project, Feature
3135

3236

3337
log = logging.getLogger(__name__)
3438

3539
GITHUB_EVENT_HEADER = 'HTTP_X_GITHUB_EVENT'
3640
GITHUB_SIGNATURE_HEADER = 'HTTP_X_HUB_SIGNATURE'
41+
GITHUB_MEMBER = 'member'
3742
GITHUB_PUSH = 'push'
3843
GITHUB_PULL_REQUEST = 'pull_request'
3944
GITHUB_PULL_REQUEST_OPENED = 'opened'
@@ -459,6 +464,16 @@ def handle_webhook(self):
459464
except KeyError:
460465
raise ParseError('Parameter "ref" is required')
461466

467+
# Re-sync repositories for the user if any permission has changed
468+
if event == GITHUB_MEMBER:
469+
uid = self.data.get('member').get('id')
470+
socialaccount = SocialAccount.objects.get(
471+
provider=GitHubProvider.id,
472+
uid=uid,
473+
)
474+
service = GitHubService(user=socialaccount.user, account=socialaccount)
475+
service.sync()
476+
462477
return None
463478

464479
def _normalize_ref(self, ref):

readthedocs/oauth/services/github.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def get_webhook_data(self, project, integration):
197197
'secret': integration.secret,
198198
'content_type': 'json',
199199
},
200-
'events': ['push', 'pull_request', 'create', 'delete'],
200+
'events': ['push', 'pull_request', 'create', 'delete', 'member'],
201201
})
202202

203203
def get_provider_data(self, project, integration):

0 commit comments

Comments
 (0)