BigW Consortium Gitlab

deployment.rb 3.06 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
    project.repository.ancestor?(commit.id, sha)
49
  end
50

51 52 53
  def update_merge_request_metrics!
    return unless environment.update_merge_request_metrics?

54 55 56 57
    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)
58

59 60
    if previous_deployment
      merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
61
    end
62 63 64 65 66 67 68 69 70 71

    # 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

72 73 74
    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)
75 76 77 78
  end

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

85
  def stop_action
86 87
    return unless on_stop.present?
    return unless manual_actions
88

89
    @stop_action ||= manual_actions.find_by(name: on_stop)
90 91
  end

Kamil Trzcinski committed
92
  def stop_action?
93
    stop_action.present?
94 95
  end

96 97 98 99
  def formatted_deployment_time
    created_at.to_time.in_time_zone.to_s(:medium)
  end

100 101 102 103
  def has_metrics?
    project.monitoring_service.present?
  end

104
  def metrics
105 106
    return {} unless has_metrics?

107
    project.monitoring_service.deployment_metrics(self)
108 109
  end

110 111 112 113
  def has_additional_metrics?
    project.prometheus_service.present?
  end

114
  def additional_metrics
115
    return {} unless project.prometheus_service.present?
116

117
    metrics = project.prometheus_service.additional_deployment_metrics(self)
118 119 120
    metrics&.merge(deployment_time: created_at.to_i) || {}
  end

121 122 123
  private

  def ref_path
124
    File.join(environment.ref_path, 'deployments', iid.to_s)
125
  end
126
end