issues_controller.rb 3.46 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2
  before_filter :module_enabled
3
  before_filter :issue, only: [:edit, :update, :show]
randx's avatar
randx committed
4

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5
  # Allow read any issue
gitlabhq's avatar
gitlabhq committed
6
  before_filter :authorize_read_issue!
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7
8

  # Allow write(create) issue
9
  before_filter :authorize_write_issue!, only: [:new, :create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10
11

  # Allow modify issue
12
  before_filter :authorize_modify_issue!, only: [:edit, :update]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13

14
  respond_to :html
gitlabhq's avatar
gitlabhq committed
15
16

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

19
    @issues = issues_filtered
20
    @issues = @issues.where("title LIKE ?", "%#{terms}%") if terms.present?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
21
    @issues = @issues.page(params[:page]).per(20)
gitlabhq's avatar
gitlabhq committed
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

gitlabhq's avatar
gitlabhq committed
30
    respond_to do |format|
31
      format.html
32
      format.atom { render layout: false }
33
34
35
36
37
      format.json do
        render json: {
          html: view_to_html_string("projects/issues/_issues")
        }
      end
gitlabhq's avatar
gitlabhq committed
38
39
40
41
    end
  end

  def new
Alex Denisov's avatar
Alex Denisov committed
42
    @issue = @project.issues.new(params[:issue])
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
54

55
    respond_with(@issue)
gitlabhq's avatar
gitlabhq committed
56
57
58
59
60
  end

  def create
    @issue = @project.issues.new(params[:issue])
    @issue.author = current_user
Valery Sizov's avatar
Valery Sizov committed
61
    @issue.save
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
73
74
75
  end

  def update
76
    @issue.update_attributes(params[:issue].merge(author_id_of_changes: current_user.id))
Drew Blessing's avatar
Drew Blessing committed
77
    @issue.reset_events_cache
gitlabhq's avatar
gitlabhq committed
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
gitlabhq's avatar
gitlabhq committed
88
89
90
    end
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
96
  protected
gitlabhq's avatar
gitlabhq committed
97
98

  def issue
99
    @issue ||= begin
skv's avatar
skv committed
100
                 @project.issues.find_by!(iid: params[:id])
101
102
103
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
gitlabhq's avatar
gitlabhq committed
104
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
105
106

  def authorize_modify_issue!
107
    return render_404 unless can?(current_user, :modify_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
108
109
110
  end

  def authorize_admin_issue!
111
    return render_404 unless can?(current_user, :admin_issue, @issue)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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)
randx's avatar
randx committed
123
  end
124
125
126
127
128
129
130

  # 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
skv's avatar
skv committed
131
    issue = @project.issues.find_by(id: params[:id])
132
133
134
135
136
137
138
139

    if issue
      redirect_to project_issue_path(@project, issue)
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
gitlabhq's avatar
gitlabhq committed
140
end