issues_controller.rb 3.5 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
    @notes = @issue.notes.inc_author.fresh
53
    @noteable = @issue
G
gitlabhq 已提交
54

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

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

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

  def update
76
    @issue.update_attributes(params[:issue].merge(author_id_of_changes: current_user.id))
D
Drew Blessing 已提交
77
    @issue.reset_events_cache
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
G
gitlabhq 已提交
88 89 90
    end
  end

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

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

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

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

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

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

  def issues_filtered
119 120 121 122
    params[:scope] = 'all' if params[:scope].blank?
    params[:state] = 'opened' if params[:state].blank?
    params[:project_id] = @project.id
    @issues = FilteringService.new.execute(Issue, current_user, params)
R
randx 已提交
123
  end
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  # 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 已提交
140
end