Skip to content

Handle GitHub Push events with deleted: true in the JSON #6465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions readthedocs/api/v2/views/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ def sync_versions(self, project):
'build_triggered': False,
'project': project.slug,
'versions': [version],
'versions_synced': True,
}

def get_external_version_response(self, project):
Expand Down Expand Up @@ -359,24 +360,28 @@ def get_digest(secret, msg):
def handle_webhook(self):
# Get event and trigger other webhook events
action = self.data.get('action', None)
created = self.data.get('created', False)
deleted = self.data.get('deleted', False)
event = self.request.META.get(GITHUB_EVENT_HEADER, GITHUB_PUSH)
webhook_github.send(
Project,
project=self.project,
data=self.data,
event=event,
)
# Handle push events and trigger builds
if event == GITHUB_PUSH:
# Don't build a branch if it's a push that was actually a delete
# https://developer.github.com/v3/activity/events/types/#pushevent
if event == GITHUB_PUSH and not (deleted or created):
try:
branches = [self._normalize_ref(self.data['ref'])]
return self.get_response_push(self.project, branches)
except KeyError:
raise ParseError('Parameter "ref" is required')
if event in (GITHUB_CREATE, GITHUB_DELETE):
# Sync versions on other PUSH events that create or delete
elif event in (GITHUB_CREATE, GITHUB_DELETE, GITHUB_PUSH):
return self.sync_versions(self.project)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably not the most efficient thing we could be doing. We could grab the branch name from the Push and delete it explicitly, but this seemed like the easiest implementation.


if (
elif (
self.project.has_feature(Feature.EXTERNAL_VERSION_BUILD) and
event == GITHUB_PULL_REQUEST and action
):
Expand Down
20 changes: 20 additions & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,26 @@ def test_github_webhook_for_tags(self, trigger_build):
[mock.call(force=True, version=self.version_tag, project=self.project)],
)

@mock.patch('readthedocs.core.views.hooks.sync_repository_task')
def test_github_webhook_no_build_on_delete(self, sync_repository_task, trigger_build):
client = APIClient()

payload = {'ref': 'master', 'deleted': True}
headers = {GITHUB_EVENT_HEADER: GITHUB_PUSH}
resp = client.post(
'/api/v2/webhook/github/{}/'.format(self.project.slug),
payload,
format='json',
**headers
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertFalse(resp.data['build_triggered'])
self.assertEqual(resp.data['project'], self.project.slug)
self.assertEqual(resp.data['versions'], [LATEST])
trigger_build.assert_not_called()
latest_version = self.project.versions.get(slug=LATEST)
sync_repository_task.delay.assert_called_with(latest_version.pk)

@mock.patch('readthedocs.core.views.hooks.sync_repository_task')
def test_github_create_event(self, sync_repository_task, trigger_build):
client = APIClient()
Expand Down