issues_controller.rb 5.9 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2
  include ToggleSubscriptionAction
3
  include IssuableActions
4
  include ToggleAwardEmoji
5

6
  before_action :module_enabled
7 8
  before_action :issue, only: [:edit, :update, :show, :referenced_merge_requests,
                               :related_branches, :can_create_branch]
R
randx 已提交
9

D
Dmitriy Zaporozhets 已提交
10
  # Allow read any issue
11
  before_action :authorize_read_issue!, only: [:show]
D
Dmitriy Zaporozhets 已提交
12 13

  # Allow write(create) issue
D
Dmitriy Zaporozhets 已提交
14
  before_action :authorize_create_issue!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
15 16

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

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

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

  def index
25
    terms = params['issue_search']
D
Dmitriy Zaporozhets 已提交
26
    @issues = get_issues_collection
27 28 29 30 31 32 33 34 35

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

36
    @issues = @issues.page(params[:page])
37
    @labels = @project.labels.where(title: params[:label_name])
G
gitlabhq 已提交
38

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

  def new
52 53 54 55
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )

56
    @issue = @noteable = @project.issues.new(issue_params)
G
gitlabhq 已提交
57 58 59 60 61 62 63 64
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

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

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

70
    @note     = @project.notes.new(noteable: @issue)
71
    @noteable = @issue
G
gitlabhq 已提交
72

73 74 75 76 77 78
    respond_to do |format|
      format.html
      format.json do
        render json: @issue.to_json(include: [:milestone, :labels])
      end
    end
G
gitlabhq 已提交
79 80 81
  end

  def create
82
    @issue = Issues::CreateService.new(project, current_user, issue_params).execute
G
gitlabhq 已提交
83

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

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

101 102
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
103 104
      return render_404 unless issue.can_move?(current_user, new_project)

105
      move_service = Issues::MoveService.new(project, current_user)
106
      @issue = move_service.execute(@issue, new_project)
107
    end
G
gitlabhq 已提交
108 109

    respond_to do |format|
110
      format.html do
111
        if @issue.valid?
112
          redirect_to issue_path(@issue)
113 114 115 116
        else
          render :edit
        end
      end
117

118
      format.json do
119
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } })
120
      end
G
gitlabhq 已提交
121
    end
122 123 124 125

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

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

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

153 154 155 156 157 158 159 160 161 162 163 164
  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 已提交
165
  def bulk_update
166
    result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
A
Alfredo Sumaran 已提交
167 168 169 170 171 172

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

N
Nihad Abbasov 已提交
175
  protected
G
gitlabhq 已提交
176 177

  def issue
178
    @issue ||= begin
S
skv 已提交
179
                 @project.issues.find_by!(iid: params[:id])
180 181 182
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
183
  end
184
  alias_method :subscribable_resource, :issue
185
  alias_method :issuable, :issue
186
  alias_method :awardable, :issue
D
Dmitriy Zaporozhets 已提交
187

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

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

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

  def module_enabled
201
    return render_404 unless @project.issues_enabled && @project.default_issues_tracker?
202
  end
203

204 205 206 207 208 209
  # 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 已提交
210
    issue = @project.issues.find_by(id: params[:id])
211 212

    if issue
213
      redirect_to issue_path(issue)
214 215 216 217 218
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
219 220

  def issue_params
221
    params.require(:issue).permit(
222
      :title, :assignee_id, :position, :description, :confidential,
223
      :milestone_id, :due_date, :state_event, :task_num, :lock_version, label_ids: []
224 225
    )
  end
226 227 228 229 230 231

  def bulk_update_params
    params.require(:update).permit(
      :issues_ids,
      :assignee_id,
      :milestone_id,
232
      :state_event,
233 234 235
      label_ids: [],
      add_label_ids: [],
      remove_label_ids: []
236 237
    )
  end
G
gitlabhq 已提交
238
end