BigW Consortium Gitlab

merge_request_observer.rb 1.66 KB
Newer Older
1 2 3
class MergeRequestObserver < ActivityObserver
  observe :merge_request

4
  def after_create(merge_request)
5
    if merge_request.author_id
6 7 8
      create_event(merge_request, Event.determine_action(merge_request))
    end

9
    notification.new_merge_request(merge_request, current_user)
10
    merge_request.create_cross_references!(merge_request.project, current_user)
11
    execute_hooks(merge_request)
12 13
  end

14
  def after_close(merge_request, transition)
15
    create_event(merge_request, Event::CLOSED)
16
    notification.close_mr(merge_request, current_user)
17 18
    create_note(merge_request)
    execute_hooks(merge_request)
19 20
  end

21
  def after_reopen(merge_request, transition)
22
    create_event(merge_request, Event::REOPENED)
23
    create_note(merge_request)
24
    execute_hooks(merge_request)
25 26 27
  end

  def after_update(merge_request)
tsl0922 committed
28
    notification.reassigned_merge_request(merge_request, current_user) if merge_request.is_being_reassigned?
29 30

    merge_request.notice_added_references(merge_request.project, current_user)
31
    execute_hooks(merge_request)
32
  end
33 34 35

  def create_event(record, status)
    Event.create(
36 37 38 39
      project: record.target_project,
      target_id: record.id,
      target_type: record.class.name,
      action: status,
40
      author_id: current_user.id
41 42
    )
  end
43 44 45 46 47 48 49 50 51

  private

  # Create merge request note with service comment like 'Status changed to closed'
  def create_note(merge_request)
    Note.create_status_change_note(merge_request, merge_request.target_project, current_user, merge_request.state, nil)
  end

  def execute_hooks(merge_request)
52 53 54
    if merge_request.project
      merge_request.project.execute_hooks(merge_request.to_hook_data, :merge_request_hooks)
    end
55
  end
56
end