2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-26 00:43:47 -04:00
|
|
|
class Projects::DiscussionsController < Projects::ApplicationController
|
2018-02-27 19:10:43 -05:00
|
|
|
include NotesHelper
|
|
|
|
include RendersNotes
|
|
|
|
|
2017-06-20 07:13:04 -04:00
|
|
|
before_action :check_merge_requests_available!
|
2016-07-26 00:43:47 -04:00
|
|
|
before_action :merge_request
|
2018-04-05 10:23:57 -04:00
|
|
|
before_action :discussion, only: [:resolve, :unresolve]
|
|
|
|
before_action :authorize_resolve_discussion!, only: [:resolve, :unresolve]
|
2016-07-26 00:43:47 -04:00
|
|
|
|
|
|
|
def resolve
|
2016-10-26 17:21:50 -04:00
|
|
|
Discussions::ResolveService.new(project, current_user, merge_request: merge_request).execute(discussion)
|
2016-07-28 22:39:35 -04:00
|
|
|
|
2018-02-27 19:10:43 -05:00
|
|
|
render_discussion
|
2016-07-26 00:43:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def unresolve
|
|
|
|
discussion.unresolve!
|
|
|
|
|
2018-02-27 19:10:43 -05:00
|
|
|
render_discussion
|
|
|
|
end
|
|
|
|
|
2018-03-13 08:01:36 -04:00
|
|
|
def show
|
|
|
|
render json: {
|
2018-06-21 08:22:40 -04:00
|
|
|
truncated_diff_lines: discussion.try(:truncated_diff_lines)
|
2018-03-13 08:01:36 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-02-27 19:10:43 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def render_discussion
|
|
|
|
if serialize_notes?
|
|
|
|
prepare_notes_for_rendering(discussion.notes, merge_request)
|
|
|
|
render_json_with_discussions_serializer
|
|
|
|
else
|
|
|
|
render_json_with_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_json_with_discussions_serializer
|
|
|
|
render json:
|
2019-01-16 07:09:29 -05:00
|
|
|
DiscussionSerializer.new(project: project, noteable: discussion.noteable, current_user: current_user, note_entity: ProjectNoteEntity)
|
2018-06-21 08:22:40 -04:00
|
|
|
.represent(discussion, context: self, render_truncated_diff_lines: true)
|
2018-02-27 19:10:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Legacy method used to render discussions notes when not using Vue on views.
|
|
|
|
def render_json_with_html
|
2016-07-27 13:34:04 -04:00
|
|
|
render json: {
|
2018-02-27 19:10:43 -05:00
|
|
|
resolved_by: discussion.resolved_by.try(:name),
|
2016-07-28 22:09:36 -04:00
|
|
|
discussion_headline_html: view_to_html_string('discussions/_headline', discussion: discussion)
|
2016-07-27 13:34:04 -04:00
|
|
|
}
|
2016-07-26 00:43:47 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-07-26 00:43:47 -04:00
|
|
|
def merge_request
|
2016-11-29 08:47:43 -05:00
|
|
|
@merge_request ||= MergeRequestsFinder.new(current_user, project_id: @project.id).find_by!(iid: params[:merge_request_id])
|
2016-07-26 00:43:47 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-07-26 00:43:47 -04:00
|
|
|
|
|
|
|
def discussion
|
2017-03-09 20:29:11 -05:00
|
|
|
@discussion ||= @merge_request.find_discussion(params[:id]) || render_404
|
2016-07-26 00:43:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_resolve_discussion!
|
|
|
|
access_denied! unless discussion.can_resolve?(current_user)
|
|
|
|
end
|
|
|
|
end
|