Skip to content

Commit 0c9fafb

Browse files
humitosagjohnson
authored andcommitted
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)
1 parent 6bd1c68 commit 0c9fafb

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

readthedocs/oauth/utils.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from readthedocs.integrations.models import Integration
1313
from readthedocs.oauth.services import (
1414
BitbucketService, GitHubService, GitLabService, registry)
15+
from readthedocs.projects.models import Project
1516

1617
log = logging.getLogger(__name__)
1718

@@ -74,9 +75,21 @@ def update_webhook(project, integration, request=None):
7475
service_cls = SERVICE_MAP.get(integration.integration_type)
7576
if service_cls is None:
7677
return None
77-
account = project.remote_repository.account
78-
service = service_cls(request.user, account)
79-
updated, __ = service.update_webhook(project, integration)
78+
79+
try:
80+
account = project.remote_repository.account
81+
service = service_cls(request.user, account)
82+
updated, __ = service.update_webhook(project, integration)
83+
except Project.remote_repository.RelatedObjectDoesNotExist:
84+
# The project was imported manually and doesn't have a RemoteRepository
85+
# attached. We do brute force over all the accounts registered for this
86+
# service
87+
service_accounts = service_cls.for_user(request.user)
88+
for service in service_accounts:
89+
updated, __ = service.update_webhook(project, integration)
90+
if updated:
91+
break
92+
8093
if updated:
8194
messages.success(request, _('Webhook activated'))
8295
project.has_valid_webhook = True

0 commit comments

Comments
 (0)