Skip to content

Commit 3499743

Browse files
authored
Webhooks: use PUBLIC_API_URL to generate the webhook URL (#10801)
* Webhooks: use PUBLIC_API_URL to generate the webhook URL When using the beta instance, the webhook URL generated points to the beta instance, but this shouldn't be used, since that domain will be removed when the beta is over. * Expose PUBLIC_API_URL in templates * Fix tests
1 parent 7440c7b commit 3499743

File tree

6 files changed

+23
-63
lines changed

6 files changed

+23
-63
lines changed

readthedocs/core/context_processors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ def readthedocs_processor(request):
1616
"USE_PROMOS": settings.USE_PROMOS,
1717
"USE_ORGANIZATIONS": settings.RTD_ALLOW_ORGANIZATIONS,
1818
"SUPPORT_EMAIL": settings.SUPPORT_EMAIL,
19+
"PUBLIC_API_URL": settings.PUBLIC_API_URL,
1920
}
2021
return exports

readthedocs/oauth/services/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from allauth.socialaccount.models import SocialAccount
77
from allauth.socialaccount.providers import registry
88
from django.conf import settings
9+
from django.urls import reverse
910
from django.utils import timezone
1011
from django.utils.translation import gettext_lazy as _
1112
from oauthlib.oauth2.rfc6749.errors import InvalidClientIdError
@@ -263,6 +264,19 @@ def get_paginated_results(self, response):
263264
"""
264265
raise NotImplementedError
265266

267+
def get_webhook_url(self, project, integration):
268+
"""Get the webhook URL for the project's integration."""
269+
return "{base_url}{path}".format(
270+
base_url=settings.PUBLIC_API_URL,
271+
path=reverse(
272+
"api_webhook",
273+
kwargs={
274+
"project_slug": project.slug,
275+
"integration_pk": integration.pk,
276+
},
277+
),
278+
)
279+
266280
def get_provider_data(self, project, integration):
267281
"""
268282
Gets provider data from Git Providers Webhooks API.

readthedocs/oauth/services/bitbucket.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
BitbucketOAuth2Adapter,
99
)
1010
from django.conf import settings
11-
from django.urls import reverse
1211
from requests.exceptions import RequestException
1312

1413
from readthedocs.builds import utils as build_utils
@@ -213,16 +212,7 @@ def get_webhook_data(self, project, integration):
213212
"description": "Read the Docs ({domain})".format(
214213
domain=settings.PRODUCTION_DOMAIN,
215214
),
216-
"url": "https://{domain}{path}".format(
217-
domain=settings.PRODUCTION_DOMAIN,
218-
path=reverse(
219-
"api_webhook",
220-
kwargs={
221-
"project_slug": project.slug,
222-
"integration_pk": integration.pk,
223-
},
224-
),
225-
),
215+
"url": self.get_webhook_url(project, integration),
226216
"active": True,
227217
"events": ["repo:push"],
228218
}
@@ -247,16 +237,7 @@ def get_provider_data(self, project, integration):
247237
owner, repo = build_utils.get_bitbucket_username_repo(url=project.repo)
248238
url = f"https://api.bitbucket.org/2.0/repositories/{owner}/{repo}/hooks"
249239

250-
rtd_webhook_url = "https://{domain}{path}".format(
251-
domain=settings.PRODUCTION_DOMAIN,
252-
path=reverse(
253-
"api_webhook",
254-
kwargs={
255-
"project_slug": project.slug,
256-
"integration_pk": integration.pk,
257-
},
258-
),
259-
)
240+
rtd_webhook_url = self.get_webhook_url(project, integration)
260241

261242
log.bind(
262243
project_slug=project.slug,

readthedocs/oauth/services/github.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import structlog
77
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
88
from django.conf import settings
9-
from django.urls import reverse
109
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError, TokenExpiredError
1110
from requests.exceptions import RequestException
1211

@@ -217,16 +216,7 @@ def get_webhook_data(self, project, integration):
217216
"name": "web",
218217
"active": True,
219218
"config": {
220-
"url": "https://{domain}{path}".format(
221-
domain=settings.PRODUCTION_DOMAIN,
222-
path=reverse(
223-
"api_webhook",
224-
kwargs={
225-
"project_slug": project.slug,
226-
"integration_pk": integration.pk,
227-
},
228-
),
229-
),
219+
"url": self.get_webhook_url(project, integration),
230220
"secret": integration.secret,
231221
"content_type": "json",
232222
},
@@ -258,16 +248,7 @@ def get_provider_data(self, project, integration):
258248
integration_id=integration.pk,
259249
)
260250

261-
rtd_webhook_url = "https://{domain}{path}".format(
262-
domain=settings.PRODUCTION_DOMAIN,
263-
path=reverse(
264-
"api_webhook",
265-
kwargs={
266-
"project_slug": project.slug,
267-
"integration_pk": integration.pk,
268-
},
269-
),
270-
)
251+
rtd_webhook_url = self.get_webhook_url(project, integration)
271252

272253
try:
273254
resp = session.get(url)

readthedocs/oauth/services/gitlab.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import structlog
88
from allauth.socialaccount.providers.gitlab.views import GitLabOAuth2Adapter
99
from django.conf import settings
10-
from django.urls import reverse
1110
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError, TokenExpiredError
1211
from requests.exceptions import RequestException
1312

@@ -294,16 +293,7 @@ def get_webhook_data(self, repo_id, project, integration):
294293
"id": repo_id,
295294
"push_events": True,
296295
"tag_push_events": True,
297-
"url": "https://{domain}{path}".format(
298-
domain=settings.PRODUCTION_DOMAIN,
299-
path=reverse(
300-
"api_webhook",
301-
kwargs={
302-
"project_slug": project.slug,
303-
"integration_pk": integration.pk,
304-
},
305-
),
306-
),
296+
"url": self.get_webhook_url(project, integration),
307297
"token": integration.secret,
308298
# Optional
309299
"issues_events": False,
@@ -341,16 +331,7 @@ def get_provider_data(self, project, integration):
341331
integration_id=integration.pk,
342332
)
343333

344-
rtd_webhook_url = "https://{domain}{path}".format(
345-
domain=settings.PRODUCTION_DOMAIN,
346-
path=reverse(
347-
"api_webhook",
348-
kwargs={
349-
"project_slug": project.slug,
350-
"integration_pk": integration.pk,
351-
},
352-
),
353-
)
334+
rtd_webhook_url = self.get_webhook_url(project, integration)
354335

355336
try:
356337
resp = session.get(

readthedocs/rtd_tests/tests/test_notifications.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class TestNotification(Notification):
6767
"GLOBAL_ANALYTICS_CODE": mock.ANY,
6868
"PRODUCTION_DOMAIN": "readthedocs.org",
6969
"PUBLIC_DOMAIN": mock.ANY,
70+
"PUBLIC_API_URL": mock.ANY,
7071
"SITE_ROOT": mock.ANY,
7172
"SUPPORT_EMAIL": "[email protected]",
7273
"TEMPLATE_ROOT": mock.ANY,
@@ -232,6 +233,7 @@ def test_context_data(self):
232233
"GLOBAL_ANALYTICS_CODE": mock.ANY,
233234
"PRODUCTION_DOMAIN": "readthedocs.org",
234235
"PUBLIC_DOMAIN": mock.ANY,
236+
"PUBLIC_API_URL": mock.ANY,
235237
"SITE_ROOT": mock.ANY,
236238
"SUPPORT_EMAIL": "[email protected]",
237239
"TEMPLATE_ROOT": mock.ANY,

0 commit comments

Comments
 (0)