issues_controller.rb 5.7 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]
R
randx 已提交
13

D
Dmitriy Zaporozhets 已提交
14
  # Allow read any issue
15
  before_action :authorize_read_issue!, only: [:show]
D
Dmitriy Zaporozhets 已提交
16 17

  # Allow write(create) issue
D
Dmitriy Zaporozhets 已提交
18
  before_action :authorize_create_issue!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
19 20

  # Allow modify issue
D
Dmitriy Zaporozhets 已提交
21
  before_action :authorize_update_issue!, only: [:edit, :update]
D
Dmitriy Zaporozhets 已提交
22

D
Dmitriy Zaporozhets 已提交
23
  respond_to :html
G
gitlabhq 已提交
24 25

  def index
26
    @issues = issues_collection
27
    @issues = @issues.page(params[:page])
28

29
    if params[:label_name].present?
30 31
      @labels = LabelsFinder.new(current_user, project_id: @project.id, title: params[:label_name]).execute
    end
G
gitlabhq 已提交
32

G
gitlabhq 已提交
33
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
34
      format.html
35
      format.atom { render layout: false }
D
Dmitriy Zaporozhets 已提交
36 37
      format.json do
        render json: {
38
          html: view_to_html_string("projects/issues/_issues"),
P
Phil Hughes 已提交
39
          labels: @labels.as_json(methods: :text_color)
D
Dmitriy Zaporozhets 已提交
40 41
        }
      end
G
gitlabhq 已提交
42 43 44 45
    end
  end

  def new
46 47 48 49
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )

50
    @issue = @noteable = @project.issues.new(issue_params)
G
gitlabhq 已提交
51 52 53 54 55 56 57 58
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
59
    raw_notes = @issue.notes.inc_relations_for_view.fresh
60 61 62 63

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

64
    @note     = @project.notes.new(noteable: @issue)
65
    @noteable = @issue
G
gitlabhq 已提交
66

S
Stan Hu 已提交
67
    preload_max_access_for_authors(@notes, @project)
68

69 70 71
    respond_to do |format|
      format.html
      format.json do
72
        render json: IssueSerializer.new.represent(@issue)
73 74
      end
    end
G
gitlabhq 已提交
75 76 77
  end

  def create
78
    @issue = Issues::CreateService.new(project, current_user, issue_params.merge(request: request)).execute
G
gitlabhq 已提交
79

80
    respond_to do |format|
81
      format.html do
82
        if @issue.valid?
83
          redirect_to issue_path(@issue)
84 85 86 87
        else
          render :new
        end
      end
88
      format.js do
89 90
        @link = @issue.attachment.url.to_js
      end
91
    end
G
gitlabhq 已提交
92 93 94
  end

  def update
95
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
G
gitlabhq 已提交
96

97 98
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
99 100
      return render_404 unless issue.can_move?(current_user, new_project)

101
      move_service = Issues::MoveService.new(project, current_user)
102
      @issue = move_service.execute(@issue, new_project)
103
    end
G
gitlabhq 已提交
104 105

    respond_to do |format|
106
      format.html do
107
        if @issue.valid?
108
          redirect_to issue_path(@issue)
109 110 111 112
        else
          render :edit
        end
      end
113

114
      format.json do
115
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } }, methods: [:task_status, :task_status_short])
116
      end
G
gitlabhq 已提交
117
    end
118 119 120 121

  rescue ActiveRecord::StaleObjectError
    @conflict = true
    render :edit
G
gitlabhq 已提交
122 123
  end

124 125 126 127 128 129 130 131 132 133 134 135 136 137
  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
138
    @related_branches = @issue.related_branches(current_user)
139 140 141 142 143 144 145 146 147 148

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

149 150 151 152 153 154 155 156 157 158 159 160
  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

N
Nihad Abbasov 已提交
161
  protected
G
gitlabhq 已提交
162 163

  def issue
164 165
    # 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
G
gitlabhq 已提交
166
  end
167
  alias_method :subscribable_resource, :issue
168
  alias_method :issuable, :issue
169
  alias_method :awardable, :issue
170
  alias_method :spammable, :issue
D
Dmitriy Zaporozhets 已提交
171

172 173 174 175
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

D
Dmitriy Zaporozhets 已提交
176
  def authorize_update_issue!
177
    return render_404 unless can?(current_user, :update_issue, @issue)
D
Dmitriy Zaporozhets 已提交
178 179
  end

D
Dmitriy Zaporozhets 已提交
180 181
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
182
  end
183 184

  def module_enabled
F
Felipe Artur 已提交
185
    return render_404 unless @project.feature_available?(:issues, current_user) && @project.default_issues_tracker?
186
  end
187

188
  def redirect_to_external_issue_tracker
189
    external = @project.external_issue_tracker
190

191 192 193 194 195
    return unless external

    if action_name == 'new'
      redirect_to external.new_issue_path
    else
196
      redirect_to external.project_path
197
    end
198 199
  end

200 201 202 203 204 205
  # 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
S
skv 已提交
206
    issue = @project.issues.find_by(id: params[:id])
207 208

    if issue
209
      redirect_to issue_path(issue)
210 211 212 213
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
214 215

  def issue_params
216
    params.require(:issue).permit(
217
      :title, :assignee_id, :position, :description, :confidential,
218
      :milestone_id, :due_date, :state_event, :task_num, :lock_version, label_ids: []
219 220
    )
  end
G
gitlabhq 已提交
221
end