Class: Projects::MergeRequests::DraftsController

Inherits:
ApplicationController
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/controllers/projects/merge_requests/drafts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/projects/merge_requests/drafts_controller.rb', line 24

def create
  create_params = draft_note_params.merge(in_reply_to_discussion_id: params[:in_reply_to_discussion_id])
  create_service = DraftNotes::CreateService.new(merge_request, current_user, create_params)

  draft_note = create_service.execute

  if draft_note.errors.present?
    render json: { errors: draft_note.errors.full_messages.to_sentence }, status: :unprocessable_entity
    return
  end

  prepare_notes_for_rendering(draft_note)

  render json: DraftNoteSerializer.new(current_user: current_user).represent(draft_note)
end

#destroyObject



49
50
51
52
53
# File 'app/controllers/projects/merge_requests/drafts_controller.rb', line 49

def destroy
  DraftNotes::DestroyService.new(merge_request, current_user).execute(draft_note)

  head :ok
end

#discardObject



86
87
88
89
90
# File 'app/controllers/projects/merge_requests/drafts_controller.rb', line 86

def discard
  DraftNotes::DestroyService.new(merge_request, current_user).execute

  head :ok
end

#indexObject



19
20
21
22
# File 'app/controllers/projects/merge_requests/drafts_controller.rb', line 19

def index
  drafts = prepare_notes_for_rendering(draft_notes)
  render json: DraftNoteSerializer.new(current_user: current_user).represent(drafts)
end

#publishObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/projects/merge_requests/drafts_controller.rb', line 55

def publish
  result = DraftNotes::PublishService.new(merge_request, current_user).execute(draft_note(allow_nil: true))

  if create_note_params[:note]
    ::Notes::CreateService.new(@project, current_user, create_note_params).execute

    merge_request_activity_counter.track_submit_review_comment(user: current_user)
  end

  if Gitlab::Utils.to_boolean(approve_params[:approve])
    unless merge_request.approved_by?(current_user)
      success = ::MergeRequests::ApprovalService
        .new(project: @project, current_user: current_user, params: approve_params)
        .execute(merge_request)

      unless success
        return render json: { message: _('An error occurred while approving, please try again.') },
          status: :internal_server_error
      end
    end

    merge_request_activity_counter.track_submit_review_approve(user: current_user)
  end

  if result[:status] == :success
    head :ok
  else
    render json: { message: result[:message] }, status: :internal_server_error
  end
end

#updateObject



40
41
42
43
44
45
46
47
# File 'app/controllers/projects/merge_requests/drafts_controller.rb', line 40

def update
  if draft_note.update(draft_note_params)
    prepare_notes_for_rendering(draft_note)
    render json: DraftNoteSerializer.new(current_user: current_user).represent(draft_note)
  else
    render json: { errors: draft_note.errors.full_messages.to_sentence }, status: :unprocessable_entity
  end
end