From 9d19cba65f1d77c52d6b4ebc7d73ca64919718b3 Mon Sep 17 00:00:00 2001 From: Xavier Antoviaque Date: Sat, 29 Aug 2015 16:38:16 +0200 Subject: [PATCH] Update commit hash in instance name when setting branch to tip --- instance/models/instance.py | 14 +++++++++++++- instance/tests/models/test_instance.py | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/instance/models/instance.py b/instance/models/instance.py index 87416b156..8bb302a28 100644 --- a/instance/models/instance.py +++ b/instance/models/instance.py @@ -277,10 +277,22 @@ def set_to_branch_tip(self, branch_name=None, ref_type=None, commit=True): if ref_type is not None: self.ref_type = ref_type self.log('info', 'Setting instance {} to tip of branch {}'.format(self, self.branch_name)) - self.commit_id = github.get_commit_id_from_ref( + new_commit_id = github.get_commit_id_from_ref( self.fork_name, self.branch_name, ref_type=self.ref_type) + + if new_commit_id != self.commit_id: + old_commit_short_id = self.commit_short_id + self.commit_id = new_commit_id + + # Update the hash in the instance title if it is present there + # TODO: Find a better way to handle this - include the hash dynamically? + # TODO: Figure out why the warnings aren't suppressed despite the fact that it's a mixin + if self.name and old_commit_short_id: #pylint: disable=access-member-before-definition + #pylint: disable=attribute-defined-outside-init + self.name = self.name.replace(old_commit_short_id, self.commit_short_id) + if commit: self.save() diff --git a/instance/tests/models/test_instance.py b/instance/tests/models/test_instance.py index d7d7a3890..84c3dcd0d 100644 --- a/instance/tests/models/test_instance.py +++ b/instance/tests/models/test_instance.py @@ -223,6 +223,16 @@ def test_set_to_branch_tip_extra_args(self, mock_get_commit_id_from_ref): self.assertEqual(instance.branch_name, 'new-branch') self.assertEqual(instance.ref_type, 'tag') + @patch('instance.models.instance.github.get_commit_id_from_ref') + def test_set_to_branch_tip_replace_commit_hash(self, mock_get_commit_id_from_ref): + """ + The hash should be updated in the instance name when updating + """ + mock_get_commit_id_from_ref.return_value = '1234567' + 'd' * 33 + instance = OpenEdXInstanceFactory(commit_id='a' * 40, name='Test Instance (aaaaaaa)') + instance.set_to_branch_tip(branch_name='new-branch', ref_type='tag') + self.assertEqual(instance.name, 'Test Instance (1234567)') + class AnsibleInstanceTestCase(TestCase): """