Class: MergeRequests::BaseService

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

Instance Attribute Summary

Attributes inherited from BaseContainerService

#container, #current_user, #group, #params, #project

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

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

Methods included from ErrorLogger

#log_error

Methods included from AssignsMergeParams

#assign_allowed_merge_params, included

Methods inherited from BaseContainerService

#group_container?, #namespace_container?, #project_container?, #project_group, #root_ancestor

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?, #can_all?, #can_any?

Constructor Details

#initialize(project:, current_user: nil, params: {}) ⇒ BaseService

Returns a new instance of BaseService.



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

def initialize(project:, current_user: nil, params: {})
  super(container: project, current_user: current_user, params: params)
end

Instance Method Details

#cancel_review_app_jobs!(merge_request) ⇒ Object



99
100
101
102
# File 'app/services/merge_requests/base_service.rb', line 99

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



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

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



15
16
17
# File 'app/services/merge_requests/base_service.rb', line 15

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

#deactivate_pages_deployments(merge_request) ⇒ Object



123
124
125
# File 'app/services/merge_requests/base_service.rb', line 123

def deactivate_pages_deployments(merge_request)
  Pages::DeactivateMrDeploymentsWorker.perform_async(merge_request.id)
end

#execute_external_hooks(merge_request, merge_data) ⇒ Object



44
45
46
# File 'app/services/merge_requests/base_service.rb', line 44

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

#execute_group_mention_hooks(merge_request, merge_data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/merge_requests/base_service.rb', line 48

def execute_group_mention_hooks(merge_request, merge_data)
  return unless merge_request.instance_of?(MergeRequest)

  args = {
    mentionable_type: 'MergeRequest',
    mentionable_id: merge_request.id,
    hook_data: merge_data,
    is_confidential: false
  }

  merge_request.run_after_commit_or_now do
    Integrations::GroupMentionWorker.perform_async(args)
  end
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/merge_requests/base_service.rb', line 29

def execute_hooks(merge_request, action = 'open', old_rev: nil, old_associations: {})
  # NOTE: Due to the async merge request diffs generation, we need to skip this for CreateService and execute it in
  #   AfterCreateService instead so that the webhook consumers receive the update when diffs are ready.
  return if merge_request.skip_ensure_merge_request_diff

  merge_data = Gitlab::Lazy.new { 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)
  execute_group_mention_hooks(merge_request, merge_data) if action == 'open'

  enqueue_jira_connect_messages_for(merge_request)
end

#handle_assignees_change(merge_request, old_assignees) ⇒ Object



72
73
74
75
76
# File 'app/services/merge_requests/base_service.rb', line 72

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

#handle_changes(merge_request, options) ⇒ Object



63
64
65
66
67
68
69
70
# File 'app/services/merge_requests/base_service.rb', line 63

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/services/merge_requests/base_service.rb', line 78

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)
  invalidate_cache_counts(merge_request, users: merge_request.assignees)

  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)
  trigger_merge_request_reviewers_updated(merge_request)

  set_first_reviewer_assigned_at_metrics(merge_request) if new_reviewers.any?
end

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



19
20
21
22
23
24
25
26
27
# File 'app/services/merge_requests/base_service.rb', line 19

def hook_data(merge_request, action, old_rev: nil, old_associations: {})
  hook_data = merge_request.to_hook_data(current_user, old_associations: old_associations, 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.



113
114
115
116
117
# File 'app/services/merge_requests/base_service.rb', line 113

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

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

#merge_request_activity_counterObject



119
120
121
# File 'app/services/merge_requests/base_service.rb', line 119

def merge_request_activity_counter
  Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter
end

#source_projectObject



104
105
106
# File 'app/services/merge_requests/base_service.rb', line 104

def source_project
  @source_project ||= merge_request.source_project
end

#target_projectObject



108
109
110
# File 'app/services/merge_requests/base_service.rb', line 108

def target_project
  @target_project ||= merge_request.target_project
end