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

4
5
  belongs_to :project, required: true
  belongs_to :environment, required: true
6
  belongs_to :user
7
  belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
8

Kamil Trzcinski's avatar
Kamil Trzcinski committed
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
Kamil Trzcinski's avatar
Kamil Trzcinski committed
26
    Commit.truncate_sha(sha)
27
  end
28
29
30
31

  def last?
    self == environment.last_deployment
  end
32

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
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's avatar
Kamil Trzcinski committed
92
  def stop_action?
Kamil Trzcinski's avatar
Kamil Trzcinski committed
93
    stop_action.present?
94
95
  end

Z.J. van de Weg's avatar
Z.J. van de Weg committed
96
97
98
99
  def formatted_deployment_time
    created_at.to_time.in_time_zone.to_s(:medium)
  end

Fatih Acet's avatar
Fatih Acet committed
100
  def has_metrics?
101
    prometheus_adapter&.can_query?
Fatih Acet's avatar
Fatih Acet committed
102
103
  end

104
  def metrics
Fatih Acet's avatar
Fatih Acet committed
105
106
    return {} unless has_metrics?

107
108
    metrics = prometheus_adapter.query(:deployment, self)
    metrics&.merge(deployment_time: created_at.to_i) || {}
109
110
  end

111
  def additional_metrics
112
    return {} unless has_metrics?
113

114
    metrics = prometheus_adapter.query(:additional_metrics_deployment, self)
115
116
117
    metrics&.merge(deployment_time: created_at.to_i) || {}
  end

118
119
  private

120
121
122
123
  def prometheus_adapter
    environment.prometheus_adapter
  end

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