issues_controller.rb 6.83 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
Douwe Maan's avatar
Douwe Maan committed
2
  include RendersNotes
3
  include ToggleSubscriptionAction
4
  include IssuableActions
5
  include ToggleAwardEmoji
6
  include IssuableCollections
7
  include SpammableActions
8

9 10
  prepend_before_action :authenticate_user!, only: [:new]

11
  before_action :check_issues_available!
12
  before_action :issue, except: [:index, :new, :create, :bulk_update]
13
  before_action :set_issuables_index, only: [:index]
14 15

  # Allow write(create) issue
16
  before_action :authorize_create_issue!, only: [:new, :create]
17 18

  # Allow modify issue
19
  before_action :authorize_update_issuable!, only: [:edit, :update, :move]
20

21 22 23
  # Allow create a new branch and empty WIP merge request from current issue
  before_action :authorize_create_merge_request!, only: [:create_merge_request]

24
  respond_to :html
gitlabhq's avatar
gitlabhq committed
25 26

  def index
27
    @issues = @issuables
28

gitlabhq's avatar
gitlabhq committed
29
    respond_to do |format|
30
      format.html
31
      format.atom { render layout: 'xml.atom' }
32 33
      format.json do
        render json: {
34
          html: view_to_html_string("projects/issues/_issues"),
35
          labels: @labels.as_json(methods: :text_color)
36 37
        }
      end
gitlabhq's avatar
gitlabhq committed
38 39 40 41
    end
  end

  def new
42
    params[:issue] ||= ActionController::Parameters.new(
43
      assignee_ids: ""
44
    )
45
    build_params = issue_params.merge(
Bob Van Landuyt's avatar
Bob Van Landuyt committed
46
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
47
      discussion_to_resolve: params[:discussion_to_resolve]
48
    )
49
    service = Issues::BuildService.new(project, current_user, build_params)
50

51
    @issue = @noteable = service.execute
Bob Van Landuyt's avatar
Bob Van Landuyt committed
52
    @merge_request_to_resolve_discussions_of = service.merge_request_to_resolve_discussions_of
53
    @discussion_to_resolve = service.discussions_to_resolve.first if params[:discussion_to_resolve]
54

gitlabhq's avatar
gitlabhq committed
55 56 57
    respond_with(@issue)
  end

58 59 60 61
  def edit
    respond_with(@issue)
  end

62
  def discussions
63 64 65 66 67
    notes = @issue.notes
      .inc_relations_for_view
      .includes(:noteable)
      .fresh

68 69
    notes = prepare_notes_for_rendering(notes)
    notes = notes.reject { |n| n.cross_reference_not_visible_for?(current_user) }
70

71
    discussions = Discussion.build_collection(notes, @issue)
72

73
    render json: DiscussionSerializer.new(project: @project, noteable: @issue, current_user: current_user).represent(discussions)
74 75
  end

gitlabhq's avatar
gitlabhq committed
76
  def create
77
    create_params = issue_params.merge(spammable_params).merge(
Bob Van Landuyt's avatar
Bob Van Landuyt committed
78
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
79 80 81
      discussion_to_resolve: params[:discussion_to_resolve]
    )

82 83 84
    service = Issues::CreateService.new(project, current_user, create_params)
    @issue = service.execute

85
    if service.discussions_to_resolve.count(&:resolved?) > 0
86
      flash[:notice] = if service.discussion_to_resolve_id
87 88 89 90
                         "Resolved 1 discussion."
                       else
                         "Resolved all discussions."
                       end
91
    end
gitlabhq's avatar
gitlabhq committed
92

93
    respond_to do |format|
94
      format.html do
95
        recaptcha_check_with_fallback { render :new }
96
      end
97
      format.js do
98 99
        @link = @issue.attachment.url.to_js
      end
100
    end
gitlabhq's avatar
gitlabhq committed
101 102
  end

103 104 105
  def move
    params.require(:move_to_project_id)

106 107
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
108 109
      return render_404 unless issue.can_move?(current_user, new_project)

110
      @issue = Issues::UpdateService.new(project, current_user, target_project: new_project).execute(issue)
111
    end
gitlabhq's avatar
gitlabhq committed
112 113

    respond_to do |format|
114
      format.json do
115
        render_issue_json
116
      end
gitlabhq's avatar
gitlabhq committed
117
    end
118 119

  rescue ActiveRecord::StaleObjectError
120
    render_conflict_response
gitlabhq's avatar
gitlabhq committed
121 122
  end

123 124 125 126 127 128 129 130 131 132 133 134 135 136
  def referenced_merge_requests
    @merge_requests = @issue.referenced_merge_requests(current_user)
    @closed_by_merge_requests = @issue.closed_by_merge_requests(current_user)

    respond_to do |format|
      format.json do
        render json: {
          html: view_to_html_string('projects/issues/_merge_requests')
        }
      end
    end
  end

  def related_branches
137
    @related_branches = @issue.related_branches(current_user)
138 139 140 141 142 143 144 145 146 147

    respond_to do |format|
      format.json do
        render json: {
          html: view_to_html_string('projects/issues/_related_branches')
        }
      end
    end
  end

148 149 150 151 152 153 154
  def can_create_branch
    can_create = current_user &&
      can?(current_user, :push_code, @project) &&
      @issue.can_be_worked_on?(current_user)

    respond_to do |format|
      format.json do
155
        render json: { can_create_branch: can_create, has_related_branch: @issue.has_related_branch? }
156 157 158 159
      end
    end
  end

160
  def create_merge_request
161
    result = ::MergeRequests::CreateFromIssueService.new(project, current_user, issue_iid: issue.iid).execute
162 163 164 165 166 167 168 169

    if result[:status] == :success
      render json: MergeRequestCreateSerializer.new.represent(result[:merge_request])
    else
      render json: result[:messsage], status: :unprocessable_entity
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
170
  protected
gitlabhq's avatar
gitlabhq committed
171 172

  def issue
173
    return @issue if defined?(@issue)
174

175
    # The Sortable default scope causes performance issues when used with find_by
176 177
    @issuable = @noteable = @issue ||= @project.issues.where(iid: params[:id]).reorder(nil).take!
    @note = @project.notes.new(noteable: @issuable)
178 179 180 181

    return render_404 unless can?(current_user, :read_issue, @issue)

    @issue
gitlabhq's avatar
gitlabhq committed
182
  end
183
  alias_method :subscribable_resource, :issue
184
  alias_method :issuable, :issue
185
  alias_method :awardable, :issue
186
  alias_method :spammable, :issue
187

188 189 190 191
  def spammable_path
    project_issue_path(@project, @issue)
  end

192
  def authorize_create_merge_request!
193
    render_404 unless can?(current_user, :push_code, @project) && @issue.can_be_worked_on?(current_user)
194 195
  end

196
  def check_issues_available!
197
    return render_404 unless @project.feature_available?(:issues, current_user)
198
  end
199

200 201 202 203 204 205 206 207
  def render_issue_json
    if @issue.valid?
      render json: serializer.represent(@issue)
    else
      render json: { errors: @issue.errors.full_messages }, status: :unprocessable_entity
    end
  end

208
  def issue_params
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    params.require(:issue).permit(*issue_params_attributes)
  end

  def issue_params_attributes
    %i[
      title
      assignee_id
      position
      description
      confidential
      milestone_id
      due_date
      state_event
      task_num
      lock_version
224
      discussion_locked
225
    ] + [{ label_ids: [], assignee_ids: [] }]
226
  end
227 228 229 230 231 232

  def authenticate_user!
    return if current_user

    notice = "Please sign in to create the new issue."

233 234 235 236
    if request.get? && !request.xhr?
      store_location_for :user, request.fullpath
    end

237 238
    redirect_to new_user_session_path, notice: notice
  end
239 240 241 242

  def serializer
    IssueSerializer.new(current_user: current_user, project: issue.project)
  end
243 244 245 246 247

  def update_service
    update_params = issue_params.merge(spammable_params)
    Issues::UpdateService.new(project, current_user, update_params)
  end
248 249 250 251 252

  def set_issuables_index
    @finder_type = IssuesFinder
    super
  end
gitlabhq's avatar
gitlabhq committed
253
end