Skip to content

Commit

Permalink
Webhooks: use PUBLIC_API_URL to generate the webhook URL (#10801)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stsewd authored Oct 9, 2023
1 parent 7440c7b commit 3499743
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 63 deletions.
1 change: 1 addition & 0 deletions readthedocs/core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ def readthedocs_processor(request):
"USE_PROMOS": settings.USE_PROMOS,
"USE_ORGANIZATIONS": settings.RTD_ALLOW_ORGANIZATIONS,
"SUPPORT_EMAIL": settings.SUPPORT_EMAIL,
"PUBLIC_API_URL": settings.PUBLIC_API_URL,
}
return exports
14 changes: 14 additions & 0 deletions readthedocs/oauth/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from allauth.socialaccount.models import SocialAccount
from allauth.socialaccount.providers import registry
from django.conf import settings
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from oauthlib.oauth2.rfc6749.errors import InvalidClientIdError
Expand Down Expand Up @@ -263,6 +264,19 @@ def get_paginated_results(self, response):
"""
raise NotImplementedError

def get_webhook_url(self, project, integration):
"""Get the webhook URL for the project's integration."""
return "{base_url}{path}".format(
base_url=settings.PUBLIC_API_URL,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
)

def get_provider_data(self, project, integration):
"""
Gets provider data from Git Providers Webhooks API.
Expand Down
23 changes: 2 additions & 21 deletions readthedocs/oauth/services/bitbucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
BitbucketOAuth2Adapter,
)
from django.conf import settings
from django.urls import reverse
from requests.exceptions import RequestException

from readthedocs.builds import utils as build_utils
Expand Down Expand Up @@ -213,16 +212,7 @@ def get_webhook_data(self, project, integration):
"description": "Read the Docs ({domain})".format(
domain=settings.PRODUCTION_DOMAIN,
),
"url": "https://{domain}{path}".format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
),
"url": self.get_webhook_url(project, integration),
"active": True,
"events": ["repo:push"],
}
Expand All @@ -247,16 +237,7 @@ def get_provider_data(self, project, integration):
owner, repo = build_utils.get_bitbucket_username_repo(url=project.repo)
url = f"https://api.bitbucket.org/2.0/repositories/{owner}/{repo}/hooks"

rtd_webhook_url = "https://{domain}{path}".format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
)
rtd_webhook_url = self.get_webhook_url(project, integration)

log.bind(
project_slug=project.slug,
Expand Down
23 changes: 2 additions & 21 deletions readthedocs/oauth/services/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import structlog
from allauth.socialaccount.providers.github.views import GitHubOAuth2Adapter
from django.conf import settings
from django.urls import reverse
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError, TokenExpiredError
from requests.exceptions import RequestException

Expand Down Expand Up @@ -217,16 +216,7 @@ def get_webhook_data(self, project, integration):
"name": "web",
"active": True,
"config": {
"url": "https://{domain}{path}".format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
),
"url": self.get_webhook_url(project, integration),
"secret": integration.secret,
"content_type": "json",
},
Expand Down Expand Up @@ -258,16 +248,7 @@ def get_provider_data(self, project, integration):
integration_id=integration.pk,
)

rtd_webhook_url = "https://{domain}{path}".format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
)
rtd_webhook_url = self.get_webhook_url(project, integration)

try:
resp = session.get(url)
Expand Down
23 changes: 2 additions & 21 deletions readthedocs/oauth/services/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import structlog
from allauth.socialaccount.providers.gitlab.views import GitLabOAuth2Adapter
from django.conf import settings
from django.urls import reverse
from oauthlib.oauth2.rfc6749.errors import InvalidGrantError, TokenExpiredError
from requests.exceptions import RequestException

Expand Down Expand Up @@ -294,16 +293,7 @@ def get_webhook_data(self, repo_id, project, integration):
"id": repo_id,
"push_events": True,
"tag_push_events": True,
"url": "https://{domain}{path}".format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
),
"url": self.get_webhook_url(project, integration),
"token": integration.secret,
# Optional
"issues_events": False,
Expand Down Expand Up @@ -341,16 +331,7 @@ def get_provider_data(self, project, integration):
integration_id=integration.pk,
)

rtd_webhook_url = "https://{domain}{path}".format(
domain=settings.PRODUCTION_DOMAIN,
path=reverse(
"api_webhook",
kwargs={
"project_slug": project.slug,
"integration_pk": integration.pk,
},
),
)
rtd_webhook_url = self.get_webhook_url(project, integration)

try:
resp = session.get(
Expand Down
2 changes: 2 additions & 0 deletions readthedocs/rtd_tests/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TestNotification(Notification):
"GLOBAL_ANALYTICS_CODE": mock.ANY,
"PRODUCTION_DOMAIN": "readthedocs.org",
"PUBLIC_DOMAIN": mock.ANY,
"PUBLIC_API_URL": mock.ANY,
"SITE_ROOT": mock.ANY,
"SUPPORT_EMAIL": "[email protected]",
"TEMPLATE_ROOT": mock.ANY,
Expand Down Expand Up @@ -232,6 +233,7 @@ def test_context_data(self):
"GLOBAL_ANALYTICS_CODE": mock.ANY,
"PRODUCTION_DOMAIN": "readthedocs.org",
"PUBLIC_DOMAIN": mock.ANY,
"PUBLIC_API_URL": mock.ANY,
"SITE_ROOT": mock.ANY,
"SUPPORT_EMAIL": "[email protected]",
"TEMPLATE_ROOT": mock.ANY,
Expand Down

0 comments on commit 3499743

Please sign in to comment.