From 0c9fafbde596f372b564d8daf29c8593b7c30e82 Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Fri, 13 Apr 2018 16:49:18 -0500 Subject: [PATCH] Resync valid webhook for project manually imported (#3935) When a project is imported manually, if the user has a valid social account we setup a webhook for this project. If the user goes to Integrations and try to resync, we handle that properly (use the Project.remote_repository only when available, otherwise use the accounts registered for that service even if they are not associated with that project) --- readthedocs/oauth/utils.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/readthedocs/oauth/utils.py b/readthedocs/oauth/utils.py index b8476da10c9..fbb47916b5e 100644 --- a/readthedocs/oauth/utils.py +++ b/readthedocs/oauth/utils.py @@ -12,6 +12,7 @@ from readthedocs.integrations.models import Integration from readthedocs.oauth.services import ( BitbucketService, GitHubService, GitLabService, registry) +from readthedocs.projects.models import Project log = logging.getLogger(__name__) @@ -74,9 +75,21 @@ def update_webhook(project, integration, request=None): service_cls = SERVICE_MAP.get(integration.integration_type) if service_cls is None: return None - account = project.remote_repository.account - service = service_cls(request.user, account) - updated, __ = service.update_webhook(project, integration) + + try: + account = project.remote_repository.account + service = service_cls(request.user, account) + updated, __ = service.update_webhook(project, integration) + except Project.remote_repository.RelatedObjectDoesNotExist: + # The project was imported manually and doesn't have a RemoteRepository + # attached. We do brute force over all the accounts registered for this + # service + service_accounts = service_cls.for_user(request.user) + for service in service_accounts: + updated, __ = service.update_webhook(project, integration) + if updated: + break + if updated: messages.success(request, _('Webhook activated')) project.has_valid_webhook = True