issues_controller.rb 7.1 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2
  include NotesHelper
3
  include ToggleSubscriptionAction
4
  include IssuableActions
5
  include ToggleAwardEmoji
6
  include IssuableCollections
7
  include SpammableActions
8

9
  before_action :redirect_to_external_issue_tracker, only: [:index, :new]
10
  before_action :module_enabled
11 12
  before_action :issue, only: [:edit, :update, :show, :referenced_merge_requests,
                               :related_branches, :can_create_branch]
randx's avatar
randx committed
13

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14
  # Allow read any issue
15
  before_action :authorize_read_issue!, only: [:show]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
16 17

  # Allow write(create) issue
18
  before_action :authorize_create_issue!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
19 20

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

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

  def index
26 27
    return redirect_to_fixed_params if params[:assignee_id].present? || params[:author_id].present?

28
    @issues = issues_collection
29 30
    @issues = @issues.page(params[:page])
    if @issues.out_of_range? && @issues.total_pages != 0
31
      return redirect_to url_for(params.merge(page: @issues.total_pages))
32
    end
33

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

gitlabhq's avatar
gitlabhq committed
38
    respond_to do |format|
39
      format.html
40
      format.atom { render layout: false }
41 42
      format.json do
        render json: {
43
          html: view_to_html_string("projects/issues/_issues"),
44
          labels: @labels.as_json(methods: :text_color)
45 46
        }
      end
gitlabhq's avatar
gitlabhq committed
47 48 49 50
    end
  end

  def new
51 52 53
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )
54 55
    build_params = issue_params.merge(merge_request_for_resolving_discussions: merge_request_for_resolving_discussions)
    @issue = @noteable = Issues::BuildService.new(project, current_user, build_params).execute
56

gitlabhq's avatar
gitlabhq committed
57 58 59 60 61 62 63 64
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
65
    raw_notes = @issue.notes.inc_relations_for_view.fresh
66 67 68 69

    @notes = Banzai::NoteRenderer.
      render(raw_notes, @project, current_user, @path, @project_wiki, @ref)

70
    @note     = @project.notes.new(noteable: @issue)
71
    @noteable = @issue
gitlabhq's avatar
gitlabhq committed
72

Stan Hu's avatar
Stan Hu committed
73
    preload_max_access_for_authors(@notes, @project)
74

75 76 77
    respond_to do |format|
      format.html
      format.json do
78
        render json: IssueSerializer.new.represent(@issue)
79 80
      end
    end
gitlabhq's avatar
gitlabhq committed
81 82 83
  end

  def create
84 85 86
    extra_params = { request: request,
                     merge_request_for_resolving_discussions: merge_request_for_resolving_discussions }
    @issue = Issues::CreateService.new(project, current_user, issue_params.merge(extra_params)).execute
gitlabhq's avatar
gitlabhq committed
87

88
    respond_to do |format|
89
      format.html do
90
        if @issue.valid?
91
          redirect_to issue_path(@issue)
92 93 94 95
        else
          render :new
        end
      end
96
      format.js do
97 98
        @link = @issue.attachment.url.to_js
      end
99
    end
gitlabhq's avatar
gitlabhq committed
100 101 102
  end

  def update
103
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
gitlabhq's avatar
gitlabhq committed
104

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

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

    respond_to do |format|
114
      format.html do
115
        if @issue.valid?
116
          redirect_to issue_path(@issue)
117 118 119 120
        else
          render :edit
        end
      end
121

122
      format.json do
123
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } }, methods: [:task_status, :task_status_short])
124
      end
gitlabhq's avatar
gitlabhq committed
125
    end
126 127 128 129

  rescue ActiveRecord::StaleObjectError
    @conflict = true
    render :edit
gitlabhq's avatar
gitlabhq committed
130 131
  end

132 133 134 135 136 137 138 139 140 141 142 143 144 145
  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
146
    @related_branches = @issue.related_branches(current_user)
147 148 149 150 151 152 153 154 155 156

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

157 158 159 160 161 162 163 164 165 166 167 168
  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
        render json: { can_create_branch: can_create }
      end
    end
  end

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

  def issue
172 173
    # The Sortable default scope causes performance issues when used with find_by
    @noteable = @issue ||= @project.issues.where(iid: params[:id]).reorder(nil).take || redirect_old
gitlabhq's avatar
gitlabhq committed
174
  end
175
  alias_method :subscribable_resource, :issue
176
  alias_method :issuable, :issue
177
  alias_method :awardable, :issue
178
  alias_method :spammable, :issue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
179

180 181 182 183 184 185 186 187
  def merge_request_for_resolving_discussions
    return unless merge_request_iid = params[:merge_request_for_resolving_discussions]

    @merge_request_for_resolving_discussions ||= MergeRequestsFinder.new(current_user, project_id: project.id).
                                                   execute.
                                                   find_by(iid: merge_request_iid)
  end

188 189 190 191
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

192
  def authorize_update_issue!
193
    return render_404 unless can?(current_user, :update_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
194 195
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
196 197
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
198
  end
199 200

  def module_enabled
201
    return render_404 unless @project.feature_available?(:issues, current_user) && @project.default_issues_tracker?
202
  end
203

204
  def redirect_to_external_issue_tracker
205
    external = @project.external_issue_tracker
206

207 208 209 210 211
    return unless external

    if action_name == 'new'
      redirect_to external.new_issue_path
    else
212
      redirect_to external.project_path
213
    end
214 215
  end

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  def redirect_to_fixed_params
    fixed_params = params.except(:assignee_id, :author_id)

    if params[:assignee_id].present?
      assignee = User.find_by_id(params[:assignee_id])
      fixed_params.merge!(assignee_username: assignee.username) if assignee
    end

    if params[:author_id].present?
      author = User.find_by_id(params[:author_id])
      fixed_params.merge!(author_username: author.username) if author
    end

    redirect_to url_for(fixed_params)
  end

232 233 234 235 236 237
  # Since iids are implemented only in 6.1
  # user may navigate to issue page using old global ids.
  #
  # To prevent 404 errors we provide a redirect to correct iids until 7.0 release
  #
  def redirect_old
skv's avatar
skv committed
238
    issue = @project.issues.find_by(id: params[:id])
239 240

    if issue
241
      redirect_to issue_path(issue)
242 243 244 245
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
246 247

  def issue_params
248
    params.require(:issue).permit(
249
      :title, :assignee_id, :position, :description, :confidential,
250
      :milestone_id, :due_date, :state_event, :task_num, :lock_version, label_ids: []
251 252
    )
  end
gitlabhq's avatar
gitlabhq committed
253
end