issues_controller.rb 7.19 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
    @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))
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
    build_params = issue_params.merge(
68 69
      merge_request_for_resolving_discussions: params[:merge_request_for_resolving_discussions],
      discussion_to_resolve: params[:discussion_to_resolve]
70
    )
71
    service = Issues::BuildService.new(project, current_user, build_params)
72

73
    @issue = @noteable = service.execute
74 75
    @merge_request_for_resolving_discussions = service.merge_request_for_resolving_discussions
    @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
    raw_notes = @issue.notes.inc_relations_for_view.fresh
86 87 88 89

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

90
    @note     = @project.notes.new(noteable: @issue)
91
    @noteable = @issue
gitlabhq's avatar
gitlabhq committed
92

Stan Hu's avatar
Stan Hu committed
93
    preload_max_access_for_authors(@notes, @project)
94

95 96 97
    respond_to do |format|
      format.html
      format.json do
98
        render json: IssueSerializer.new.represent(@issue)
99 100
      end
    end
gitlabhq's avatar
gitlabhq committed
101 102 103
  end

  def create
104 105 106 107 108
    create_params = issue_params.merge(spammable_params).merge(
      merge_request_for_resolving_discussions: params[:merge_request_for_resolving_discussions],
      discussion_to_resolve: params[:discussion_to_resolve]
    )

109 110 111 112 113 114 115
    service = Issues::CreateService.new(project, current_user, create_params)
    @issue = service.execute

    if service.discussions_to_resolve.any? && service.discussions_to_resolve.all?(&:resolved?)
      resolve_count = service.discussions_to_resolve.size
      flash[:notice] = "Resolved #{resolve_count} #{'discussion'.pluralize(resolve_count)}."
    end
gitlabhq's avatar
gitlabhq committed
116

117
    respond_to do |format|
118
      format.html do
119
        recaptcha_check_with_fallback { render :new }
120
      end
121
      format.js do
122 123
        @link = @issue.attachment.url.to_js
      end
124
    end
gitlabhq's avatar
gitlabhq committed
125 126 127
  end

  def update
128 129 130
    update_params = issue_params.merge(spammable_params)

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

132 133
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
134 135
      return render_404 unless issue.can_move?(current_user, new_project)

136
      move_service = Issues::MoveService.new(project, current_user)
137
      @issue = move_service.execute(@issue, new_project)
138
    end
gitlabhq's avatar
gitlabhq committed
139 140

    respond_to do |format|
141
      format.html do
142
        recaptcha_check_with_fallback { render :edit }
143
      end
144

145
      format.json do
146
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } }, methods: [:task_status, :task_status_short])
147
      end
gitlabhq's avatar
gitlabhq committed
148
    end
149 150

  rescue ActiveRecord::StaleObjectError
151
    render_conflict_response
gitlabhq's avatar
gitlabhq committed
152 153
  end

154 155 156 157 158 159 160 161 162 163 164 165 166 167
  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
168
    @related_branches = @issue.related_branches(current_user)
169 170 171 172 173 174 175 176 177 178

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

179 180 181 182 183 184 185 186 187 188 189 190
  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
191
  protected
gitlabhq's avatar
gitlabhq committed
192 193

  def issue
194 195
    # 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
196
  end
197
  alias_method :subscribable_resource, :issue
198
  alias_method :issuable, :issue
199
  alias_method :awardable, :issue
200
  alias_method :spammable, :issue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
201

202 203 204 205
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

206
  def authorize_update_issue!
207
    return render_404 unless can?(current_user, :update_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
208 209
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
210 211
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
212
  end
213 214

  def module_enabled
215
    return render_404 unless @project.feature_available?(:issues, current_user) && @project.default_issues_tracker?
216
  end
217

218
  def redirect_to_external_issue_tracker
219
    external = @project.external_issue_tracker
220

221 222 223 224 225
    return unless external

    if action_name == 'new'
      redirect_to external.new_issue_path
    else
226
      redirect_to external.project_path
227
    end
228 229
  end

230 231 232 233 234 235
  # 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
236
    issue = @project.issues.find_by(id: params[:id])
237 238

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

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