Skip to content

Commit

Permalink
Validate a branch that we parse when running cherry_picker --continue
Browse files Browse the repository at this point in the history
When running cherry_picker --continue we count on being able to get
certain information from the branch's name which cherry_picker
constructed earlier.  Verify as best we can that the branch name is one
which cherry_picker could have constructed.

Relies on the changes here: python#265
(which does the work of one of the validations)
  • Loading branch information
abadger committed Jun 16, 2018
1 parent f90d993 commit b42b8a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
26 changes: 20 additions & 6 deletions cherry_picker/cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ def upstream(self):

@property
def sorted_branches(self):
def version_from_branch(branch):
try:
return tuple(map(int, re.match(r'^.*(?P<version>\d+(\.\d+)+).*$', branch).groupdict()['version'].split('.')))
except AttributeError as attr_err:
raise ValueError(f'Branch {branch} seems to not have a version in its name.') from attr_err

"""Return the branches to cherry-pick to, sorted by version"""
return sorted(
self.branches,
reverse=True,
Expand Down Expand Up @@ -423,9 +418,28 @@ def get_base_branch(cherry_pick_branch):
return '2.7' from 'backport-sha-2.7'
"""
prefix, sha, base_branch = cherry_pick_branch.split('-', 2)

if prefix != 'backport':
raise ValueError('branch name is not prefixed with "backport-". Is this a cherry_picker branch?')
if not re.match('[0-9a-f]{7,40}', sha):
raise ValueError('branch name has an invalid sha')
# Subject the parsed base_branch to the same tests as when we generated it
# This throws a ValueError if the base_branch doesn't need our requirements
version_from_branch(base_branch)

return base_branch


def version_from_branch(branch):
"""
return version information from a git branch name
"""
try:
return tuple(map(int, re.match(r'^.*(?P<version>\d+(\.\d+)+).*$', branch).groupdict()['version'].split('.')))
except AttributeError as attr_err:
raise ValueError(f'Branch {branch} seems to not have a version in its name.') from attr_err


def get_current_branch():
"""
Return the current branch
Expand Down
15 changes: 13 additions & 2 deletions cherry_picker/cherry_picker/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,29 @@ def changedir(d):


def test_get_base_branch():
# The format of cherry-pick branches we create are "backport-{SHA}-{base_branch}"
# The format of cherry-pick branches we create are::
# backport-{SHA}-{base_branch}
cherry_pick_branch = 'backport-afc23f4-2.7'
result = get_base_branch(cherry_pick_branch)
assert result == '2.7'


def test_get_base_branch_which_has_dashes():
cherry_pick_branch ='backport-afc23f4-baseprefix-2.7-basesuffix'
cherry_pick_branch = 'backport-afc23f4-baseprefix-2.7-basesuffix'
result = get_base_branch(cherry_pick_branch)
assert result == 'baseprefix-2.7-basesuffix'


@pytest.mark.parametrize('cherry_pick_branch', ['backport-afc23f4', # Not enough fields
'prefix-afc23f4-2.7', # Not the prefix we were expecting
'backport-afc23f4-base', # No version info in the base branch
]
)
def test_get_base_branch_invalid(cherry_pick_branch):
with pytest.raises(ValueError):
get_base_branch(cherry_pick_branch)


@mock.patch('subprocess.check_output')
def test_get_current_branch(subprocess_check_output):
subprocess_check_output.return_value = b'master'
Expand Down

0 comments on commit b42b8a9

Please sign in to comment.