Fix the skip branch logic

This commit is contained in:
Joseph Schorr 2015-09-22 14:44:49 -04:00
parent 8e4a3af590
commit 05c9a5f7b8
2 changed files with 62 additions and 10 deletions

View file

@ -75,28 +75,28 @@ def should_skip_commit(message):
return '[skip build]' in message or '[build skip]' in message
def raise_if_skipped_build(prepared_build):
def raise_if_skipped_build(prepared_build, config):
""" Raises a SkipRequestException if the given build should be skipped. """
# Check to ensure we have metadata.
if not prepared_build.metadata:
logger.debug('Skipping request due to missing metadata for prepared build')
raise SkipRequestException()
if should_skip_commit(prepared_build.metadata['commit_info']['message']):
logger.debug('Skipping request due to commit message request')
raise SkipRequestException()
def raise_if_skipped(config, ref):
""" Raises a SkipRequestException if the given ref should be skipped. """
# Check the branchtag regex.
if 'branchtag_regex' in config:
try:
regex = re.compile(config['branchtag_regex'])
except:
regex = re.compile('.*')
if not matches_ref(ref, regex):
if not matches_ref(prepared_build.metadata.get('ref'), regex):
raise SkipRequestException()
# Check the commit message.
if should_skip_commit(prepared_build.metadata['commit_info']['message']):
logger.debug('Skipping request due to commit message request')
raise SkipRequestException()
def matches_ref(ref, regex):
match_string = ref.split('/', 1)[1]

View file

@ -1,7 +1,9 @@
import unittest
import re
from buildtrigger.triggerutil import matches_ref
from buildtrigger.triggerutil import matches_ref, raise_if_skipped_build
from buildtrigger.triggerutil import SkipRequestException
from endpoints.building import PreparedBuild
class TestRegex(unittest.TestCase):
def assertDoesNotMatch(self, ref, filt):
@ -25,5 +27,55 @@ class TestRegex(unittest.TestCase):
self.assertDoesNotMatch('ref/heads/delta', '(((heads/alpha)|(heads/beta))|(heads/gamma))|(heads/master)')
class TestSkipBuild(unittest.TestCase):
def testSkipNoMetadata(self):
prepared = PreparedBuild()
prepared.metadata = {}
config = {}
self.assertRaises(SkipRequestException, raise_if_skipped_build, prepared, config)
def testSkipByBranchtagRegex(self):
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
}
config = {
'branchtag_regex': 'nothing'
}
self.assertRaises(SkipRequestException, raise_if_skipped_build, prepared, config)
def testSkipByMessage(self):
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
'commit_info': {
'message': '[skip build]',
},
}
config = {}
self.assertRaises(SkipRequestException, raise_if_skipped_build, prepared, config)
def testDoesNotSkip(self):
prepared = PreparedBuild()
prepared.metadata = {
'ref': 'ref/heads/master',
'commit_info': {
'message': 'some cool message',
},
}
config = {
'branchtag_regex': '(master)|(heads/master)',
}
raise_if_skipped_build(prepared, config)
if __name__ == '__main__':
unittest.main()