diff options
Diffstat (limited to 'app/models/environment.rb')
-rw-r--r-- | app/models/environment.rb | 81 |
1 files changed, 41 insertions, 40 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb index 87bdb52b58b..8671285be1a 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -3,15 +3,15 @@ class Environment < ActiveRecord::Base include Gitlab::Utils::StrongMemoize # Used to generate random suffixes for the slug - LETTERS = 'a'..'z' - NUMBERS = '0'..'9' + LETTERS = "a".."z" + NUMBERS = "0".."9" SUFFIX_CHARS = LETTERS.to_a + NUMBERS.to_a belongs_to :project, required: true has_many :deployments, -> { success }, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent - has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment' + has_one :last_deployment, -> { success.order("deployments.id DESC") }, class_name: "Deployment" before_validation :nullify_external_url before_validation :generate_slug, if: ->(env) { env.slug.blank? } @@ -19,23 +19,23 @@ class Environment < ActiveRecord::Base before_save :set_environment_type validates :name, - presence: true, - uniqueness: { scope: :project_id }, - length: { maximum: 255 }, - format: { with: Gitlab::Regex.environment_name_regex, - message: Gitlab::Regex.environment_name_regex_message } + presence: true, + uniqueness: {scope: :project_id}, + length: {maximum: 255}, + format: {with: Gitlab::Regex.environment_name_regex, + message: Gitlab::Regex.environment_name_regex_message,} validates :slug, - presence: true, - uniqueness: { scope: :project_id }, - length: { maximum: 24 }, - format: { with: Gitlab::Regex.environment_slug_regex, - message: Gitlab::Regex.environment_slug_regex_message } + presence: true, + uniqueness: {scope: :project_id}, + length: {maximum: 24}, + format: {with: Gitlab::Regex.environment_slug_regex, + message: Gitlab::Regex.environment_slug_regex_message,} validates :external_url, - length: { maximum: 255 }, - allow_nil: true, - url: true + length: {maximum: 255}, + allow_nil: true, + url: true delegate :stop_action, :manual_actions, to: :last_deployment, allow_nil: true @@ -44,22 +44,22 @@ class Environment < ActiveRecord::Base scope :order_by_last_deployed_at, -> do max_deployment_id_sql = Deployment.select(Deployment.arel_table[:id].maximum) - .where(Deployment.arel_table[:environment_id].eq(arel_table[:id])) - .to_sql - order(Gitlab::Database.nulls_first_order("(#{max_deployment_id_sql})", 'ASC')) + .where(Deployment.arel_table[:environment_id].eq(arel_table[:id])) + .to_sql + order(Gitlab::Database.nulls_first_order("(#{max_deployment_id_sql})", "ASC")) end scope :in_review_folder, -> { where(environment_type: "review") } - scope :for_name, -> (name) { where(name: name) } + scope :for_name, ->(name) { where(name: name) } ## # Search environments which have names like the given query. # Do not set a large limit unless you've confirmed that it works on gitlab.com scale. - scope :for_name_like, -> (query, limit: 5) do - where('name LIKE ?', "#{sanitize_sql_like(query)}%").limit(limit) + scope :for_name_like, ->(query, limit: 5) do + where("name LIKE ?", "#{sanitize_sql_like(query)}%").limit(limit) end - scope :for_project, -> (project) { where(project_id: project) } - scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id').where(sha: sha)) } + scope :for_project, ->(project) { where(project_id: project) } + scope :with_deployment, ->(sha) { where("EXISTS (?)", Deployment.select(1).where("deployments.environment_id = environments.id").where(sha: sha)) } state_machine :state, initial: :available do event :start do @@ -84,8 +84,8 @@ class Environment < ActiveRecord::Base def predefined_variables Gitlab::Ci::Variables::Collection.new - .append(key: 'CI_ENVIRONMENT_NAME', value: name) - .append(key: 'CI_ENVIRONMENT_SLUG', value: slug) + .append(key: "CI_ENVIRONMENT_NAME", value: name) + .append(key: "CI_ENVIRONMENT_SLUG", value: slug) end def recently_updated_on_branch?(ref) @@ -93,11 +93,11 @@ class Environment < ActiveRecord::Base end def nullify_external_url - self.external_url = nil if self.external_url.blank? + self.external_url = nil if external_url.blank? end def set_environment_type - names = name.split('/') + names = name.split("/") self.environment_type = names.many? ? names.first : nil end @@ -121,7 +121,7 @@ class Environment < ActiveRecord::Base return nil unless ref - deployment_iid = ref.split('/').last + deployment_iid = ref.split("/").last deployments.find_by(iid: deployment_iid) end @@ -132,7 +132,7 @@ class Environment < ActiveRecord::Base def formatted_external_url return nil unless external_url - external_url.gsub(%r{\A.*?://}, '') + external_url.gsub(%r{\A.*?://}, "") end def stop_action_available? @@ -193,25 +193,25 @@ class Environment < ActiveRecord::Base # * cannot end with `-` def generate_slug # Lowercase letters and numbers only - slugified = +name.to_s.downcase.gsub(/[^a-z0-9]/, '-') + slugified = +name.to_s.downcase.gsub(/[^a-z0-9]/, "-") # Must start with a letter - slugified = 'env-' + slugified unless LETTERS.cover?(slugified[0]) + slugified = "env-" + slugified unless LETTERS.cover?(slugified[0]) # Repeated dashes are invalid (OpenShift limitation) - slugified.gsub!(/\-+/, '-') + slugified.gsub!(/\-+/, "-") # Maximum length: 24 characters (OpenShift limitation) slugified = slugified[0..23] # Cannot end with a dash (Kubernetes label limitation) - slugified.chop! if slugified.end_with?('-') + slugified.chop! if slugified.end_with?("-") # Add a random suffix, shortening the current string if necessary, if it # has been slugified. This ensures uniqueness. if slugified != name slugified = slugified[0..16] - slugified << '-' unless slugified.end_with?('-') + slugified << "-" unless slugified.end_with?("-") slugified << random_suffix end @@ -219,12 +219,12 @@ class Environment < ActiveRecord::Base end def external_url_for(path, commit_sha) - return unless self.external_url + return unless external_url public_path = project.public_path_for_source_path(path, commit_sha) return unless public_path - [external_url, public_path].join('/') + [external_url, public_path].join("/") end def expire_etag_cache @@ -236,11 +236,12 @@ class Environment < ActiveRecord::Base def etag_cache_key Gitlab::Routing.url_helpers.project_environments_path( project, - format: :json) + format: :json + ) end def folder_name - self.environment_type || self.name + environment_type || name end def name_without_type @@ -249,7 +250,7 @@ class Environment < ActiveRecord::Base def deployment_platform strong_memoize(:deployment_platform) do - project.deployment_platform(environment: self.name) + project.deployment_platform(environment: name) end end |