From 555cdadee69cb65768cd3655830f980194e9e607 Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Thu, 16 Aug 2018 13:24:25 +0000 Subject: Resolve "Enable Auto DevOps instance-wide on GitLab.com" --- app/models/project.rb | 8 +++--- app/models/project_auto_devops.rb | 6 +---- app/services/projects/update_service.rb | 2 +- doc/topics/autodevops/index.md | 5 ++++ spec/models/project_auto_devops_spec.rb | 6 ++--- spec/models/project_spec.rb | 48 ++++++++++++++++++++++----------- spec/spec_helper.rb | 7 +++++ 7 files changed, 54 insertions(+), 28 deletions(-) diff --git a/app/models/project.rb b/app/models/project.rb index 7735f23cb9e..94c1d60f071 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -27,6 +27,7 @@ class Project < ActiveRecord::Base include FastDestroyAll::Helpers include WithUploads include BatchDestroyDependentAssociations + include FeatureGate extend Gitlab::Cache::RequestCache extend Gitlab::ConfigHelper @@ -519,18 +520,19 @@ class Project < ActiveRecord::Base def auto_devops_enabled? if auto_devops&.enabled.nil? - Gitlab::CurrentSettings.auto_devops_enabled? + has_auto_devops_implicitly_enabled? else auto_devops.enabled? end end def has_auto_devops_implicitly_enabled? - auto_devops&.enabled.nil? && Gitlab::CurrentSettings.auto_devops_enabled? + auto_devops&.enabled.nil? && + (Gitlab::CurrentSettings.auto_devops_enabled? || Feature.enabled?(:force_autodevops_on_by_default, self)) end def has_auto_devops_implicitly_disabled? - auto_devops&.enabled.nil? && !Gitlab::CurrentSettings.auto_devops_enabled? + auto_devops&.enabled.nil? && !(Gitlab::CurrentSettings.auto_devops_enabled? || Feature.enabled?(:force_autodevops_on_by_default, self)) end def empty_repo? diff --git a/app/models/project_auto_devops.rb b/app/models/project_auto_devops.rb index 155400d1a43..dc6736dd9cd 100644 --- a/app/models/project_auto_devops.rb +++ b/app/models/project_auto_devops.rb @@ -47,12 +47,8 @@ class ProjectAutoDevops < ActiveRecord::Base end def needs_to_create_deploy_token? - auto_devops_enabled? && + project.auto_devops_enabled? && !project.public? && !project.deploy_tokens.find_by(name: DeployToken::GITLAB_DEPLOY_TOKEN_NAME).present? end - - def auto_devops_enabled? - Gitlab::CurrentSettings.auto_devops_enabled? || enabled? - end end diff --git a/app/services/projects/update_service.rb b/app/services/projects/update_service.rb index 97f181ccea8..e390d7a04c3 100644 --- a/app/services/projects/update_service.rb +++ b/app/services/projects/update_service.rb @@ -30,7 +30,7 @@ module Projects def run_auto_devops_pipeline? return false if project.repository.gitlab_ci_yml || !project.auto_devops&.previous_changes&.include?('enabled') - project.auto_devops.enabled? || (project.auto_devops.enabled.nil? && Gitlab::CurrentSettings.auto_devops_enabled?) + project.auto_devops_enabled? end private diff --git a/doc/topics/autodevops/index.md b/doc/topics/autodevops/index.md index f5574506595..0474182e324 100644 --- a/doc/topics/autodevops/index.md +++ b/doc/topics/autodevops/index.md @@ -233,6 +233,11 @@ in **Admin Area > Settings > Continuous Integration and Deployment**. Doing that all the projects that haven't explicitly set an option will have Auto DevOps enabled by default. +NOTE: **Note:** +There is also a feature flag to enable Auto DevOps to a percentage of projects +which can be enabled from the console with +`Feature.get(:force_autodevops_on_by_default).enable_percentage_of_actors(10)`. + ### Deployment strategy > [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/38542) in GitLab 11.0. diff --git a/spec/models/project_auto_devops_spec.rb b/spec/models/project_auto_devops_spec.rb index 749b2094787..797d767465a 100644 --- a/spec/models/project_auto_devops_spec.rb +++ b/spec/models/project_auto_devops_spec.rb @@ -100,7 +100,7 @@ describe ProjectAutoDevops do end end - describe '#set_gitlab_deploy_token' do + describe '#create_gitlab_deploy_token' do let(:auto_devops) { build(:project_auto_devops, project: project) } context 'when the project is public' do @@ -144,9 +144,9 @@ describe ProjectAutoDevops do end end - context 'when autodevops is enabled at instancel level' do + context 'when autodevops is enabled at instance level' do let(:project) { create(:project, :repository, :internal) } - let(:auto_devops) { build(:project_auto_devops, :disabled, project: project) } + let(:auto_devops) { build(:project_auto_devops, enabled: nil, project: project) } it 'should create a deploy token' do allow(Gitlab::CurrentSettings).to receive(:auto_devops_enabled?).and_return(true) diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 076de06cf99..d8a5e5f6869 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3259,6 +3259,11 @@ describe Project do end describe '#auto_devops_enabled?' do + before do + allow(Feature).to receive(:enabled?).and_call_original + Feature.get(:force_autodevops_on_by_default).enable_percentage_of_actors(0) + end + set(:project) { create(:project) } subject { project.auto_devops_enabled? } @@ -3268,19 +3273,14 @@ describe Project do stub_application_setting(auto_devops_enabled: true) end - it 'auto devops is implicitly enabled' do - expect(project.auto_devops).to be_nil - expect(project).to be_auto_devops_enabled - end + it { is_expected.to be_truthy } context 'when explicitly enabled' do before do create(:project_auto_devops, project: project) end - it "auto devops is enabled" do - expect(project).to be_auto_devops_enabled - end + it { is_expected.to be_truthy } end context 'when explicitly disabled' do @@ -3288,9 +3288,7 @@ describe Project do create(:project_auto_devops, project: project, enabled: false) end - it "auto devops is disabled" do - expect(project).not_to be_auto_devops_enabled - end + it { is_expected.to be_falsey } end end @@ -3299,19 +3297,22 @@ describe Project do stub_application_setting(auto_devops_enabled: false) end - it 'auto devops is implicitly disabled' do - expect(project.auto_devops).to be_nil - expect(project).not_to be_auto_devops_enabled - end + it { is_expected.to be_falsey } context 'when explicitly enabled' do before do create(:project_auto_devops, project: project) end - it "auto devops is enabled" do - expect(project).to be_auto_devops_enabled + it { is_expected.to be_truthy } + end + + context 'when force_autodevops_on_by_default is enabled for the project' do + before do + Feature.get(:force_autodevops_on_by_default).enable_percentage_of_actors(100) end + + it { is_expected.to be_truthy } end end end @@ -3361,6 +3362,11 @@ describe Project do end describe '#has_auto_devops_implicitly_disabled?' do + before do + allow(Feature).to receive(:enabled?).and_call_original + Feature.get(:force_autodevops_on_by_default).enable_percentage_of_actors(0) + end + set(:project) { create(:project) } context 'when enabled in settings' do @@ -3382,6 +3388,16 @@ describe Project do expect(project).to have_auto_devops_implicitly_disabled end + context 'when force_autodevops_on_by_default is enabled for the project' do + before do + Feature.get(:force_autodevops_on_by_default).enable_percentage_of_actors(100) + end + + it 'does not have auto devops implicitly disabled' do + expect(project).not_to have_auto_devops_implicitly_disabled + end + end + context 'when explicitly disabled' do before do create(:project_auto_devops, project: project, enabled: false) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bd564cc60a6..f4441a6b700 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -111,6 +111,13 @@ RSpec.configure do |config| config.before(:example) do # Enable all features by default for testing allow(Feature).to receive(:enabled?) { true } + + # The following can be removed when we remove the staged rollout strategy + # and we can just enable it using instance wide settings + # (ie. ApplicationSetting#auto_devops_enabled) + allow(Feature).to receive(:enabled?) + .with(:force_autodevops_on_by_default, anything) + .and_return(false) end config.before(:example, :request_store) do -- cgit v1.2.1