diff --git a/endpoints/trigger.py b/endpoints/trigger.py index 4c476533c..2bcdc6242 100644 --- a/endpoints/trigger.py +++ b/endpoints/trigger.py @@ -488,30 +488,36 @@ class BitbucketBuildTrigger(BuildTriggerHandler): # Parse the JSON payload. payload_json = request.form.get('payload') if not payload_json: + logger.debug('Skipping BitBucket request due to missing payload') raise SkipRequestException() try: payload = json.loads(payload_json) except ValueError: + logger.debug('Skipping BitBucket request due to invalid payload') raise SkipRequestException() logger.debug('BitBucket trigger payload %s', payload) # Make sure we have a commit in the payload. if not payload.get('commits'): + logger.debug('Skipping BitBucket request due to missing commits block') raise SkipRequestException() # Check if this build should be skipped by commit message. commit = payload['commits'][0] commit_message = commit['message'] if should_skip_commit(commit_message): + logger.debug('Skipping BitBucket request due to commit message request') raise SkipRequestException() # Check to see if this build should be skipped by ref. if not commit.get('branch') and not commit.get('tag'): + logger.debug('Skipping BitBucket request due to missing branch and tag') raise SkipRequestException() ref = 'refs/heads/' + commit['branch'] if commit.get('branch') else 'refs/tags/' + commit['tag'] + logger.debug('Checking BitBucket request: %s', ref) raise_if_skipped(self.config, ref) commit_sha = commit['node'] diff --git a/test/test_trigger.py b/test/test_trigger.py new file mode 100644 index 000000000..d01853d96 --- /dev/null +++ b/test/test_trigger.py @@ -0,0 +1,29 @@ +import unittest +import re + +from endpoints.trigger import matches_ref + +class TestRegex(unittest.TestCase): + def assertDoesNotMatch(self, ref, filt): + self.assertFalse(matches_ref(ref, re.compile(filt))) + + def assertMatches(self, ref, filt): + self.assertTrue(matches_ref(ref, re.compile(filt))) + + def test_matches_ref(self): + self.assertMatches('ref/heads/master', '.+') + self.assertMatches('ref/heads/master', 'heads/.+') + self.assertMatches('ref/heads/master', 'heads/master') + + self.assertDoesNotMatch('ref/heads/foobar', 'heads/master') + self.assertDoesNotMatch('ref/heads/master', 'tags/master') + + self.assertMatches('ref/heads/master', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)') + self.assertMatches('ref/heads/alpha', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)') + self.assertMatches('ref/heads/beta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)') + self.assertMatches('ref/heads/gamma', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)') + + self.assertDoesNotMatch('ref/heads/delta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)') + +if __name__ == '__main__': + unittest.main()