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
12
  before_filter :authorize_modify_issue!, only: [:edit, :update]
D
Dmitriy Zaporozhets 已提交
13

D
Dmitriy Zaporozhets 已提交
14
  respond_to :html
G
gitlabhq 已提交
15 16

  def index
17 18
    terms = params['issue_search']

19
    @issues = issues_filtered
20
    @issues = @issues.where("title LIKE ?", "%#{terms}%") if terms.present?
D
Dmitriy Zaporozhets 已提交
21
    @issues = @issues.page(params[:page]).per(20)
G
gitlabhq 已提交
22

23
    assignee_id, milestone_id = params[:assignee_id], params[:milestone_id]
24
    @assignee = @project.team.find(assignee_id) if assignee_id.present? && !assignee_id.to_i.zero?
25
    @milestone = @project.milestones.find(milestone_id) if milestone_id.present? && !milestone_id.to_i.zero?
26 27 28
    sort_param = params[:sort] || 'newest'
    @sort = sort_param.humanize unless sort_param.empty?

29

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

  def new
A
Alex Denisov 已提交
42
    @issue = @project.issues.new(params[:issue])
G
gitlabhq 已提交
43 44 45 46 47 48 49 50
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
51
    @note = @project.notes.new(noteable: @issue)
52 53
    @notes = @issue.notes.inc_author.fresh
    @target_type = 'Issue'
R
Riyad Preukschas 已提交
54
    @target_id = @issue.id
G
gitlabhq 已提交
55

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

  def create
    @issue = @project.issues.new(params[:issue])
    @issue.author = current_user
V
Valery Sizov 已提交
62
    @issue.save
G
gitlabhq 已提交
63

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

  def update
77
    @issue.update_attributes(params[:issue].merge(author_id_of_changes: current_user.id))
D
Drew Blessing 已提交
78
    @issue.reset_events_cache
G
gitlabhq 已提交
79 80 81

    respond_to do |format|
      format.js
82
      format.html do
83 84 85 86 87 88
        if @issue.valid?
          redirect_to [@project, @issue]
        else
          render :edit
        end
      end
G
gitlabhq 已提交
89 90 91
    end
  end

R
randx 已提交
92
  def bulk_update
93
    result = Issues::BulkUpdateContext.new(project, current_user, params).execute
94
    redirect_to :back, notice: "#{result[:count]} issues updated"
R
randx 已提交
95 96
  end

N
Nihad Abbasov 已提交
97
  protected
G
gitlabhq 已提交
98 99

  def issue
100 101 102 103 104
    @issue ||= begin
                 @project.issues.find_by_iid!(params[:id])
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
105
  end
D
Dmitriy Zaporozhets 已提交
106 107

  def authorize_modify_issue!
108
    return render_404 unless can?(current_user, :modify_issue, @issue)
D
Dmitriy Zaporozhets 已提交
109 110 111
  end

  def authorize_admin_issue!
112
    return render_404 unless can?(current_user, :admin_issue, @issue)
D
Dmitriy Zaporozhets 已提交
113
  end
114 115 116 117

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
118 119

  def issues_filtered
120
    @issues = Issues::ListContext.new(project, current_user, params).execute
R
randx 已提交
121
  end
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

  # 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
    issue = @project.issues.find_by_id(params[:id])

    if issue
      redirect_to project_issue_path(@project, issue)
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
G
gitlabhq 已提交
138
end