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)
T
Tap 已提交
35
    @label = @project.labels.find_by(title: params[:label_name])
G
gitlabhq 已提交
36

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

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

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

  def edit
    respond_with(@issue)
  end

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

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

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

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

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

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

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

V
tests  
Valery Sizov 已提交
113
  def toggle_subscription
114 115
    return unless current_user

V
tests  
Valery Sizov 已提交
116
    @issue.toggle_subscription(current_user)
117

V
Valery Sizov 已提交
118 119 120
    render nothing: true
  end

121
  def closed_by_merge_requests
122
    @closed_by_merge_requests ||= @issue.closed_by_merge_requests(current_user)
123 124
  end

N
Nihad Abbasov 已提交
125
  protected
G
gitlabhq 已提交
126 127

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

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

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

  def module_enabled
144
    return render_404 unless @project.issues_enabled && @project.default_issues_tracker?
145
  end
146

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

    if issue
156
      redirect_to issue_path(issue)
157 158 159 160 161
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
162 163

  def issue_params
164
    params.require(:issue).permit(
165
      :title, :assignee_id, :position, :description,
166
      :milestone_id, :state_event, :task_num, label_ids: []
167 168
    )
  end
169 170 171 172 173 174 175 176 177

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