issues_controller.rb 5.04 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]
randx's avatar
randx committed
8

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9
  # Allow read any issue
10
  before_action :authorize_read_issue!, only: [:show]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
11 12

  # Allow write(create) issue
13
  before_action :authorize_create_issue!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
14 15

  # Allow modify issue
16
  before_action :authorize_update_issue!, only: [:edit, :update]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
17 18

  # Allow issues bulk update
19
  before_action :authorize_admin_issues!, only: [:bulk_update]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
20

21
  respond_to :html
gitlabhq's avatar
gitlabhq committed
22 23

  def index
24
    terms = params['issue_search']
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])
Tap's avatar
Tap committed
36
    @label = @project.labels.find_by(title: params[:label_name])
gitlabhq's avatar
gitlabhq committed
37

gitlabhq's avatar
gitlabhq committed
38
    respond_to do |format|
39
      format.html
40
      format.atom { render layout: false }
41 42 43 44 45
      format.json do
        render json: {
          html: view_to_html_string("projects/issues/_issues")
        }
      end
gitlabhq's avatar
gitlabhq committed
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)
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
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

gitlabhq's avatar
gitlabhq committed
74 75 76
  end

  def create
77
    @issue = Issues::CreateService.new(project, current_user, issue_params).execute
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
91 92 93
  end

  def update
94
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
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
112
        render json: @issue.to_json(include: [:milestone, :labels, assignee: { methods: :avatar_url }])
113
      end
gitlabhq's avatar
gitlabhq committed
114 115 116
    end
  end

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

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

randx's avatar
randx committed
142
  def bulk_update
143
    result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
144
    redirect_back_or_default(default: { action: 'index' }, options: { notice: "#{result[:count]} issues updated" })
randx's avatar
randx committed
145 146
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
147
  protected
gitlabhq's avatar
gitlabhq committed
148 149

  def issue
150
    @issue ||= begin
skv's avatar
skv committed
151
                 @project.issues.find_by!(iid: params[:id])
152 153 154
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
gitlabhq's avatar
gitlabhq committed
155
  end
156
  alias_method :subscribable_resource, :issue
157
  alias_method :issuable, :issue
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
158

159 160 161 162
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

163
  def authorize_update_issue!
164
    return render_404 unless can?(current_user, :update_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
165 166
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
167 168
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
169
  end
170 171

  def module_enabled
172
    return render_404 unless @project.issues_enabled && @project.default_issues_tracker?
173
  end
174

175 176 177 178 179 180
  # 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
skv's avatar
skv committed
181
    issue = @project.issues.find_by(id: params[:id])
182 183

    if issue
184
      redirect_to issue_path(issue)
185 186 187 188 189
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
190 191

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

  def bulk_update_params
    params.require(:update).permit(
      :issues_ids,
      :assignee_id,
      :milestone_id,
      :state_event
    )
  end
gitlabhq's avatar
gitlabhq committed
206
end