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
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: if 'allow_override' in attrs and self.instance:
# case where user is turning off this project setting # case where user is turning off this project setting
if self.instance.allow_override and not attrs['allow_override']: if self.instance.allow_override and not attrs['allow_override']:
used_by = ( used_by = set(
set(JobTemplate.objects.filter(project=self.instance, scm_branch__isnull=False).values_list('pk', flat=True)) | JobTemplate.objects.filter(
set(JobTemplate.objects.filter(project=self.instance, ask_scm_branch_on_launch=True).values_list('pk', flat=True)) 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: if used_by:
raise serializers.ValidationError({ 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]) ' '.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, user=admin_user,
expect=400 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 'ids: 2' in str(r2.data['allow_override'])
assert Project.objects.get(pk=r1.data['id']).allow_override is True assert Project.objects.get(pk=r1.data['id']).allow_override is True