From 2a04463e1dbc436e9d41f10db710aeddfca92ab9 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 5 Jan 2018 05:51:27 -0800 Subject: [PATCH 1/3] Check to make sure changes exist in BitBucket pushes --- readthedocs/restapi/views/integrations.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readthedocs/restapi/views/integrations.py b/readthedocs/restapi/views/integrations.py index 80699a1c196..ddc226cf472 100644 --- a/readthedocs/restapi/views/integrations.py +++ b/readthedocs/restapi/views/integrations.py @@ -233,7 +233,8 @@ def handle_webhook(self): try: changes = self.request.data['push']['changes'] branches = [change['new']['name'] - for change in changes] + for change in changes + if change.get('new')] return self.get_response_push(self.project, branches) except KeyError: raise ParseError('Invalid request') From 404546a0b92a8fcc331b8ad84d9b551ac26ed1b2 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 5 Jan 2018 06:10:25 -0800 Subject: [PATCH 2/3] Add test --- readthedocs/rtd_tests/tests/test_api.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/readthedocs/rtd_tests/tests/test_api.py b/readthedocs/rtd_tests/tests/test_api.py index 75e2a7e1f34..219417c9a20 100644 --- a/readthedocs/rtd_tests/tests/test_api.py +++ b/readthedocs/rtd_tests/tests/test_api.py @@ -429,6 +429,22 @@ def test_bitbucket_webhook(self, trigger_build): trigger_build.assert_has_calls( [mock.call(force=True, version=mock.ANY, project=self.project)]) + client.post( + '/api/v2/webhook/bitbucket/{0}/'.format(self.project.slug), + { + 'push': { + 'changes': [ + { + 'new': None, + }, + ], + }, + }, + format='json', + ) + trigger_build.assert_not_called( + [mock.call(force=True, version=mock.ANY, project=self.project)]) + def test_bitbucket_invalid_webhook(self, trigger_build): """Bitbucket webhook unhandled event.""" client = APIClient() From b080dce4cc0e8dccad133459e0bc44484afe3e09 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 5 Jan 2018 13:14:49 -0800 Subject: [PATCH 3/3] Define return values outside variable execution --- readthedocs/core/views/hooks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readthedocs/core/views/hooks.py b/readthedocs/core/views/hooks.py index a99f66c1169..9140c45a59c 100644 --- a/readthedocs/core/views/hooks.py +++ b/readthedocs/core/views/hooks.py @@ -74,10 +74,10 @@ def build_branches(project, branch_list): to_build - a list of branches that were built not_building - a list of branches that we won't build """ + to_build = set() + not_building = set() for branch in branch_list: versions = project.versions_from_branch_name(branch) - to_build = set() - not_building = set() for version in versions: log.info("(Branch Build) Processing %s:%s", project.slug, version.slug)