diff --git a/app/models/concerns/project_api_compatibility.rb b/app/models/concerns/project_api_compatibility.rb index cb00efb06df..631b2a11e9a 100644 --- a/app/models/concerns/project_api_compatibility.rb +++ b/app/models/concerns/project_api_compatibility.rb @@ -9,12 +9,10 @@ module ProjectAPICompatibility end def auto_devops_enabled=(value) - self.build_auto_devops if self.auto_devops&.enabled.nil? - self.auto_devops.update! enabled: value + (auto_devops || build_auto_devops).enabled = value end def auto_devops_deploy_strategy=(value) - self.build_auto_devops if self.auto_devops&.enabled.nil? - self.auto_devops.update! deploy_strategy: value + (auto_devops || build_auto_devops).deploy_strategy = value end end diff --git a/app/models/project.rb b/app/models/project.rb index 2906aca75fc..8dfe2212282 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -277,7 +277,7 @@ class Project < ApplicationRecord has_many :project_deploy_tokens has_many :deploy_tokens, through: :project_deploy_tokens - has_one :auto_devops, class_name: 'ProjectAutoDevops' + has_one :auto_devops, class_name: 'ProjectAutoDevops', inverse_of: :project, autosave: true has_many :custom_attributes, class_name: 'ProjectCustomAttribute' has_many :project_badges, class_name: 'ProjectBadge' diff --git a/app/models/project_auto_devops.rb b/app/models/project_auto_devops.rb index 67c12363a3c..f39f54f0434 100644 --- a/app/models/project_auto_devops.rb +++ b/app/models/project_auto_devops.rb @@ -5,7 +5,7 @@ class ProjectAutoDevops < ApplicationRecord ignore_column :domain - belongs_to :project + belongs_to :project, inverse_of: :auto_devops enum deploy_strategy: { continuous: 0, diff --git a/changelogs/unreleased/64731-fix-project-auto-devops-api.yml b/changelogs/unreleased/64731-fix-project-auto-devops-api.yml new file mode 100644 index 00000000000..7fe2c036773 --- /dev/null +++ b/changelogs/unreleased/64731-fix-project-auto-devops-api.yml @@ -0,0 +1,5 @@ +--- +title: Fix the project auto devops API +merge_request: 30946 +author: +type: fixed diff --git a/spec/models/concerns/project_api_compatibility_spec.rb b/spec/models/concerns/project_api_compatibility_spec.rb index 8cecd4fe7bc..f5722f88aac 100644 --- a/spec/models/concerns/project_api_compatibility_spec.rb +++ b/spec/models/concerns/project_api_compatibility_spec.rb @@ -16,23 +16,44 @@ describe ProjectAPICompatibility do expect(project.build_allow_git_fetch).to eq(false) end - # auto_devops_enabled - it "converts auto_devops_enabled=false to auto_devops_enabled?=false" do - expect(project.auto_devops_enabled?).to eq(true) - project.update!(auto_devops_enabled: false) - expect(project.auto_devops_enabled?).to eq(false) + describe '#auto_devops_enabled' do + where( + initial: [:missing, nil, false, true], + final: [nil, false, true] + ) + + with_them do + before do + project.build_auto_devops(enabled: initial) unless initial == :missing + end + + # Implicit auto devops when enabled is nil + let(:expected) { final.nil? ? true : final } + + it 'sets the correct value' do + project.update!(auto_devops_enabled: final) + + expect(project.auto_devops_enabled?).to eq(expected) + end + end end - it "converts auto_devops_enabled=true to auto_devops_enabled?=true" do - expect(project.auto_devops_enabled?).to eq(true) - project.update!(auto_devops_enabled: true) - expect(project.auto_devops_enabled?).to eq(true) - end + describe '#auto_devops_deploy_strategy' do + where( + initial: [:missing, *ProjectAutoDevops.deploy_strategies.keys], + final: ProjectAutoDevops.deploy_strategies.keys + ) - # auto_devops_deploy_strategy - it "converts auto_devops_deploy_strategy=timed_incremental to auto_devops.deploy_strategy=timed_incremental" do - expect(project.auto_devops).to be_nil - project.update!(auto_devops_deploy_strategy: 'timed_incremental') - expect(project.auto_devops.deploy_strategy).to eq('timed_incremental') + with_them do + before do + project.build_auto_devops(deploy_strategy: initial) unless initial == :missing + end + + it 'sets the correct value' do + project.update!(auto_devops_deploy_strategy: final) + + expect(project.auto_devops.deploy_strategy).to eq(final) + end + end end end