issues_controller.rb 6.3 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 23

  # Allow issues bulk update
24
  before_action :authorize_admin_issues!, only: [:bulk_update]
D
Dmitriy Zaporozhets 已提交
25

D
Dmitriy Zaporozhets 已提交
26
  respond_to :html
G
gitlabhq 已提交
27 28

  def index
29
    terms = params['issue_search']
30
    @issues = issues_collection
31 32 33 34 35 36 37 38 39

    if terms.present?
      if terms =~ /\A#(\d+)\z/
        @issues = @issues.where(iid: $1)
      else
        @issues = @issues.full_search(terms)
      end
    end

40
    @issues = @issues.page(params[:page])
41
    @labels = @project.labels.where(title: params[:label_name])
G
gitlabhq 已提交
42

G
gitlabhq 已提交
43
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
44
      format.html
45
      format.atom { render layout: false }
D
Dmitriy Zaporozhets 已提交
46 47
      format.json do
        render json: {
48
          html: view_to_html_string("projects/issues/_issues"),
P
Phil Hughes 已提交
49
          labels: @labels.as_json(methods: :text_color)
D
Dmitriy Zaporozhets 已提交
50 51
        }
      end
G
gitlabhq 已提交
52 53 54 55
    end
  end

  def new
56 57 58 59
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )

60
    @issue = @noteable = @project.issues.new(issue_params)
G
gitlabhq 已提交
61 62 63 64 65 66 67 68
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
69 70 71 72 73
    raw_notes = @issue.notes_with_associations.fresh

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

74
    @note     = @project.notes.new(noteable: @issue)
75
    @noteable = @issue
G
gitlabhq 已提交
76

S
Stan Hu 已提交
77
    preload_max_access_for_authors(@notes, @project)
78

79 80 81 82 83 84
    respond_to do |format|
      format.html
      format.json do
        render json: @issue.to_json(include: [:milestone, :labels])
      end
    end
G
gitlabhq 已提交
85 86 87
  end

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

90
    respond_to do |format|
91
      format.html do
92
        if @issue.valid?
93
          redirect_to issue_path(@issue)
94 95 96 97
        else
          render :new
        end
      end
98
      format.js do
99 100
        @link = @issue.attachment.url.to_js
      end
101
    end
G
gitlabhq 已提交
102 103 104
  end

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

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

111
      move_service = Issues::MoveService.new(project, current_user)
112
      @issue = move_service.execute(@issue, new_project)
113
    end
G
gitlabhq 已提交
114 115

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

124
      format.json do
125
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } })
126
      end
G
gitlabhq 已提交
127
    end
128 129 130 131

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

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

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

159 160 161 162 163 164 165 166 167 168 169 170
  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

R
randx 已提交
171
  def bulk_update
172
    result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
A
Alfredo Sumaran 已提交
173 174 175 176 177 178

    respond_to do |format|
      format.json do
        render json: { notice: "#{result[:count]} issues updated" }
      end
    end
R
randx 已提交
179 180
  end

N
Nihad Abbasov 已提交
181
  protected
G
gitlabhq 已提交
182 183

  def issue
184
    @noteable = @issue ||= @project.issues.find_by(iid: params[:id]) || redirect_old
G
gitlabhq 已提交
185
  end
186
  alias_method :subscribable_resource, :issue
187
  alias_method :issuable, :issue
188
  alias_method :awardable, :issue
189
  alias_method :spammable, :issue
D
Dmitriy Zaporozhets 已提交
190

191 192 193 194
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

D
Dmitriy Zaporozhets 已提交
195
  def authorize_update_issue!
196
    return render_404 unless can?(current_user, :update_issue, @issue)
D
Dmitriy Zaporozhets 已提交
197 198
  end

D
Dmitriy Zaporozhets 已提交
199 200
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
201
  end
202 203

  def module_enabled
204
    return render_404 unless @project.issues_enabled && @project.default_issues_tracker?
205
  end
206

207
  def redirect_to_external_issue_tracker
208
    external = @project.external_issue_tracker
209

210 211 212 213 214 215 216
    return unless external

    if action_name == 'new'
      redirect_to external.new_issue_path
    else
      redirect_to external.issues_url
    end
217 218
  end

219 220 221 222 223 224
  # 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 已提交
225
    issue = @project.issues.find_by(id: params[:id])
226 227

    if issue
228
      redirect_to issue_path(issue)
229 230 231 232
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
233 234

  def issue_params
235
    params.require(:issue).permit(
236
      :title, :assignee_id, :position, :description, :confidential,
237
      :milestone_id, :due_date, :state_event, :task_num, :lock_version, label_ids: []
238 239
    )
  end
240 241 242 243 244 245

  def bulk_update_params
    params.require(:update).permit(
      :issues_ids,
      :assignee_id,
      :milestone_id,
246
      :state_event,
247
      :subscription_event,
248 249 250
      label_ids: [],
      add_label_ids: [],
      remove_label_ids: []
251 252
    )
  end
G
gitlabhq 已提交
253
end