Class: MergeRequests::CleanupRefsService

Inherits:
Object
  • Object
show all
Includes:
BaseServiceUtility
Defined in:
app/services/merge_requests/cleanup_refs_service.rb

Constant Summary collapse

TIME_THRESHOLD =
14.days

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(merge_request) ⇒ CleanupRefsService

Returns a new instance of CleanupRefsService.


15
16
17
18
19
20
21
22
# File 'app/services/merge_requests/cleanup_refs_service.rb', line 15

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
  @ref_head_sha = @repository.commit(merge_request.ref_path)&.id
  @merge_ref_sha = merge_request.merge_ref_head&.id
end

Instance Attribute Details

#merge_requestObject (readonly)

Returns the value of attribute merge_request.


9
10
11
# File 'app/services/merge_requests/cleanup_refs_service.rb', line 9

def merge_request
  @merge_request
end

Class Method Details

.schedule(merge_request) ⇒ Object


11
12
13
# File 'app/services/merge_requests/cleanup_refs_service.rb', line 11

def self.schedule(merge_request)
  merge_request.create_cleanup_schedule(scheduled_at: TIME_THRESHOLD.from_now)
end

Instance Method Details

#executeObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/merge_requests/cleanup_refs_service.rb', line 24

def execute
  return error("Merge request is not scheduled to be cleaned up yet.") unless scheduled?
  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

  delete_refs if repository.exists?

  return error('Failed to update schedule.') unless update_schedule

  success
rescue Gitlab::Git::Repository::GitError, Gitlab::Git::CommandError => e
  error(e.message)
end