Fix logic for turning off override behavior

This commit is contained in:
AlanCoding 2019-08-12 11:54:42 -04:00
parent 3df476e3f6
commit be21a8bcb4
No known key found for this signature in database
GPG Key ID: FD2C3C012A72926B
2 changed files with 7 additions and 5 deletions

View File

@ -1393,13 +1393,15 @@ class ProjectSerializer(UnifiedJobTemplateSerializer, ProjectOptionsSerializer):
if 'allow_override' in attrs and self.instance:
# case where user is turning off this project setting
if self.instance.allow_override and not attrs['allow_override']:
used_by = (
set(JobTemplate.objects.filter(project=self.instance, scm_branch__isnull=False).values_list('pk', flat=True)) |
set(JobTemplate.objects.filter(project=self.instance, ask_scm_branch_on_launch=True).values_list('pk', flat=True))
used_by = set(
JobTemplate.objects.filter(
models.Q(project=self.instance),
models.Q(ask_scm_branch_on_launch=True) | ~models.Q(scm_branch="")
).values_list('pk', flat=True)
)
if used_by:
raise serializers.ValidationError({
'allow_override': _('One or more job templates already specify a branch for this project (ids: {}).').format(
'allow_override': _('One or more job templates depend on branch override behavior for this project (ids: {}).').format(
' '.join([str(pk) for pk in used_by])
)})

View File

@ -70,7 +70,7 @@ def test_no_changing_overwrite_behavior_if_used(post, patch, organization, admin
user=admin_user,
expect=400
)
assert 'job templates already specify a branch for this project' in str(r2.data['allow_override'])
assert 'job templates depend on branch override behavior for this project' in str(r2.data['allow_override'])
assert 'ids: 2' in str(r2.data['allow_override'])
assert Project.objects.get(pk=r1.data['id']).allow_override is True