issues_controller.rb 3.6 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']
D
Dmitriy Zaporozhets 已提交
21
    @issues = get_issues_collection
22
    @issues = @issues.full_search(terms) if terms.present?
D
Dmitriy Zaporozhets 已提交
23
    @issues = @issues.page(params[:page]).per(20)
G
gitlabhq 已提交
24

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

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

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

  def edit
    respond_with(@issue)
  end

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

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

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

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

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

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

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

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

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

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

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

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
122

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

    if issue
V
Vinnie Okada 已提交
132 133
      redirect_to namespace_project_issue_path(@project.namespace, @project,
                                               issue)
134 135 136 137 138
      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