BigW Consortium Gitlab

environment.rb 1.21 KB
Newer Older
1
class Environment < ActiveRecord::Base
2
  belongs_to :project, required: true, validate: true
3 4 5

  has_many :deployments

Z.J. van de Weg committed
6
  before_validation :nullify_external_url
7
  before_save :set_environment_type
Z.J. van de Weg committed
8

9 10
  validates :name,
            presence: true,
11
            uniqueness: { scope: :project_id },
12 13 14
            length: { within: 0..255 },
            format: { with: Gitlab::Regex.environment_name_regex,
                      message: Gitlab::Regex.environment_name_regex_message }
15

16 17
  validates :external_url,
            uniqueness: { scope: :project_id },
Z.J. van de Weg committed
18 19 20
            length: { maximum: 255 },
            allow_nil: true,
            addressable_url: true
21

22 23 24
  def last_deployment
    deployments.last
  end
Z.J. van de Weg committed
25 26 27 28

  def nullify_external_url
    self.external_url = nil if self.external_url.blank?
  end
29

30 31 32 33 34 35 36 37 38 39 40
  def set_environment_type
    names = name.split('/')

    self.environment_type =
      if names.many?
        names.first
      else
        nil
      end
  end

41
  def includes_commit?(commit)
Z.J. van de Weg committed
42
    return false unless last_deployment
43

44
    last_deployment.includes_commit?(commit)
45
  end
46 47 48 49

  def update_merge_request_metrics?
    self.name == "production"
  end
50 51

  def ref_path
52
    "refs/environments/#{Shellwords.shellescape(name)}"
53
  end
54
end