update project if certain params changed

This commit is contained in:
AlanCoding 2018-05-11 09:41:21 -04:00
parent 1b64e52f64
commit b79606d9d3
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B
3 changed files with 54 additions and 2 deletions

View File

@ -241,6 +241,7 @@ class Project(UnifiedJobTemplate, ProjectOptions, ResourceMixin, CustomVirtualEn
SOFT_UNIQUE_TOGETHER = [('polymorphic_ctype', 'name', 'organization')]
FIELDS_TO_PRESERVE_AT_COPY = ['labels', 'instance_groups', 'credentials']
FIELDS_TO_DISCARD_AT_COPY = ['local_path']
FIELDS_TRIGGER_UPDATE = frozenset(['scm_url', 'scm_branch', 'scm_type'])
class Meta:
app_label = 'main'
@ -323,6 +324,11 @@ class Project(UnifiedJobTemplate, ProjectOptions, ResourceMixin, CustomVirtualEn
['name', 'description', 'schedule']
)
def __init__(self, *args, **kwargs):
r = super(Project, self).__init__(*args, **kwargs)
self._prior_values_store = self._current_sensitive_fields()
return r
def save(self, *args, **kwargs):
new_instance = not bool(self.pk)
# If update_fields has been specified, add our field names to it,
@ -354,9 +360,22 @@ class Project(UnifiedJobTemplate, ProjectOptions, ResourceMixin, CustomVirtualEn
with disable_activity_stream():
self.save(update_fields=update_fields)
# If we just created a new project with SCM, start the initial update.
if new_instance and self.scm_type and not skip_update:
# also update if certain fields have changed
relevant_change = False
new_values = self._current_sensitive_fields()
if hasattr(self, '_prior_values_store') and self._prior_values_store != new_values:
relevant_change = True
self._prior_values_store = new_values
if (relevant_change or new_instance) and (not skip_update) and self.scm_type:
self.update()
def _current_sensitive_fields(self):
new_values = {}
for attr, val in self.__dict__.items():
if attr in Project.FIELDS_TRIGGER_UPDATE:
new_values[attr] = val
return new_values
def _get_current_status(self):
if self.scm_type:
if self.current_job and self.current_job.status:

View File

@ -0,0 +1,33 @@
import pytest
import mock
from awx.main.models import Project
@pytest.mark.django_db
def test_project_initial_update():
with mock.patch.object(Project, "update") as mock_update:
Project.objects.create(name='foo', scm_type='git')
mock_update.assert_called_once_with()
@pytest.mark.django_db
def test_does_not_update_nonsensitive_change(project):
with mock.patch.object(Project, "update") as mock_update:
project.scm_update_on_launch = not project.scm_update_on_launch
project.save()
mock_update.assert_not_called()
@pytest.mark.django_db
def test_sensitive_change_triggers_update(project):
with mock.patch.object(Project, "update") as mock_update:
project.scm_url = 'https://foo.invalid'
project.save()
mock_update.assert_called_once_with()
# test other means of initialization
project = Project.objects.get(pk=project.pk)
with mock.patch.object(Project, "update") as mock_update:
project.scm_url = 'https://foo2.invalid'
project.save()
mock_update.assert_called_once_with()

View File

@ -113,7 +113,7 @@ def test_single_job_dependencies_project_launch(default_instance_group, job_temp
p.scm_update_cache_timeout = 0
p.scm_type = "git"
p.scm_url = "http://github.com/ansible/ansible.git"
p.save()
p.save(skip_update=True)
with mock.patch("awx.main.scheduler.TaskManager.start_task"):
tm = TaskManager()
with mock.patch.object(TaskManager, "create_project_update", wraps=tm.create_project_update) as mock_pu: