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

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

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

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

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

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

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

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

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

35
    @issues = @issues.page(params[:page])
T
Tap 已提交
36
    @label = @project.labels.find_by(title: params[:label_name])
G
gitlabhq 已提交
37

G
gitlabhq 已提交
38
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
39
      format.html
40
      format.atom { render layout: false }
D
Dmitriy Zaporozhets 已提交
41 42 43 44 45
      format.json do
        render json: {
          html: view_to_html_string("projects/issues/_issues")
        }
      end
G
gitlabhq 已提交
46 47 48 49
    end
  end

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

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

  def edit
    respond_with(@issue)
  end

  def show
63 64
    @note     = @project.notes.new(noteable: @issue)
    @notes    = @issue.notes.nonawards.with_associations.fresh
65
    @noteable = @issue
G
gitlabhq 已提交
66

67 68 69 70 71 72 73
    respond_to do |format|
      format.html
      format.json do
        render json: @issue.to_json(include: [:milestone, :labels])
      end
    end

G
gitlabhq 已提交
74 75 76
  end

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

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

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

96 97
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
98
      move_service = Issues::MoveService.new(project, current_user)
99
      @issue = move_service.execute(@issue, new_project)
100
    end
G
gitlabhq 已提交
101 102 103

    respond_to do |format|
      format.js
104
      format.html do
105
        if @issue.valid?
106
          redirect_to issue_path(@issue)
107 108 109 110
        else
          render :edit
        end
      end
111
      format.json do
J
Jacob Schatz 已提交
112
        render json: @issue.to_json(include: [:milestone, :labels, assignee: { methods: :avatar_url }])
113
      end
G
gitlabhq 已提交
114 115 116
    end
  end

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  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
    merge_requests = @issue.referenced_merge_requests(current_user)

    @related_branches = @issue.related_branches -
      merge_requests.map(&:source_branch)

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

R
randx 已提交
145
  def bulk_update
146
    result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
147
    redirect_back_or_default(default: { action: 'index' }, options: { notice: "#{result[:count]} issues updated" })
R
randx 已提交
148 149
  end

N
Nihad Abbasov 已提交
150
  protected
G
gitlabhq 已提交
151 152

  def issue
153
    @issue ||= begin
S
skv 已提交
154
                 @project.issues.find_by!(iid: params[:id])
155 156 157
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
158
  end
159
  alias_method :subscribable_resource, :issue
160
  alias_method :issuable, :issue
D
Dmitriy Zaporozhets 已提交
161

162 163 164 165
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

D
Dmitriy Zaporozhets 已提交
166
  def authorize_update_issue!
167
    return render_404 unless can?(current_user, :update_issue, @issue)
D
Dmitriy Zaporozhets 已提交
168 169
  end

D
Dmitriy Zaporozhets 已提交
170 171
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
172
  end
173 174

  def module_enabled
175
    return render_404 unless @project.issues_enabled && @project.default_issues_tracker?
176
  end
177

178 179 180 181 182 183
  # 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 已提交
184
    issue = @project.issues.find_by(id: params[:id])
185 186

    if issue
187
      redirect_to issue_path(issue)
188 189 190 191 192
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
193 194

  def issue_params
195
    params.require(:issue).permit(
196
      :title, :assignee_id, :position, :description, :confidential,
197
      :milestone_id, :state_event, :task_num, label_ids: []
198 199
    )
  end
200 201 202 203 204 205 206 207 208

  def bulk_update_params
    params.require(:update).permit(
      :issues_ids,
      :assignee_id,
      :milestone_id,
      :state_event
    )
  end
G
gitlabhq 已提交
209
end