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

17 18 19
  # Cross-reference merge requests
  before_action :closed_by_merge_requests, only: [:show]

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

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

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

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

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

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

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

  def edit
    respond_with(@issue)
  end

  def show
61
    @participants = @issue.participants(current_user)
62
    @note = @project.notes.new(noteable: @issue)
V
Valery Sizov 已提交
63
    @notes = @issue.notes.nonawards.with_associations.fresh
64
    @noteable = @issue
G
gitlabhq 已提交
65

D
Dmitriy Zaporozhets 已提交
66
    respond_with(@issue)
G
gitlabhq 已提交
67 68 69
  end

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

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

  def update
87
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
G
gitlabhq 已提交
88 89 90

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

R
randx 已提交
107
  def bulk_update
108
    result = Issues::BulkUpdateService.new(project, current_user, bulk_update_params).execute
109
    redirect_back_or_default(default: { action: 'index' }, options: { notice: "#{result[:count]} issues updated" })
R
randx 已提交
110 111
  end

V
tests  
Valery Sizov 已提交
112 113
  def toggle_subscription
    @issue.toggle_subscription(current_user)
114

V
Valery Sizov 已提交
115 116 117
    render nothing: true
  end

118
  def closed_by_merge_requests
119
    @closed_by_merge_requests ||= @issue.closed_by_merge_requests(current_user)
120 121
  end

N
Nihad Abbasov 已提交
122
  protected
G
gitlabhq 已提交
123 124

  def issue
125
    @issue ||= begin
S
skv 已提交
126
                 @project.issues.find_by!(iid: params[:id])
127 128 129
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
130
  end
D
Dmitriy Zaporozhets 已提交
131

D
Dmitriy Zaporozhets 已提交
132
  def authorize_update_issue!
133
    return render_404 unless can?(current_user, :update_issue, @issue)
D
Dmitriy Zaporozhets 已提交
134 135
  end

D
Dmitriy Zaporozhets 已提交
136 137
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
138
  end
139 140

  def module_enabled
141
    return render_404 unless @project.issues_enabled && @project.default_issues_tracker?
142
  end
143

144 145 146 147 148 149
  # 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 已提交
150
    issue = @project.issues.find_by(id: params[:id])
151 152

    if issue
153
      redirect_to issue_path(issue)
154 155 156 157 158
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
159 160

  def issue_params
161
    permitted = params.require(:issue).permit(
162
      :title, :assignee_id, :position, :description,
163
      :milestone_id, :state_event, :task_num, label_ids: []
164
    )
165 166
    params[:issue][:title].strip! if params[:issue][:title]
    permitted
167
  end
168 169 170 171 172 173 174 175 176

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