issues_controller.rb 8.48 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]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13 14

  # Allow write(create) issue
15
  before_action :authorize_create_issue!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16 17

  # Allow modify issue
18
  before_action :authorize_update_issue!, only: [:edit, :update]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19

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

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

  def index
26 27 28
    @collection_type    = "Issue"
    @issues             = issues_collection
    @issues             = @issues.page(params[:page])
29
    @issuable_meta_data = issuable_meta_data(@issues, @collection_type)
30

31
    if @issues.out_of_range? && @issues.total_pages != 0
32
      return redirect_to url_for(params.merge(page: @issues.total_pages, only_path: true))
33
    end
34

35
    if params[:label_name].present?
36 37
      @labels = LabelsFinder.new(current_user, project_id: @project.id, title: params[:label_name]).execute
    end
gitlabhq's avatar
gitlabhq committed
38

39 40 41 42 43 44 45 46 47 48 49 50
    @users = []

    if params[:assignee_id].present?
      assignee = User.find_by_id(params[:assignee_id])
      @users.push(assignee) if assignee
    end

    if params[:author_id].present?
      author = User.find_by_id(params[:author_id])
      @users.push(author) if author
    end

gitlabhq's avatar
gitlabhq committed
51
    respond_to do |format|
52
      format.html
53
      format.atom { render layout: 'xml.atom' }
54 55
      format.json do
        render json: {
56
          html: view_to_html_string("projects/issues/_issues"),
57
          labels: @labels.as_json(methods: :text_color)
58 59
        }
      end
gitlabhq's avatar
gitlabhq committed
60 61 62 63
    end
  end

  def new
64
    params[:issue] ||= ActionController::Parameters.new(
65
      assignee_ids: ""
66
    )
67
    build_params = issue_params.merge(
Bob Van Landuyt's avatar
Bob Van Landuyt committed
68
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
69
      discussion_to_resolve: params[:discussion_to_resolve]
70
    )
71
    service = Issues::BuildService.new(project, current_user, build_params)
72

73
    @issue = @noteable = service.execute
Bob Van Landuyt's avatar
Bob Van Landuyt committed
74
    @merge_request_to_resolve_discussions_of = service.merge_request_to_resolve_discussions_of
75
    @discussion_to_resolve = service.discussions_to_resolve.first if params[:discussion_to_resolve]
76

gitlabhq's avatar
gitlabhq committed
77 78 79 80 81 82 83 84
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
85
    @noteable = @issue
86
    @note     = @project.notes.new(noteable: @issue)
gitlabhq's avatar
gitlabhq committed
87

88 89
    @discussions = @issue.discussions
    @notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes))
90

91 92 93
    respond_to do |format|
      format.html
      format.json do
94
        render json: serializer.represent(@issue)
95 96
      end
    end
gitlabhq's avatar
gitlabhq committed
97 98
  end

99
  def discussions
100 101 102 103 104
    notes = @issue.notes.inc_relations_for_view.includes(:noteable).fresh.to_a
    notes.reject! { |n| n.cross_reference_not_visible_for?(current_user) }
    prepare_notes_for_rendering(notes)

    @discussions = Discussion.build_collection(notes, @issue)
105 106 107 108

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

gitlabhq's avatar
gitlabhq committed
109
  def create
110
    create_params = issue_params.merge(spammable_params).merge(
Bob Van Landuyt's avatar
Bob Van Landuyt committed
111
      merge_request_to_resolve_discussions_of: params[:merge_request_to_resolve_discussions_of],
112 113 114
      discussion_to_resolve: params[:discussion_to_resolve]
    )

115 116 117
    service = Issues::CreateService.new(project, current_user, create_params)
    @issue = service.execute

118
    if service.discussions_to_resolve.count(&:resolved?) > 0
119
      flash[:notice] = if service.discussion_to_resolve_id
120 121 122 123
                         "Resolved 1 discussion."
                       else
                         "Resolved all discussions."
                       end
124
    end
gitlabhq's avatar
gitlabhq committed
125

126
    respond_to do |format|
127
      format.html do
128
        recaptcha_check_with_fallback { render :new }
129
      end
130
      format.js do
131 132
        @link = @issue.attachment.url.to_js
      end
133
    end
gitlabhq's avatar
gitlabhq committed
134 135 136
  end

  def update
137 138 139
    update_params = issue_params.merge(spammable_params)

    @issue = Issues::UpdateService.new(project, current_user, update_params).execute(issue)
gitlabhq's avatar
gitlabhq committed
140

141 142
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
143 144
      return render_404 unless issue.can_move?(current_user, new_project)

145
      move_service = Issues::MoveService.new(project, current_user)
146
      @issue = move_service.execute(@issue, new_project)
147
    end
gitlabhq's avatar
gitlabhq committed
148 149

    respond_to do |format|
150
      format.html do
151
        recaptcha_check_with_fallback { render :edit }
152
      end
153

154
      format.json do
155
        if @issue.valid?
156
          render json: serializer.represent(@issue)
157 158 159
        else
          render json: { errors: @issue.errors.full_messages }, status: :unprocessable_entity
        end
160
      end
gitlabhq's avatar
gitlabhq committed
161
    end
162 163

  rescue ActiveRecord::StaleObjectError
164
    render_conflict_response
gitlabhq's avatar
gitlabhq committed
165 166
  end

167 168 169 170 171 172 173 174 175 176 177 178 179 180
  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
181
    @related_branches = @issue.related_branches(current_user)
182 183 184 185 186 187 188 189 190 191

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

192 193 194 195 196 197 198
  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
199
        render json: { can_create_branch: can_create, has_related_branch: @issue.has_related_branch? }
200 201 202 203
      end
    end
  end

204
  def realtime_changes
Regis Boudinot's avatar
Regis Boudinot committed
205
    Gitlab::PollingInterval.set_header(response, interval: 3_000)
Regis's avatar
Regis committed
206

207
    response = {
208
      title: view_context.markdown_field(@issue, :title),
Regis's avatar
Regis committed
209
      title_text: @issue.title,
210 211
      description: view_context.markdown_field(@issue, :description),
      description_text: @issue.description,
212
      task_status: @issue.task_status
213
    }
214 215 216 217 218 219 220 221

    if @issue.is_edited?
      response[:updated_at] = @issue.updated_at
      response[:updated_by_name] = @issue.last_edited_by.name
      response[:updated_by_path] = user_path(@issue.last_edited_by)
    end

    render json: response
Regis Boudinot's avatar
Regis Boudinot committed
222 223
  end

224
  def create_merge_request
225
    result = ::MergeRequests::CreateFromIssueService.new(project, current_user, issue_iid: issue.iid).execute
226 227 228 229 230 231 232 233

    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
234
  protected
gitlabhq's avatar
gitlabhq committed
235 236

  def issue
237
    return @issue if defined?(@issue)
238
    # The Sortable default scope causes performance issues when used with find_by
239
    @noteable = @issue ||= @project.issues.where(iid: params[:id]).reorder(nil).take!
240 241 242 243

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

    @issue
gitlabhq's avatar
gitlabhq committed
244
  end
245
  alias_method :subscribable_resource, :issue
246
  alias_method :issuable, :issue
247
  alias_method :awardable, :issue
248
  alias_method :spammable, :issue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
249

250 251 252 253
  def spammable_path
    project_issue_path(@project, @issue)
  end

254
  def authorize_update_issue!
255
    render_404 unless can?(current_user, :update_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
256 257
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
258
  def authorize_admin_issues!
259
    render_404 unless can?(current_user, :admin_issue, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
260
  end
261

262
  def authorize_create_merge_request!
263
    render_404 unless can?(current_user, :push_code, @project) && @issue.can_be_worked_on?(current_user)
264 265
  end

266
  def check_issues_available!
267
    return render_404 unless @project.feature_available?(:issues, current_user)
268
  end
269

270
  def issue_params
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    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
    ] + [{ label_ids: [], assignee_ids: [] }]
287
  end
288 289 290 291 292 293

  def authenticate_user!
    return if current_user

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

294 295 296 297
    if request.get? && !request.xhr?
      store_location_for :user, request.fullpath
    end

298 299
    redirect_to new_user_session_path, notice: notice
  end
300 301 302 303

  def serializer
    IssueSerializer.new(current_user: current_user, project: issue.project)
  end
gitlabhq's avatar
gitlabhq committed
304
end