BigW Consortium Gitlab

deployment.rb 3.33 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
  belongs_to :user
7
  belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
8

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

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

14
  after_create :create_ref
15
  after_create :invalidate_cache
16

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

  def commit_title
    commit.try(:title)
  end

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

  def last?
    self == environment.last_deployment
  end
32

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

37 38 39 40
  def invalidate_cache
    environment.expire_etag_cache
  end

41
  def manual_actions
42
    @manual_actions ||= deployable.try(:other_actions)
43
  end
44

45
  def includes_commit?(commit)
46 47
    return false unless commit

48 49 50 51 52 53 54 55
    # Before 8.10, deployments didn't have keep-around refs. Any deployment
    # created before then could have a `sha` referring to a commit that no
    # longer exists in the repository, so just ignore those.
    begin
      project.repository.is_ancestor?(commit.id, sha)
    rescue Rugged::OdbError
      false
    end
56
  end
57

58 59 60
  def update_merge_request_metrics!
    return unless environment.update_merge_request_metrics?

61 62 63 64
    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)
65

66 67
    if previous_deployment
      merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
68
    end
69 70 71 72 73 74 75 76 77 78

    # 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

79 80 81
    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)
82 83 84 85
  end

  def previous_deployment
    @previous_deployment ||=
86 87 88 89
      project.deployments.joins(:environment)
      .where(environments: { name: self.environment.name }, ref: self.ref)
      .where.not(id: self.id)
      .take
90
  end
91

92
  def stop_action
93 94
    return unless on_stop.present?
    return unless manual_actions
95

96
    @stop_action ||= manual_actions.find_by(name: on_stop)
97 98
  end

Kamil Trzcinski committed
99
  def stop_action?
100
    stop_action.present?
101 102
  end

103 104 105 106
  def formatted_deployment_time
    created_at.to_time.in_time_zone.to_s(:medium)
  end

107 108 109 110
  def has_metrics?
    project.monitoring_service.present?
  end

111
  def metrics
112 113
    return {} unless has_metrics?

114
    project.monitoring_service.deployment_metrics(self)
115 116
  end

117 118 119 120
  def has_additional_metrics?
    project.prometheus_service.present?
  end

121
  def additional_metrics
122
    return {} unless project.prometheus_service.present?
123

124
    metrics = project.prometheus_service.additional_deployment_metrics(self)
125 126 127
    metrics&.merge(deployment_time: created_at.to_i) || {}
  end

128 129 130
  private

  def ref_path
131
    File.join(environment.ref_path, 'deployments', iid.to_s)
132
  end
133
end