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

4
  before_action :module_enabled
5
  before_action :issue, only: [:edit, :update, :show]
R
randx 已提交
6

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

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

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

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

19 20 21
  # Cross-reference merge requests
  before_action :closed_by_merge_requests, only: [:show]

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

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

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

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

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

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

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

  def edit
    respond_with(@issue)
  end

  def show
64
    @note = @project.notes.new(noteable: @issue)
V
Valery Sizov 已提交
65
    @notes = @issue.notes.nonawards.with_associations.fresh
66
    @noteable = @issue
67
    @merge_requests = @issue.referenced_merge_requests(current_user)
Z
Zeger-Jan van de Weg 已提交
68
    @related_branches = @issue.related_branches - @merge_requests.map(&:source_branch)
G
gitlabhq 已提交
69

D
Dmitriy Zaporozhets 已提交
70
    respond_with(@issue)
G
gitlabhq 已提交
71 72 73
  end

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

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

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

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

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

116
  def closed_by_merge_requests
117
    @closed_by_merge_requests ||= @issue.closed_by_merge_requests(current_user)
118 119
  end

N
Nihad Abbasov 已提交
120
  protected
G
gitlabhq 已提交
121 122

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

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

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

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

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

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

  def issue_params
160
    params.require(:issue).permit(
161
      :title, :assignee_id, :position, :description, :confidential,
162
      :milestone_id, :state_event, :task_num, label_ids: []
163 164
    )
  end
165 166 167 168 169 170 171 172 173

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