issues_controller.rb 5.9 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

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

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

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

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

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

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

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

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

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

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

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

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

  def edit
    respond_with(@issue)
  end

  def show
67 68 69 70 71
    raw_notes = @issue.notes_with_associations.fresh

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

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

S
Stan Hu 已提交
75
    preload_max_access_for_authors(@notes, @project)
76

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

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

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

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

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

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

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

122
      format.json do
123
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } })
124
      end
G
gitlabhq 已提交
125 126 127
    end
  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, 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
      :subscription_event,
234 235 236
      label_ids: [],
      add_label_ids: [],
      remove_label_ids: []
237 238
    )
  end
G
gitlabhq 已提交
239
end