issues_controller.rb 3.63 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

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

  # Allow write(create) issue
9
  before_filter :authorize_write_issue!, only: [:new, :create]
10 11

  # Allow modify issue
12 13 14 15
  before_filter :authorize_modify_issue!, only: [:edit, :update]

  # Allow issues bulk update
  before_filter :authorize_admin_issues!, only: [:bulk_update]
16

17
  respond_to :html
gitlabhq's avatar
gitlabhq committed
18 19

  def index
20 21
    terms = params['issue_search']

22
    @issues = issues_filtered
23
    @issues = @issues.where("title LIKE ?", "%#{terms}%") if terms.present?
24
    @issues = @issues.page(params[:page]).per(20)
gitlabhq's avatar
gitlabhq committed
25

26
    assignee_id, milestone_id = params[:assignee_id], params[:milestone_id]
27
    @assignee = @project.team.find(assignee_id) if assignee_id.present? && !assignee_id.to_i.zero?
28
    @milestone = @project.milestones.find(milestone_id) if milestone_id.present? && !milestone_id.to_i.zero?
29 30
    sort_param = params[:sort] || 'newest'
    @sort = sort_param.humanize unless sort_param.empty?
31
    @assignees = User.where(id: @project.issues.pluck(:assignee_id))
32

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

  def new
45
    @issue = @project.issues.new(params[:issue])
gitlabhq's avatar
gitlabhq committed
46 47 48 49 50 51 52 53
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
54
    @note = @project.notes.new(noteable: @issue)
55
    @notes = @issue.notes.inc_author.fresh
56
    @noteable = @issue
gitlabhq's avatar
gitlabhq committed
57

58
    respond_with(@issue)
gitlabhq's avatar
gitlabhq committed
59 60 61
  end

  def create
62
    @issue = Issues::CreateService.new(project, current_user, params[:issue]).execute
gitlabhq's avatar
gitlabhq committed
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 74
      format.js do |format|
        @link = @issue.attachment.url.to_js
      end
75
    end
gitlabhq's avatar
gitlabhq committed
76 77 78
  end

  def update
79
    @issue = Issues::UpdateService.new(project, current_user, params[:issue]).execute(issue)
gitlabhq's avatar
gitlabhq committed
80 81 82

    respond_to do |format|
      format.js
83
      format.html do
84 85 86 87 88 89
        if @issue.valid?
          redirect_to [@project, @issue]
        else
          render :edit
        end
      end
gitlabhq's avatar
gitlabhq committed
90 91 92
    end
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
98
  protected
gitlabhq's avatar
gitlabhq committed
99 100

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

  def authorize_modify_issue!
109
    return render_404 unless can?(current_user, :modify_issue, @issue)
110 111
  end

112 113
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
114
  end
115 116 117 118

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

  def issues_filtered
121 122
    params[:scope] = 'all' if params[:scope].blank?
    params[:state] = 'opened' if params[:state].blank?
123
    @issues = IssuesFinder.new.execute(current_user, params.merge(project_id: @project.id))
randx's avatar
randx committed
124
  end
125 126 127 128 129 130 131

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

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