gitlab-org--gitlab-foss/app/models/deployment.rb

108 lines
2.7 KiB
Ruby
Raw Normal View History

2016-06-10 17:36:54 -04:00
class Deployment < ActiveRecord::Base
include InternalId
belongs_to :project, required: true, validate: true
belongs_to :environment, required: true, validate: true
2016-06-10 17:36:54 -04:00
belongs_to :user
belongs_to :deployable, polymorphic: true
2016-06-14 12:34:48 -04:00
validates :sha, presence: true
validates :ref, presence: true
2016-06-10 17:36:54 -04:00
delegate :name, to: :environment, prefix: true
after_create :create_ref
2016-06-10 17:36:54 -04:00
def commit
project.commit(sha)
end
def commit_title
commit.try(:title)
end
def short_sha
2016-06-14 12:34:48 -04:00
Commit.truncate_sha(sha)
2016-06-10 17:36:54 -04:00
end
2016-06-14 08:47:00 -04:00
def last?
self == environment.last_deployment
end
2016-09-30 12:40:56 -04:00
def create_ref
project.repository.create_ref(ref, ref_path)
end
2016-07-16 12:39:58 -04:00
def manual_actions
@manual_actions ||= deployable.try(:other_actions)
2016-07-16 12:39:58 -04:00
end
2016-08-02 08:01:22 -04:00
def includes_commit?(commit)
2016-08-02 08:01:22 -04:00
return false unless commit
# 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
2016-08-02 08:01:22 -04:00
end
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)
if previous_deployment
merge_requests = merge_requests.where("merge_request_metrics.merged_at >= ?", previous_deployment.created_at)
end
# 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)
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-09-30 09:45:27 -04:00
def stop_action
2016-10-17 10:13:19 -04:00
return nil unless on_stop.present?
return nil unless manual_actions
@stop_action ||= manual_actions.find_by(name: on_stop)
end
def stoppable?
2016-10-17 10:13:19 -04:00
stop_action.present?
end
2016-10-12 09:21:01 -04:00
def formatted_deployment_time
created_at.to_time.in_time_zone.to_s(:medium)
end
2016-09-30 09:45:27 -04:00
private
def ref_path
2016-10-19 08:49:09 -04:00
File.join(environment.ref_path, 'deployments', iid.to_s)
2016-09-30 09:45:27 -04:00
end
2016-06-10 17:36:54 -04:00
end