issues_controller.rb 3.8 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2 3
  before_action :module_enabled
  before_action :issue, only: [:edit, :update, :show, :toggle_subscription]
R
randx 已提交
4

D
Dmitriy Zaporozhets 已提交
5
  # Allow read any issue
6
  before_action :authorize_read_issue!
D
Dmitriy Zaporozhets 已提交
7 8

  # Allow write(create) issue
D
Dmitriy Zaporozhets 已提交
9
  before_action :authorize_create_issue!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
10 11

  # Allow modify issue
D
Dmitriy Zaporozhets 已提交
12
  before_action :authorize_update_issue!, only: [:edit, :update]
D
Dmitriy Zaporozhets 已提交
13 14

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

D
Dmitriy Zaporozhets 已提交
17
  respond_to :html
G
gitlabhq 已提交
18 19

  def index
20
    terms = params['issue_search']
D
Dmitriy Zaporozhets 已提交
21
    @issues = get_issues_collection
22 23 24 25 26 27 28 29 30

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

31
    @issues = @issues.page(params[:page]).per(PER_PAGE)
G
gitlabhq 已提交
32

G
gitlabhq 已提交
33
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
34
      format.html
35
      format.atom { render layout: false }
D
Dmitriy Zaporozhets 已提交
36 37 38 39 40
      format.json do
        render json: {
          html: view_to_html_string("projects/issues/_issues")
        }
      end
G
gitlabhq 已提交
41 42 43 44
    end
  end

  def new
45 46 47 48
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )

49
    @issue = @project.issues.new(issue_params)
G
gitlabhq 已提交
50 51 52 53 54 55 56 57
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
58
    @participants = @issue.participants(current_user, @project)
59
    @note = @project.notes.new(noteable: @issue)
60
    @notes = @issue.notes.inc_author.fresh
61
    @noteable = @issue
G
gitlabhq 已提交
62

D
Dmitriy Zaporozhets 已提交
63
    respond_with(@issue)
G
gitlabhq 已提交
64 65 66
  end

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

69
    respond_to do |format|
70
      format.html do
71
        if @issue.valid?
72
          redirect_to issue_path(@issue)
73 74 75 76
        else
          render :new
        end
      end
77 78 79
      format.js do |format|
        @link = @issue.attachment.url.to_js
      end
80
    end
G
gitlabhq 已提交
81 82 83
  end

  def update
84
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
G
gitlabhq 已提交
85 86 87

    respond_to do |format|
      format.js
88
      format.html do
89
        if @issue.valid?
90
          redirect_to issue_path(@issue)
91 92 93 94
        else
          render :edit
        end
      end
95 96 97
      format.json do
        render json: {
          saved: @issue.valid?,
98
          assignee_avatar_url: @issue.assignee.try(:avatar_url)
99 100
        }
      end
G
gitlabhq 已提交
101 102 103
    end
  end

R
randx 已提交
104
  def bulk_update
105
    result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
106
    redirect_to :back, notice: "#{result[:count]} issues updated"
R
randx 已提交
107 108
  end

V
tests  
Valery Sizov 已提交
109 110
  def toggle_subscription
    @issue.toggle_subscription(current_user)
111

V
Valery Sizov 已提交
112 113 114
    render nothing: true
  end

N
Nihad Abbasov 已提交
115
  protected
G
gitlabhq 已提交
116 117

  def issue
118
    @issue ||= begin
S
skv 已提交
119
                 @project.issues.find_by!(iid: params[:id])
120 121 122
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
123
  end
D
Dmitriy Zaporozhets 已提交
124

D
Dmitriy Zaporozhets 已提交
125
  def authorize_update_issue!
126
    return render_404 unless can?(current_user, :update_issue, @issue)
D
Dmitriy Zaporozhets 已提交
127 128
  end

D
Dmitriy Zaporozhets 已提交
129 130
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
131
  end
132 133 134 135

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
136

137 138 139 140 141 142
  # 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 已提交
143
    issue = @project.issues.find_by(id: params[:id])
144 145

    if issue
146
      redirect_to issue_path(issue)
147 148 149 150 151
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
152 153 154 155

  def issue_params
    params.require(:issue).permit(
      :title, :assignee_id, :position, :description,
156
      :milestone_id, :state_event, :task_num, label_ids: []
157 158
    )
  end
159 160 161 162 163 164 165 166 167

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