issues_controller.rb 3.6 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2
  before_filter :module_enabled
V
tests  
Valery Sizov 已提交
3
  before_filter :issue, only: [:edit, :update, :show, :toggle_subscription]
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?
23
    @issues = @issues.page(params[:page]).per(PER_PAGE)
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?
63
          redirect_to issue_path(@issue)
64 65 66 67
        else
          render :new
        end
      end
68 69 70
      format.js do |format|
        @link = @issue.attachment.url.to_js
      end
71
    end
G
gitlabhq 已提交
72 73 74
  end

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

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

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

V
tests  
Valery Sizov 已提交
100 101
  def toggle_subscription
    @issue.toggle_subscription(current_user)
V
Valery Sizov 已提交
102 103 104 105
    
    render nothing: true
  end

N
Nihad Abbasov 已提交
106
  protected
G
gitlabhq 已提交
107 108

  def issue
109
    @issue ||= begin
S
skv 已提交
110
                 @project.issues.find_by!(iid: params[:id])
111 112 113
               rescue ActiveRecord::RecordNotFound
                 redirect_old
               end
G
gitlabhq 已提交
114
  end
D
Dmitriy Zaporozhets 已提交
115 116

  def authorize_modify_issue!
117
    return render_404 unless can?(current_user, :modify_issue, @issue)
D
Dmitriy Zaporozhets 已提交
118 119
  end

D
Dmitriy Zaporozhets 已提交
120 121
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
122
  end
123 124 125 126

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
127

128 129 130 131 132 133
  # 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 已提交
134
    issue = @project.issues.find_by(id: params[:id])
135 136

    if issue
137
      redirect_to issue_path(issue)
138 139 140 141 142
      return
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
143 144 145 146

  def issue_params
    params.require(:issue).permit(
      :title, :assignee_id, :position, :description,
147
      :milestone_id, :state_event, :task_num, label_ids: []
148 149
    )
  end
150 151 152 153 154 155 156 157 158

  def bulk_update_params
    params.require(:update).permit(
      :issues_ids,
      :assignee_id,
      :milestone_id,
      :state_event
    )
  end
G
gitlabhq 已提交
159
end