Skip to content

Commit a68a6bf

Browse files
authored
Merge pull request #4743 from StackStorm/issue-354/install-pack-latest-tag
Install pack with the latest tag
2 parents c0458f1 + 8547e1c commit a68a6bf

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Changelog
44
in development
55
--------------
66

7+
Changed
8+
~~~~~~~
9+
10+
* Install pack with the latest tag version if it exists when branch is not specialized.
11+
(improvement) #4743
12+
713
Fixed
814
~~~~~
915

contrib/packs/tests/test_action_download.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ def mock_is_dir_func(path):
8181
return original_is_dir_func(path)
8282

8383

84+
def mock_get_gitref(repo, ref):
85+
"""
86+
Mock get_gitref function which return mocked object if ref passed is
87+
PACK_INDEX['test']['version']
88+
"""
89+
if PACK_INDEX['test']['version'] in ref:
90+
if ref[0] == 'v':
91+
return mock.MagicMock(hexsha=PACK_INDEX['test']['version'])
92+
else:
93+
return None
94+
elif ref:
95+
return mock.MagicMock(hexsha="abcDef")
96+
else:
97+
return None
98+
99+
84100
@mock.patch.object(pack_service, 'fetch_pack_index', mock.MagicMock(return_value=(PACK_INDEX, {})))
85101
class DownloadGitRepoActionTestCase(BaseActionTestCase):
86102
action_cls = DownloadGitRepoAction
@@ -520,7 +536,7 @@ def side_effect(ref):
520536

521537
# Fool _get_gitref into working when its ref == our ref
522538
def fake_commit(arg_ref):
523-
if arg_ref == ref:
539+
if not ref or arg_ref == ref:
524540
return gitref
525541
else:
526542
raise BadName()
@@ -572,3 +588,28 @@ def test_run_pack_download_local_directory(self):
572588
destination_path = os.path.join(self.repo_base, 'test4')
573589
self.assertTrue(os.path.exists(destination_path))
574590
self.assertTrue(os.path.exists(os.path.join(destination_path, 'pack.yaml')))
591+
592+
@mock.patch('st2common.util.pack_management.get_gitref', mock_get_gitref)
593+
def test_run_pack_download_with_tag(self):
594+
action = self.get_action_instance()
595+
result = action.run(packs=['test'], abs_repo_base=self.repo_base)
596+
temp_dir = hashlib.md5(PACK_INDEX['test']['repo_url'].encode()).hexdigest()
597+
598+
self.assertEqual(result, {'test': 'Success.'})
599+
self.clone_from.assert_called_once_with(PACK_INDEX['test']['repo_url'],
600+
os.path.join(os.path.expanduser('~'), temp_dir))
601+
self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml')))
602+
603+
# Check repo.git.checkout is called three times
604+
self.assertEqual(self.repo_instance.git.checkout.call_count, 3)
605+
606+
# Check repo.git.checkout called with latest tag or branch
607+
self.assertEqual(PACK_INDEX['test']['version'],
608+
self.repo_instance.git.checkout.call_args_list[1][0][0])
609+
610+
# Check repo.git.checkout called with head
611+
self.assertEqual(self.repo_instance.head.reference,
612+
self.repo_instance.git.checkout.call_args_list[2][0][0])
613+
614+
self.repo_instance.git.branch.assert_called_with(
615+
'-f', self.repo_instance.head.reference, PACK_INDEX['test']['version'])

st2common/st2common/util/pack_management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def get_repo_url(pack, proxy_config=None):
370370
if not pack:
371371
raise Exception('No record of the "%s" pack in the index.' % (name_or_url))
372372

373-
return (pack['repo_url'], version)
373+
return (pack['repo_url'], version or pack['version'])
374374
else:
375375
return (eval_repo_url(name_or_url), version)
376376

0 commit comments

Comments
 (0)