summaryrefslogtreecommitdiff
path: root/app/models/ci/runner.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/ci/runner.rb')
-rw-r--r--app/models/ci/runner.rb52
1 files changed, 42 insertions, 10 deletions
diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb
index 530eacf4be0..57edd6a4956 100644
--- a/app/models/ci/runner.rb
+++ b/app/models/ci/runner.rb
@@ -12,9 +12,9 @@ module Ci
FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level maximum_timeout_human_readable].freeze
has_many :builds
- has_many :runner_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
+ has_many :runner_projects, inverse_of: :runner, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :projects, through: :runner_projects
- has_many :runner_namespaces
+ has_many :runner_namespaces, inverse_of: :runner
has_many :groups, through: :runner_namespaces
has_one :last_build, ->() { order('id DESC') }, class_name: 'Ci::Build'
@@ -56,10 +56,15 @@ module Ci
end
validate :tag_constraints
- validate :either_projects_or_group
validates :access_level, presence: true
validates :runner_type, presence: true
+ validate :no_projects, unless: :project_type?
+ validate :no_groups, unless: :group_type?
+ validate :any_project, if: :project_type?
+ validate :exactly_one_group, if: :group_type?
+ validate :validate_is_shared
+
acts_as_taggable
after_destroy :cleanup_runner_queue
@@ -115,8 +120,15 @@ module Ci
raise ArgumentError, 'Transitioning a group runner to a project runner is not supported'
end
- self.save
- project.runner_projects.create(runner_id: self.id)
+ begin
+ transaction do
+ self.projects << project
+ self.save!
+ end
+ rescue ActiveRecord::RecordInvalid => e
+ self.errors.add(:assign_to, e.message)
+ false
+ end
end
def display_name
@@ -253,13 +265,33 @@ module Ci
self.class.owned_or_shared(project_id).where(id: self.id).any?
end
- def either_projects_or_group
- if groups.many?
- errors.add(:runner, 'can only be assigned to one group')
+ def no_projects
+ if projects.any?
+ errors.add(:runner, 'cannot have projects assigned')
+ end
+ end
+
+ def no_groups
+ if groups.any?
+ errors.add(:runner, 'cannot have groups assigned')
+ end
+ end
+
+ def any_project
+ unless projects.any?
+ errors.add(:runner, 'needs to be assigned to at least one project')
+ end
+ end
+
+ def exactly_one_group
+ unless groups.one?
+ errors.add(:runner, 'needs to be assigned to exactly one group')
end
+ end
- if assigned_to_group? && assigned_to_project?
- errors.add(:runner, 'can only be assigned either to projects or to a group')
+ def validate_is_shared
+ unless is_shared? == instance_type?
+ errors.add(:is_shared, 'is not equal to instance_type?')
end
end