issues_controller.rb 6.87 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 28 29 30
    @collection_type    = "Issue"
    @issues             = issues_collection
    @issues             = @issues.page(params[:page])
    @issuable_meta_data = issuable_meta_data(@issues)

31
    if @issues.out_of_range? && @issues.total_pages != 0
32
      return redirect_to url_for(params.merge(page: @issues.total_pages))
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: false }
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 65 66
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )
67 68
    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
69

gitlabhq's avatar
gitlabhq committed
70 71 72 73 74 75 76 77
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
78
    raw_notes = @issue.notes.inc_relations_for_view.fresh
79 80 81 82

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

83
    @note     = @project.notes.new(noteable: @issue)
84
    @noteable = @issue
gitlabhq's avatar
gitlabhq committed
85

Stan Hu's avatar
Stan Hu committed
86
    preload_max_access_for_authors(@notes, @project)
87

88 89 90
    respond_to do |format|
      format.html
      format.json do
91
        render json: IssueSerializer.new.represent(@issue)
92 93
      end
    end
gitlabhq's avatar
gitlabhq committed
94 95 96
  end

  def create
97 98 99
    create_params = issue_params
      .merge(merge_request_for_resolving_discussions: merge_request_for_resolving_discussions)
      .merge(spammable_params)
100

101
    @issue = Issues::CreateService.new(project, current_user, create_params).execute
gitlabhq's avatar
gitlabhq committed
102

103
    respond_to do |format|
104
      format.html do
105
        recaptcha_check_with_fallback { render :new }
106
      end
107
      format.js do
108 109
        @link = @issue.attachment.url.to_js
      end
110
    end
gitlabhq's avatar
gitlabhq committed
111 112 113
  end

  def update
114 115 116
    update_params = issue_params.merge(spammable_params)

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

118 119
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
120 121
      return render_404 unless issue.can_move?(current_user, new_project)

122
      move_service = Issues::MoveService.new(project, current_user)
123
      @issue = move_service.execute(@issue, new_project)
124
    end
gitlabhq's avatar
gitlabhq committed
125 126

    respond_to do |format|
127
      format.html do
128
        recaptcha_check_with_fallback { render :edit }
129
      end
130

131
      format.json do
132
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } }, methods: [:task_status, :task_status_short])
133
      end
gitlabhq's avatar
gitlabhq committed
134
    end
135 136 137 138

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

141 142 143 144 145 146 147 148 149 150 151 152 153 154
  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
155
    @related_branches = @issue.related_branches(current_user)
156 157 158 159 160 161 162 163 164 165

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

166 167 168 169 170 171 172 173 174 175 176 177
  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
178
  protected
gitlabhq's avatar
gitlabhq committed
179 180

  def issue
181 182
    # 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
183
  end
184
  alias_method :subscribable_resource, :issue
185
  alias_method :issuable, :issue
186
  alias_method :awardable, :issue
187
  alias_method :spammable, :issue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
188

189 190 191 192 193 194 195 196
  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

197 198 199 200
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

201
  def authorize_update_issue!
202
    return render_404 unless can?(current_user, :update_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
203 204
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
205 206
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
207
  end
208 209

  def module_enabled
210
    return render_404 unless @project.feature_available?(:issues, current_user) && @project.default_issues_tracker?
211
  end
212

213
  def redirect_to_external_issue_tracker
214
    external = @project.external_issue_tracker
215

216 217 218 219 220
    return unless external

    if action_name == 'new'
      redirect_to external.new_issue_path
    else
221
      redirect_to external.project_path
222
    end
223 224
  end

225 226 227 228 229 230
  # 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
231
    issue = @project.issues.find_by(id: params[:id])
232 233

    if issue
234
      redirect_to issue_path(issue)
235 236 237 238
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
239 240

  def issue_params
241
    params.require(:issue).permit(
242
      :title, :assignee_id, :position, :description, :confidential,
243
      :milestone_id, :due_date, :state_event, :task_num, :lock_version, label_ids: []
244 245
    )
  end
gitlabhq's avatar
gitlabhq committed
246
end