diff options
Diffstat (limited to 'app/services/merge_requests')
-rw-r--r-- | app/services/merge_requests/cleanup_refs_service.rb | 60 | ||||
-rw-r--r-- | app/services/merge_requests/close_service.rb | 3 | ||||
-rw-r--r-- | app/services/merge_requests/post_merge_service.rb | 3 |
3 files changed, 66 insertions, 0 deletions
diff --git a/app/services/merge_requests/cleanup_refs_service.rb b/app/services/merge_requests/cleanup_refs_service.rb new file mode 100644 index 00000000000..e34d568cdcd --- /dev/null +++ b/app/services/merge_requests/cleanup_refs_service.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module MergeRequests + class CleanupRefsService + include BaseServiceUtility + + TIME_THRESHOLD = 14.days + + attr_reader :merge_request + + def self.schedule(merge_request) + MergeRequestCleanupRefsWorker.perform_in(TIME_THRESHOLD, merge_request.id) + end + + def initialize(merge_request) + @merge_request = merge_request + @repository = merge_request.project.repository + @ref_path = merge_request.ref_path + @ref_head_sha = @repository.commit(merge_request.ref_path).id + end + + def execute + 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? + + delete_refs + success + end + + private + + attr_reader :repository, :ref_path, :ref_head_sha + + 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? + Gitlab::Git::KeepAround.new(repository).kept_around?(ref_head_sha) + end + + def keep_around + repository.keep_around(ref_head_sha) + end + + def delete_refs + repository.delete_refs(ref_path) + end + end +end diff --git a/app/services/merge_requests/close_service.rb b/app/services/merge_requests/close_service.rb index c2174d2a130..b0a7face594 100644 --- a/app/services/merge_requests/close_service.rb +++ b/app/services/merge_requests/close_service.rb @@ -2,6 +2,8 @@ module MergeRequests class CloseService < MergeRequests::BaseService + include RemovesRefs + def execute(merge_request, commit = nil) return merge_request unless can?(current_user, :update_merge_request, merge_request) @@ -19,6 +21,7 @@ module MergeRequests merge_request.update_project_counter_caches cleanup_environments(merge_request) abort_auto_merge(merge_request, 'merge request was closed') + cleanup_refs(merge_request) end merge_request diff --git a/app/services/merge_requests/post_merge_service.rb b/app/services/merge_requests/post_merge_service.rb index fdf8f442297..1c78fca3c26 100644 --- a/app/services/merge_requests/post_merge_service.rb +++ b/app/services/merge_requests/post_merge_service.rb @@ -7,6 +7,8 @@ module MergeRequests # and execute all hooks and notifications # class PostMergeService < MergeRequests::BaseService + include RemovesRefs + def execute(merge_request) merge_request.mark_as_merged close_issues(merge_request) @@ -20,6 +22,7 @@ module MergeRequests delete_non_latest_diffs(merge_request) cancel_review_app_jobs!(merge_request) cleanup_environments(merge_request) + cleanup_refs(merge_request) end private |