Skip to content

Commit

Permalink
Resync valid webhook for project manually imported (#3935)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
humitos authored and agjohnson committed Apr 13, 2018
1 parent 6bd1c68 commit 0c9fafb
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions readthedocs/oauth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0c9fafb

Please sign in to comment.