2016-06-10 21:36:54 +00:00
|
|
|
class Deployment < ActiveRecord::Base
|
|
|
|
include InternalId
|
|
|
|
|
2016-06-15 13:09:50 +00:00
|
|
|
belongs_to :project, required: true, validate: true
|
|
|
|
belongs_to :environment, required: true, validate: true
|
2016-06-10 21:36:54 +00:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :deployable, polymorphic: true
|
|
|
|
|
2016-06-14 16:34:48 +00:00
|
|
|
validates :sha, presence: true
|
|
|
|
validates :ref, presence: true
|
2016-06-10 21:36:54 +00:00
|
|
|
|
|
|
|
delegate :name, to: :environment, prefix: true
|
|
|
|
|
2016-06-20 17:22:08 +00:00
|
|
|
after_save :keep_around_commit
|
|
|
|
|
2016-06-10 21:36:54 +00:00
|
|
|
def commit
|
|
|
|
project.commit(sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_title
|
|
|
|
commit.try(:title)
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_sha
|
2016-06-14 16:34:48 +00:00
|
|
|
Commit.truncate_sha(sha)
|
2016-06-10 21:36:54 +00:00
|
|
|
end
|
2016-06-14 12:47:00 +00:00
|
|
|
|
|
|
|
def last?
|
|
|
|
self == environment.last_deployment
|
|
|
|
end
|
2016-06-20 17:22:08 +00:00
|
|
|
|
|
|
|
def keep_around_commit
|
|
|
|
project.repository.keep_around(self.sha)
|
|
|
|
end
|
2016-07-16 16:39:58 +00:00
|
|
|
|
2016-07-16 21:06:34 +00:00
|
|
|
def manual_actions
|
2016-07-16 23:48:51 +00:00
|
|
|
deployable.try(:other_actions)
|
2016-07-16 16:39:58 +00:00
|
|
|
end
|
2016-08-02 12:01:22 +00:00
|
|
|
|
2016-08-09 13:11:14 +00:00
|
|
|
def includes_commit?(commit)
|
2016-08-02 12:01:22 +00:00
|
|
|
return false unless commit
|
|
|
|
|
2016-08-03 11:37:39 +00:00
|
|
|
project.repository.is_ancestor?(commit.id, sha)
|
2016-08-02 12:01:22 +00:00
|
|
|
end
|
2016-09-20 09:36:54 +00:00
|
|
|
|
2016-09-20 19:17:37 +00:00
|
|
|
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)
|
2016-09-20 09:36:54 +00:00
|
|
|
|
2016-09-20 19:17:37 +00:00
|
|
|
if previous_deployment
|
|
|
|
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
|
2016-09-20 09:36:54 +00:00
|
|
|
end
|
2016-09-20 19:17:37 +00:00
|
|
|
|
|
|
|
# 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)
|
2016-09-20 09:36:54 +00:00
|
|
|
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
|
2016-06-10 21:36:54 +00:00
|
|
|
end
|