Gate tag manipulation behind a sync_tags argument

This commit is contained in:
Erik Johnson 2018-08-23 19:41:49 -05:00
parent 29de855ede
commit ed7b99453c
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F
2 changed files with 64 additions and 22 deletions

View file

@ -158,7 +158,12 @@ def _uptodate(ret, target, comments=None, local_changes=False):
# Shouldn't be making any changes if the repo was up to date, but
# report on them so we are alerted to potential problems with our
# logic.
ret['comment'] += '\n\nChanges made: ' + comments
ret['comment'] += (
'\n\nChanges {0}made: {1}'.format(
'that would be ' if __opts__['test'] else '',
_format_comments(comments)
)
)
return ret
@ -171,8 +176,7 @@ def _neutral_test(ret, comment):
def _fail(ret, msg, comments=None):
ret['result'] = False
if comments:
msg += '\n\nChanges already made: '
msg += _format_comments(comments)
msg += '\n\nChanges already made: ' + _format_comments(comments)
ret['comment'] = msg
return ret
@ -184,8 +188,12 @@ def _already_cloned(ret, target, branch=None, comments=None):
' and is checked out to branch \'{0}\''.format(branch) if branch else ''
)
if comments:
ret['comment'] += '\n\nChanges already made: '
ret['comment'] += _format_comments(comments)
ret['comment'] += (
'\n\nChanges {0}made: {1}'.format(
'that would be ' if __opts__['test'] else '',
_format_comments(comments)
)
)
return ret
@ -268,6 +276,7 @@ def latest(name,
mirror=False,
remote='origin',
fetch_tags=True,
sync_tags=True,
depth=None,
identity=None,
https_user=None,
@ -460,6 +469,12 @@ def latest(name,
If ``True``, then when a fetch is performed all tags will be fetched,
even those which are not reachable by any branch on the remote.
sync_tags : True
If ``True``, then Salt will delete tags which exist in the local clone
but are not found on the remote repository.
.. versionadded:: 2018.3.4
depth
Defines depth in history when git a clone is needed in order to ensure
latest. E.g. ``depth: 1`` is useful when deploying from a repository
@ -1379,18 +1394,19 @@ def latest(name,
deleted_tags = all_local_tags - remote_tags
if new_tags:
ret['changes']['new_tags'] = new_tags
if deleted_tags:
if sync_tags and deleted_tags:
# Delete the local copy of the tags to keep up with the
# remote repository.
for tag_name in deleted_tags:
try:
__salt__['git.tag'](
target,
tag_name,
opts='-d',
user=user,
password=password,
output_encoding=output_encoding)
if not __opts__['test']:
__salt__['git.tag'](
target,
tag_name,
opts='-d',
user=user,
password=password,
output_encoding=output_encoding)
except CommandExecutionError as exc:
ret.setdefault('warnings', []).append(
'Failed to remove local tag \'{0}\':\n\n'
@ -1400,6 +1416,16 @@ def latest(name,
ret['changes'].setdefault(
'deleted_tags', []).append(tag_name)
if ret['changes'].get('deleted_tags'):
comments.append(
'The following tags {0} removed from the local '
'checkout: {1}'.format(
'would be' if __opts__['test']
else 'were',
', '.join(ret['changes']['deleted_tags'])
)
)
if not has_remote_rev:
try:
fetch_changes = __salt__['git.fetch'](

View file

@ -479,7 +479,7 @@ class GitTest(ModuleCase, SaltReturnAssertsMixin):
@with_git_mirror(TEST_REPO)
@uses_git_opts
def test_latest_changed_tags(self, mirror_url, admin_dir, clone_dir):
def test_latest_sync_tags(self, mirror_url, admin_dir, clone_dir):
'''
Test that a removed tag is properly reported as such and removed in the
local clone, and that new tags are reported as new.
@ -502,23 +502,39 @@ class GitTest(ModuleCase, SaltReturnAssertsMixin):
self.run_function('git.tag', [admin_dir, tag2])
self.run_function('git.push', [admin_dir, 'origin', tag2])
# Re-run the state, it should delete the tag from the local clone and
# reflect that the tag was removed in the changes dict.
ret = self.run_state('git.latest', name=mirror_url, target=clone_dir)
# Re-run the state with sync_tags=False. This should NOT delete the tag
# from the local clone, but should report that a tag has been added.
ret = self.run_state('git.latest',
name=mirror_url,
target=clone_dir,
sync_tags=False)
ret = ret[next(iter(ret))]
assert ret['result']
# Make ABSOLUTELY SURE both tags are present, since we shouldn't have
# removed tag1.
all_tags = self.run_function('git.list_tags', [clone_dir])
assert tag1 in all_tags
assert tag2 in all_tags
# Make sure the reported changes are correct
expected_changes = {'new_tags': [tag2]}
assert ret['changes'] == expected_changes, ret['changes']
# Re-run the state with sync_tags=True. This should remove the local
# tag, since it doesn't exist in the remote repository.
ret = self.run_state('git.latest',
name=mirror_url,
target=clone_dir,
sync_tags=True)
ret = ret[next(iter(ret))]
assert ret['result']
expected_changes = {
'deleted_tags': [tag1],
'new_tags': [tag2],
}
# Make ABSOLUTELY SURE the expected tags are present/gone
all_tags = self.run_function('git.list_tags', [clone_dir])
assert tag1 not in all_tags
assert tag2 in all_tags
# Make sure the reported changes are correct
expected_changes = {'deleted_tags': [tag1]}
assert ret['changes'] == expected_changes, ret['changes']
@with_tempdir(create=False)
def test_cloned(self, target):
'''