2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module MergeRequests
|
|
|
|
class CleanupRefsService
|
|
|
|
include BaseServiceUtility
|
|
|
|
|
|
|
|
TIME_THRESHOLD = 14.days
|
|
|
|
|
|
|
|
attr_reader :merge_request
|
|
|
|
|
|
|
|
def self.schedule(merge_request)
|
2021-01-29 00:20:46 +05:30
|
|
|
merge_request.create_cleanup_schedule(scheduled_at: TIME_THRESHOLD.from_now)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(merge_request)
|
|
|
|
@merge_request = merge_request
|
|
|
|
@repository = merge_request.project.repository
|
|
|
|
@ref_path = merge_request.ref_path
|
|
|
|
@merge_ref_path = merge_request.merge_ref_path
|
2021-01-03 14:25:43 +05:30
|
|
|
@ref_head_sha = @repository.commit(merge_request.ref_path)&.id
|
2020-11-24 15:15:51 +05:30
|
|
|
@merge_ref_sha = merge_request.merge_ref_head&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-01-29 00:20:46 +05:30
|
|
|
return error("Merge request is not scheduled to be cleaned up yet.") unless scheduled?
|
2020-11-24 15:15:51 +05:30
|
|
|
return error("Merge request has not been closed nor merged for #{TIME_THRESHOLD.inspect}.") unless eligible?
|
|
|
|
|
|
|
|
# Ensure that commit shas of refs are kept around so we won't lose them when GC runs.
|
|
|
|
keep_around
|
|
|
|
|
|
|
|
return error('Failed to create keep around refs.') unless kept_around?
|
|
|
|
return error('Failed to cache merge ref sha.') unless cache_merge_ref_sha
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
delete_refs if repository.exists?
|
|
|
|
|
|
|
|
return error('Failed to update schedule.') unless update_schedule
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
success
|
2021-03-08 18:12:59 +05:30
|
|
|
rescue Gitlab::Git::Repository::GitError, Gitlab::Git::CommandError => e
|
|
|
|
error(e.message)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :repository, :ref_path, :merge_ref_path, :ref_head_sha, :merge_ref_sha
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def scheduled?
|
|
|
|
merge_request.cleanup_schedule.present? && merge_request.cleanup_schedule.scheduled_at <= Time.current
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def eligible?
|
|
|
|
return met_time_threshold?(merge_request.metrics&.latest_closed_at) if merge_request.closed?
|
|
|
|
|
|
|
|
merge_request.merged? && met_time_threshold?(merge_request.metrics&.merged_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
def met_time_threshold?(attr)
|
|
|
|
attr.nil? || attr.to_i <= TIME_THRESHOLD.ago.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def kept_around?
|
|
|
|
service = Gitlab::Git::KeepAround.new(repository)
|
|
|
|
|
|
|
|
[ref_head_sha, merge_ref_sha].compact.all? do |sha|
|
|
|
|
service.kept_around?(sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def keep_around
|
|
|
|
repository.keep_around(ref_head_sha, merge_ref_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_merge_ref_sha
|
|
|
|
return true if merge_ref_sha.nil?
|
|
|
|
|
|
|
|
# Caching the merge ref sha is needed before we delete the merge ref so
|
|
|
|
# we can still show the merge ref diff (via `MergeRequest#merge_ref_head`)
|
|
|
|
merge_request.update_column(:merge_ref_sha, merge_ref_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_refs
|
|
|
|
repository.delete_refs(ref_path, merge_ref_path)
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
def update_schedule
|
|
|
|
merge_request.cleanup_schedule.update(completed_at: Time.current)
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|