From bb3deaea89d663f8ceeae0ec2559d8442cde77ff Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Sun, 14 Jan 2018 21:30:29 -0800 Subject: [PATCH] Check to make sure changes exist in BitBucket pushes (#3480) * Check to make sure changes exist in BitBucket pushes * Add test * Define return values outside variable execution --- readthedocs/core/views/hooks.py | 4 ++-- readthedocs/restapi/views/integrations.py | 3 ++- readthedocs/rtd_tests/tests/test_api.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 3 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) 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') diff --git a/readthedocs/rtd_tests/tests/test_api.py b/readthedocs/rtd_tests/tests/test_api.py index f790ffde92f..b4285500729 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()