diff options
author | Alexis Reigel <alexis.reigel.ext@siemens.com> | 2017-12-07 17:21:16 +0100 |
---|---|---|
committer | Alexis Reigel <alexis.reigel.ext@siemens.com> | 2018-02-28 09:51:22 +0100 |
commit | 7918fd1e0b5c2ddca8930c7f2c8c63a97a90ff08 (patch) | |
tree | b30526f381a99b21ef9434d9d39a31a8ba2cb383 | |
parent | 17e96889c117bc7b1b8f5d1c3e87585b258e7911 (diff) | |
download | gitlab-ce-7918fd1e0b5c2ddca8930c7f2c8c63a97a90ff08.tar.gz |
ci runners: assigned to either projects or group
-rw-r--r-- | app/models/ci/runner.rb | 11 | ||||
-rw-r--r-- | spec/models/ci/runner_spec.rb | 57 |
2 files changed, 68 insertions, 0 deletions
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index dd03ad58503..3649c12f39f 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -49,6 +49,7 @@ module Ci end validate :tag_constraints + validate :either_projects_or_group validates :access_level, presence: true acts_as_taggable @@ -221,6 +222,16 @@ module Ci self.class.owned_or_shared(project_id).where(id: self.id).any? end + def either_projects_or_group + if groups.length > 1 + errors.add(:runner, 'can only be assigned to one group') + end + + if groups.length > 0 && projects.length > 0 + errors.add(:runner, 'can only be assigned either to projects or to a group') + end + end + def accepting_tags?(build) (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty? end diff --git a/spec/models/ci/runner_spec.rb b/spec/models/ci/runner_spec.rb index ba0db43a1e7..fb724f682a5 100644 --- a/spec/models/ci/runner_spec.rb +++ b/spec/models/ci/runner_spec.rb @@ -19,6 +19,63 @@ describe Ci::Runner do end end end + + context 'either_projects_or_group' do + it 'disallows assigning to a group if already assigned to a group' do + group = create(:group) + runner = create(:ci_runner, groups: [group]) + + runner.groups << build(:group) + + expect(runner).not_to be_valid + expect(runner.errors.full_messages).to eq ['Runner can only be assigned to one group'] + end + + it 'disallows assigning to a group if already assigned to a project' do + project = create(:project) + runner = create(:ci_runner, projects: [project]) + + runner.groups << build(:group) + + expect(runner).not_to be_valid + expect(runner.errors.full_messages).to eq ['Runner can only be assigned either to projects or to a group'] + end + + it 'disallows assigning to a project if already assigned to a group' do + group = create(:group) + runner = create(:ci_runner, groups: [group]) + + runner.projects << build(:project) + + expect(runner).not_to be_valid + expect(runner.errors.full_messages).to eq ['Runner can only be assigned either to projects or to a group'] + end + + it 'allows assigning to a group if not assigned to a group nor a project' do + runner = create(:ci_runner) + + runner.groups << build(:group) + + expect(runner).to be_valid + end + + it 'allows assigning to a project if not assigned to a group nor a project' do + runner = create(:ci_runner) + + runner.projects << build(:project) + + expect(runner).to be_valid + end + + it 'allows assigning to a project if already assigned to a project' do + project = create(:project) + runner = create(:ci_runner, projects: [project]) + + runner.projects << build(:project) + + expect(runner).to be_valid + end + end end describe '#access_level' do |