Skip to content

Commit

Permalink
Add changes automatically introduced by pre-commit
Browse files Browse the repository at this point in the history
The changes in this PR were not originally intended, and are basically
stylistic changes automatically introduced by pre-commit according to
the project configuration.
  • Loading branch information
abravalheri committed Jun 6, 2021
1 parent a3c35fc commit f228147
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 93 deletions.
2 changes: 1 addition & 1 deletion docs/webhooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Incoming Webhooks and Automation

The primary method that Read the Docs uses to detect changes to your
documentation and versions is through the use of *webhooks*. Webhooks are configured with
your repository provider, such as GitHub, Bitbucket or GitLab,
your repository provider, such as GitHub, Bitbucket or GitLab,
and with each change to your repository, Read the Docs is notified. When we
receive a webhook notification, we determine if the change is related to an
active version for your project, and if it is, a build is triggered for that
Expand Down
82 changes: 45 additions & 37 deletions readthedocs/api/v2/views/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
trigger_sync_versions,
)
from readthedocs.integrations.models import HttpExchange, Integration
from readthedocs.projects.models import Feature, Project
from readthedocs.projects.models import Project

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -197,9 +197,11 @@ def get_response_push(self, project, branches):

def sync_versions_response(self, project, sync=True):
"""
Trigger a sync and returns a response indicating if the build was triggered or not.
Trigger a sync and returns a response indicating if the build was
triggered or not.
If `sync` is False, the sync isn't triggered and a response indicating so is returned.
If `sync` is False, the sync isn't triggered and a response indicating
so is returned.
"""
version = None
if sync:
Expand All @@ -213,7 +215,8 @@ def sync_versions_response(self, project, sync=True):

def get_external_version_response(self, project):
"""
Trigger builds for External versions on pull/merge request events and return API response.
Trigger builds for External versions on pull/merge request events and
return API response.
Return a JSON response with the following::
Expand Down Expand Up @@ -244,7 +247,8 @@ def get_external_version_response(self, project):

def get_deactivated_external_version_response(self, project):
"""
Deactivate the external version on merge/close events and return the API response.
Deactivate the external version on merge/close events and return the API
response.
Return a JSON response with the following::
Expand Down Expand Up @@ -389,7 +393,6 @@ def handle_webhook(self):
a pull request review is requested or removed (``action`` will contain this data)
See https://developer.github.com/v3/activity/events/types/
"""
# Get event and trigger other webhook events
action = self.data.get('action', None)
Expand All @@ -405,43 +408,46 @@ def handle_webhook(self):

# Sync versions when a branch/tag was created/deleted
if event in (GITHUB_CREATE, GITHUB_DELETE):
log.info('Triggered sync_versions: project=%s event=%s', self.project, event)
log.info(
'Triggered sync_versions: project=%s event=%s', self.project,
event
)
return self.sync_versions_response(self.project)

# Handle pull request events
if self.project.external_builds_enabled and event == GITHUB_PULL_REQUEST:
if (
action in
[
GITHUB_PULL_REQUEST_OPENED,
GITHUB_PULL_REQUEST_REOPENED,
GITHUB_PULL_REQUEST_SYNC
]
):
if (action in [GITHUB_PULL_REQUEST_OPENED,
GITHUB_PULL_REQUEST_REOPENED,
GITHUB_PULL_REQUEST_SYNC]):
# Trigger a build when PR is opened/reopened/sync
return self.get_external_version_response(self.project)

if action == GITHUB_PULL_REQUEST_CLOSED:
# Delete external version when PR is closed
return self.get_deactivated_external_version_response(self.project)
return self.get_deactivated_external_version_response(
self.project
)

# Sync versions when push event is created/deleted action
if all([
event == GITHUB_PUSH,
(created or deleted),
event == GITHUB_PUSH,
(created or deleted),
]):
integration = self.get_integration()
events = integration.provider_data.get('events', [])
if any([
GITHUB_CREATE in events,
GITHUB_DELETE in events,
GITHUB_CREATE in events,
GITHUB_DELETE in events,
]):
# GitHub will send PUSH **and** CREATE/DELETE events on a creation/deletion in newer
# webhooks. If we receive a PUSH event we need to check if the webhook doesn't
# already have the CREATE/DELETE events. So we don't trigger the sync twice.
return self.sync_versions_response(self.project, sync=False)

log.info('Triggered sync_versions: project=%s events=%s', self.project, events)
log.info(
'Triggered sync_versions: project=%s events=%s', self.project,
events
)
return self.sync_versions_response(self.project)

# Trigger a build for all branches in the push
Expand Down Expand Up @@ -554,8 +560,10 @@ def handle_webhook(self):
after = data.get('after')
# Tag/branch created/deleted
if GITLAB_NULL_HASH in (before, after):
log.info('Triggered sync_versions: project=%s before=%s after=%s',
self.project, before, after)
log.info(
'Triggered sync_versions: project=%s before=%s after=%s',
self.project, before, after
)
return self.sync_versions_response(self.project)
# Normal push to master
try:
Expand All @@ -565,20 +573,18 @@ def handle_webhook(self):
raise ParseError('Parameter "ref" is required')

if self.project.external_builds_enabled and event == GITLAB_MERGE_REQUEST:
if (
action in
[
GITLAB_MERGE_REQUEST_OPEN,
GITLAB_MERGE_REQUEST_REOPEN,
GITLAB_MERGE_REQUEST_UPDATE
]
):
if (action in [GITLAB_MERGE_REQUEST_OPEN,
GITLAB_MERGE_REQUEST_REOPEN,
GITLAB_MERGE_REQUEST_UPDATE]):
# Handle open, update, reopen merge_request event.
return self.get_external_version_response(self.project)

if action in [GITLAB_MERGE_REQUEST_CLOSE, GITLAB_MERGE_REQUEST_MERGE]:
if action in [GITLAB_MERGE_REQUEST_CLOSE,
GITLAB_MERGE_REQUEST_MERGE]:
# Handle merge and close merge_request event.
return self.get_deactivated_external_version_response(self.project)
return self.get_deactivated_external_version_response(
self.project
)
return None

def _normalize_ref(self, ref):
Expand Down Expand Up @@ -652,8 +658,10 @@ def handle_webhook(self):
# will be triggered with the normal push.
if branches:
return self.get_response_push(self.project, branches)
log.info('Triggered sync_versions: project=%s event=%s',
self.project, event)
log.info(
'Triggered sync_versions: project=%s event=%s',
self.project, event
)
return self.sync_versions_response(self.project)
except KeyError:
raise ParseError('Invalid request')
Expand Down Expand Up @@ -739,8 +747,8 @@ def is_payload_valid(self):
"""
We can't have payload validation in the generic webhook.
Since we don't know the system that would trigger the webhook.
We have a token for authentication.
Since we don't know the system that would trigger the webhook. We have a
token for authentication.
"""
return True

Expand Down
Loading

0 comments on commit f228147

Please sign in to comment.