diff --git a/app/models/project.rb b/app/models/project.rb index 8abbb92da62..50c404c300a 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -234,7 +234,7 @@ class Project < ActiveRecord::Base has_many :custom_attributes, class_name: 'ProjectCustomAttribute' has_many :project_badges, class_name: 'ProjectBadge' - has_one :ci_cd_settings, class_name: 'ProjectCiCdSetting' + has_one :ci_cd_settings, class_name: 'ProjectCiCdSetting', inverse_of: :project, autosave: true accepts_nested_attributes_for :variables, allow_destroy: true accepts_nested_attributes_for :project_feature, update_only: true @@ -331,6 +331,11 @@ class Project < ActiveRecord::Base scope :with_issues_available_for_user, ->(current_user) { with_feature_available_for_user(:issues, current_user) } scope :with_merge_requests_enabled, -> { with_feature_enabled(:merge_requests) } + scope :with_group_runners_enabled, -> do + joins(:ci_cd_settings) + .where(project_ci_cd_settings: { group_runners_enabled: true }) + end + enum auto_cancel_pending_pipelines: { disabled: 0, enabled: 1 } chronic_duration_attr :build_timeout_human_readable, :build_timeout, default: 3600 diff --git a/app/models/project_ci_cd_setting.rb b/app/models/project_ci_cd_setting.rb index 9f10a93148c..588cced5781 100644 --- a/app/models/project_ci_cd_setting.rb +++ b/app/models/project_ci_cd_setting.rb @@ -1,5 +1,5 @@ class ProjectCiCdSetting < ActiveRecord::Base - belongs_to :project + belongs_to :project, inverse_of: :ci_cd_settings # The version of the schema that first introduced this model/table. MINIMUM_SCHEMA_VERSION = 20180403035759 diff --git a/app/services/ci/register_job_service.rb b/app/services/ci/register_job_service.rb index 8f8a5fbb2b0..a7d8ad93f38 100644 --- a/app/services/ci/register_job_service.rb +++ b/app/services/ci/register_job_service.rb @@ -91,7 +91,10 @@ module Ci def builds_for_group_runner hierarchy_groups = Gitlab::GroupHierarchy.new(runner.groups).base_and_descendants projects = Project.where(namespace_id: hierarchy_groups) - new_builds.where(project: projects.without_deleted.with_builds_enabled).order('created_at ASC') + .with_group_runners_enabled + .with_builds_enabled + .without_deleted + new_builds.where(project: projects).order('created_at ASC') end def running_builds_for_shared_runners diff --git a/spec/services/ci/register_job_service_spec.rb b/spec/services/ci/register_job_service_spec.rb index 256d0027d72..8063bc7e1ac 100644 --- a/spec/services/ci/register_job_service_spec.rb +++ b/spec/services/ci/register_job_service_spec.rb @@ -2,13 +2,13 @@ require 'spec_helper' module Ci describe RegisterJobService do - let!(:project) { create :project, shared_runners_enabled: false } - let!(:group) { create :group } - let!(:pipeline) { create :ci_pipeline, project: project } - let!(:pending_job) { create :ci_build, pipeline: pipeline } - let!(:shared_runner) { create :ci_runner, is_shared: true } - let!(:specific_runner) { create :ci_runner, is_shared: false } - let!(:group_runner) { create :ci_runner, groups: [group], runner_type: :group_type } + set(:group) { create(:group) } + set(:project) { create(:project, group: group, shared_runners_enabled: false, group_runners_enabled: false) } + set(:pipeline) { create(:ci_pipeline, project: project) } + let!(:shared_runner) { create(:ci_runner, is_shared: true) } + let!(:specific_runner) { create(:ci_runner, is_shared: false) } + let!(:group_runner) { create(:ci_runner, groups: [group], runner_type: :group_type) } + let!(:pending_job) { create(:ci_build, pipeline: pipeline) } before do specific_runner.assign_to(project) @@ -152,7 +152,7 @@ module Ci context 'disallow when builds are disabled' do before do - project.update(shared_runners_enabled: true) + project.update(shared_runners_enabled: true, group_runners_enabled: true) project.project_feature.update_attribute(:builds_access_level, ProjectFeature::DISABLED) end @@ -162,7 +162,13 @@ module Ci it { expect(build).to be_nil } end - context 'and uses specific runner' do + context 'and uses group runner' do + let(:build) { execute(group_runner) } + + it { expect(build).to be_nil } + end + + context 'and uses project runner' do let(:build) { execute(specific_runner) } it { expect(build).to be_nil } @@ -171,7 +177,7 @@ module Ci context 'allow group runners' do before do - project.update!(group_runners_enabled: true, group: group) + project.update!(group_runners_enabled: true) end context 'for multiple builds' do @@ -230,7 +236,7 @@ module Ci context 'disallow group runners' do before do - project.update(group_runners_enabled: false) + project.update!(group_runners_enabled: false) end context 'group runner' do