Class: MergeRequests::BaseService

Inherits:
IssuableBaseService show all
Extended by:
Gitlab::Utils::Override
Includes:
AssignsMergeParams
Defined in:
app/services/merge_requests/base_service.rb

Instance Attribute Summary

Attributes inherited from BaseProjectService

#project

Attributes inherited from BaseContainerService

#container, #current_user, #params

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from AssignsMergeParams

#assign_allowed_merge_params, included

Methods inherited from BaseProjectService

#initialize

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods inherited from BaseContainerService

#group_container?, #initialize, #namespace_container?, #project_container?

Methods included from BaseServiceUtility

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

Methods included from Gitlab::Allowable

#can?

Instance Method Details

#cancel_review_app_jobs!(merge_request) ⇒ Object


75
76
77
78
# File 'app/services/merge_requests/base_service.rb', line 75

def cancel_review_app_jobs!(merge_request)
  environments = merge_request.environments_in_head_pipeline.in_review_folder.available
  environments.each { |environment| environment.cancel_deployment_jobs! }
end

#cleanup_environments(merge_request) ⇒ Object


70
71
72
73
# File 'app/services/merge_requests/base_service.rb', line 70

def cleanup_environments(merge_request)
  Environments::StopService.new(merge_request.source_project, current_user)
                           .execute_for_merge_request_pipeline(merge_request)
end

#create_note(merge_request, state = merge_request.state) ⇒ Object


8
9
10
# File 'app/services/merge_requests/base_service.rb', line 8

def create_note(merge_request, state = merge_request.state)
  SystemNoteService.change_status(merge_request, merge_request.target_project, current_user, state, nil)
end

#execute_external_hooks(merge_request, merge_data) ⇒ Object


32
33
34
# File 'app/services/merge_requests/base_service.rb', line 32

def execute_external_hooks(merge_request, merge_data)
  # Implemented in EE
end

#execute_hooks(merge_request, action = 'open', old_rev: nil, old_associations: {}) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'app/services/merge_requests/base_service.rb', line 22

def execute_hooks(merge_request, action = 'open', old_rev: nil, old_associations: {})
  merge_data = hook_data(merge_request, action, old_rev: old_rev, old_associations: old_associations)
  merge_request.project.execute_hooks(merge_data, :merge_request_hooks)
  merge_request.project.execute_integrations(merge_data, :merge_request_hooks)

  execute_external_hooks(merge_request, merge_data)

  enqueue_jira_connect_messages_for(merge_request)
end

#handle_assignees_change(merge_request, old_assignees) ⇒ Object


45
46
47
48
49
50
51
# File 'app/services/merge_requests/base_service.rb', line 45

def handle_assignees_change(merge_request, old_assignees)
  bulk_update_assignees_state(merge_request, merge_request.assignees - old_assignees)

  MergeRequests::HandleAssigneesChangeService
    .new(project: project, current_user: current_user)
    .async_execute(merge_request, old_assignees)
end

#handle_changes(merge_request, options) ⇒ Object


36
37
38
39
40
41
42
43
# File 'app/services/merge_requests/base_service.rb', line 36

def handle_changes(merge_request, options)
  old_associations = options.fetch(:old_associations, {})
  old_assignees = old_associations.fetch(:assignees, [])
  old_reviewers = old_associations.fetch(:reviewers, [])

  handle_assignees_change(merge_request, old_assignees) if merge_request.assignees != old_assignees
  handle_reviewers_change(merge_request, old_reviewers) if merge_request.reviewers != old_reviewers
end

#handle_reviewers_change(merge_request, old_reviewers) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/merge_requests/base_service.rb', line 53

def handle_reviewers_change(merge_request, old_reviewers)
  affected_reviewers = (old_reviewers + merge_request.reviewers) - (old_reviewers & merge_request.reviewers)
  create_reviewer_note(merge_request, old_reviewers)
  notification_service.async.changed_reviewer_of_merge_request(merge_request, current_user, old_reviewers)
  todo_service.reassigned_reviewable(merge_request, current_user, old_reviewers)
  invalidate_cache_counts(merge_request, users: affected_reviewers.compact)

  new_reviewers = merge_request.reviewers - old_reviewers
  merge_request_activity_counter.track_users_review_requested(users: new_reviewers)
  merge_request_activity_counter.track_reviewers_changed_action(user: current_user)
  bulk_update_reviewers_state(merge_request, new_reviewers)

  unless new_reviewers.include?(current_user)
    remove_attention_requested(merge_request)
  end
end

#hook_data(merge_request, action, old_rev: nil, old_associations: {}) ⇒ Object


12
13
14
15
16
17
18
19
20
# File 'app/services/merge_requests/base_service.rb', line 12

def hook_data(merge_request, action, old_rev: nil, old_associations: {})
  hook_data = merge_request.to_hook_data(current_user, old_associations: old_associations)
  hook_data[:object_attributes][:action] = action
  if old_rev && !Gitlab::Git.blank_ref?(old_rev)
    hook_data[:object_attributes][:oldrev] = old_rev
  end

  hook_data
end

#inspectObject

Don't try to print expensive instance variables.


89
90
91
92
93
# File 'app/services/merge_requests/base_service.rb', line 89

def inspect
  return "#<#{self.class}>" unless respond_to?(:merge_request)

  "#<#{self.class} #{merge_request.to_reference(full: true)}>"
end

#merge_request_activity_counterObject


95
96
97
# File 'app/services/merge_requests/base_service.rb', line 95

def merge_request_activity_counter
  Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
end

#source_projectObject


80
81
82
# File 'app/services/merge_requests/base_service.rb', line 80

def source_project
  @source_project ||= merge_request.source_project
end

#target_projectObject


84
85
86
# File 'app/services/merge_requests/base_service.rb', line 84

def target_project
  @target_project ||= merge_request.target_project
end