issues_controller.rb 3.4 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2
  before_filter :module_enabled
D
Dmitriy Zaporozhets 已提交
3
  before_filter :issue, only: [:edit, :update, :show]
R
randx 已提交
4

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

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

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

  # Allow issues bulk update
  before_filter :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']
21 22 23
    set_filters_defaults
    @issues = IssuesFinder.new.execute(current_user, params)
    set_filter_values(@issues)
24
    @issues = @issues.full_search(terms) if terms.present?
D
Dmitriy Zaporozhets 已提交
25
    @issues = @issues.page(params[:page]).per(20)
G
gitlabhq 已提交
26

G
gitlabhq 已提交
27
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
28
      format.html
29
      format.atom { render layout: false }
D
Dmitriy Zaporozhets 已提交
30 31 32 33 34
      format.json do
        render json: {
          html: view_to_html_string("projects/issues/_issues")
        }
      end
G
gitlabhq 已提交
35 36 37 38
    end
  end

  def new
39 40 41 42
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )

43
    @issue = @project.issues.new(issue_params)
G
gitlabhq 已提交
44 45 46 47 48 49 50 51
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
52
    @note = @project.notes.new(noteable: @issue)
53
    @notes = @issue.notes.inc_author.fresh
54
    @noteable = @issue
G
gitlabhq 已提交
55

D
Dmitriy Zaporozhets 已提交
56
    respond_with(@issue)
G
gitlabhq 已提交
57 58 59
  end

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

62
    respond_to do |format|
63
      format.html do
64
        if @issue.valid?
65 66 67 68 69
          redirect_to project_issue_path(@project, @issue)
        else
          render :new
        end
      end
70 71 72
      format.js do |format|
        @link = @issue.attachment.url.to_js
      end
73
    end
G
gitlabhq 已提交
74 75 76
  end

  def update
77
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
G
gitlabhq 已提交
78 79 80

    respond_to do |format|
      format.js
81
      format.html do
82 83 84 85 86 87
        if @issue.valid?
          redirect_to [@project, @issue]
        else
          render :edit
        end
      end
88 89 90
      format.json do
        render json: {
          saved: @issue.valid?,
91
          assignee_avatar_url: @issue.assignee.try(:avatar_url)
92 93
        }
      end
G
gitlabhq 已提交
94 95 96
    end
  end

R
randx 已提交
97
  def bulk_update
98
    result = Issues::BulkUpdateService.new(project, current_user, params).execute
99
    redirect_to :back, notice: "#{result[:count]} issues updated"
R
randx 已提交
100 101
  end

N
Nihad Abbasov 已提交
102
  protected
G
gitlabhq 已提交
103 104

  def issue
105
    @issue ||= begin
S
skv 已提交
106
                 @project.issues.find_by!(iid: params[:id])
107 108 109
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
110
  end
D
Dmitriy Zaporozhets 已提交
111 112

  def authorize_modify_issue!
113
    return render_404 unless can?(current_user, :modify_issue, @issue)
D
Dmitriy Zaporozhets 已提交
114 115
  end

D
Dmitriy Zaporozhets 已提交
116 117
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
118
  end
119 120 121 122

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
123

124 125 126 127 128 129
  # 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 已提交
130
    issue = @project.issues.find_by(id: params[:id])
131 132 133 134 135 136 137 138

    if issue
      redirect_to project_issue_path(@project, issue)
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
139 140 141 142

  def issue_params
    params.require(:issue).permit(
      :title, :assignee_id, :position, :description,
143
      :milestone_id, :state_event, :task_num, label_ids: []
144 145
    )
  end
G
gitlabhq 已提交
146
end