BigW Consortium Gitlab

deployment.rb 2.14 KB
Newer Older
1 2 3
class Deployment < ActiveRecord::Base
  include InternalId

4 5
  belongs_to :project, required: true, validate: true
  belongs_to :environment, required: true, validate: true
6 7 8
  belongs_to :user
  belongs_to :deployable, polymorphic: true

9 10
  validates :sha, presence: true
  validates :ref, presence: true
11 12 13

  delegate :name, to: :environment, prefix: true

14
  after_save :create_ref
15

16 17 18 19 20 21 22 23 24
  def commit
    project.commit(sha)
  end

  def commit_title
    commit.try(:title)
  end

  def short_sha
25
    Commit.truncate_sha(sha)
26
  end
27 28 29 30

  def last?
    self == environment.last_deployment
  end
31

32
  def create_ref
33
    project.repository.create_ref(ref, ref_path)
34
  end
35

36
  def manual_actions
37
    deployable.try(:other_actions)
38
  end
39

40
  def includes_commit?(commit)
41 42
    return false unless commit

Z.J. van de Weg committed
43
    project.repository.is_ancestor?(commit.id, sha)
44
  end
45

46 47 48 49 50 51 52
  def update_merge_request_metrics!
    return unless environment.update_merge_request_metrics?

    merge_requests = project.merge_requests.
                     joins(:metrics).
                     where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil }).
                     where("merge_request_metrics.merged_at <= ?", self.created_at)
53

54 55
    if previous_deployment
      merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
56
    end
57 58 59 60 61 62 63 64 65 66 67 68 69

    # Need to use `map` instead of `select` because MySQL doesn't allow `SELECT`ing from the same table
    # that we're updating.
    merge_request_ids =
      if Gitlab::Database.postgresql?
        merge_requests.select(:id)
      elsif Gitlab::Database.mysql?
        merge_requests.map(&:id)
      end

    MergeRequest::Metrics.
      where(merge_request_id: merge_request_ids, first_deployed_to_production_at: nil).
      update_all(first_deployed_to_production_at: self.created_at)
70 71 72 73 74 75 76 77 78
  end

  def previous_deployment
    @previous_deployment ||=
      project.deployments.joins(:environment).
      where(environments: { name: self.environment.name }, ref: self.ref).
      where.not(id: self.id).
      take
  end
79 80 81 82

  private

  def ref_path
83
    File.join(environment.ref_path, 'deployments', id.to_s)
84
  end
85
end