issues_controller.rb 5.5 KB
Newer Older
1
class Projects::IssuesController < Projects::ApplicationController
2
  include NotesHelper
3
  include ToggleSubscriptionAction
4
  include IssuableActions
5
  include ToggleAwardEmoji
6
  include IssuableCollections
7
  include SpammableActions
8

9
  before_action :redirect_to_external_issue_tracker, only: [:index, :new]
10
  before_action :module_enabled
11 12
  before_action :issue, only: [:edit, :update, :show, :referenced_merge_requests,
                               :related_branches, :can_create_branch]
R
randx 已提交
13

D
Dmitriy Zaporozhets 已提交
14
  # Allow read any issue
15
  before_action :authorize_read_issue!, only: [:show]
D
Dmitriy Zaporozhets 已提交
16 17

  # Allow write(create) issue
D
Dmitriy Zaporozhets 已提交
18
  before_action :authorize_create_issue!, only: [:new, :create]
D
Dmitriy Zaporozhets 已提交
19 20

  # Allow modify issue
D
Dmitriy Zaporozhets 已提交
21
  before_action :authorize_update_issue!, only: [:edit, :update]
D
Dmitriy Zaporozhets 已提交
22

D
Dmitriy Zaporozhets 已提交
23
  respond_to :html
G
gitlabhq 已提交
24 25

  def index
26
    @issues = issues_collection
27
    @issues = @issues.page(params[:page])
B
barthc 已提交
28

29
    @labels = @project.labels.where(title: params[:label_name])
G
gitlabhq 已提交
30

G
gitlabhq 已提交
31
    respond_to do |format|
D
Dmitriy Zaporozhets 已提交
32
      format.html
33
      format.atom { render layout: false }
D
Dmitriy Zaporozhets 已提交
34 35
      format.json do
        render json: {
36
          html: view_to_html_string("projects/issues/_issues"),
P
Phil Hughes 已提交
37
          labels: @labels.as_json(methods: :text_color)
D
Dmitriy Zaporozhets 已提交
38 39
        }
      end
G
gitlabhq 已提交
40 41 42 43
    end
  end

  def new
44 45 46 47
    params[:issue] ||= ActionController::Parameters.new(
      assignee_id: ""
    )

48
    @issue = @noteable = @project.issues.new(issue_params)
G
gitlabhq 已提交
49 50 51 52 53 54 55 56
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
57
    raw_notes = @issue.notes.inc_relations_for_view.fresh
58 59 60 61

    @notes = Banzai::NoteRenderer.
      render(raw_notes, @project, current_user, @path, @project_wiki, @ref)

62
    @note     = @project.notes.new(noteable: @issue)
63
    @noteable = @issue
G
gitlabhq 已提交
64

S
Stan Hu 已提交
65
    preload_max_access_for_authors(@notes, @project)
66

67 68 69 70 71 72
    respond_to do |format|
      format.html
      format.json do
        render json: @issue.to_json(include: [:milestone, :labels])
      end
    end
G
gitlabhq 已提交
73 74 75
  end

  def create
76
    @issue = Issues::CreateService.new(project, current_user, issue_params.merge(request: request)).execute
G
gitlabhq 已提交
77

78
    respond_to do |format|
79
      format.html do
80
        if @issue.valid?
81
          redirect_to issue_path(@issue)
82 83 84 85
        else
          render :new
        end
      end
86
      format.js do
87 88
        @link = @issue.attachment.url.to_js
      end
89
    end
G
gitlabhq 已提交
90 91 92
  end

  def update
93
    @issue = Issues::UpdateService.new(project, current_user, issue_params).execute(issue)
G
gitlabhq 已提交
94

95 96
    if params[:move_to_project_id].to_i > 0
      new_project = Project.find(params[:move_to_project_id])
97 98
      return render_404 unless issue.can_move?(current_user, new_project)

99
      move_service = Issues::MoveService.new(project, current_user)
100
      @issue = move_service.execute(@issue, new_project)
101
    end
G
gitlabhq 已提交
102 103

    respond_to do |format|
104
      format.html do
105
        if @issue.valid?
106
          redirect_to issue_path(@issue)
107 108 109 110
        else
          render :edit
        end
      end
111

112
      format.json do
113
        render json: @issue.to_json(include: { milestone: {}, assignee: { methods: :avatar_url }, labels: { methods: :text_color } })
114
      end
G
gitlabhq 已提交
115
    end
116 117 118 119

  rescue ActiveRecord::StaleObjectError
    @conflict = true
    render :edit
G
gitlabhq 已提交
120 121
  end

122 123 124 125 126 127 128 129 130 131 132 133 134 135
  def referenced_merge_requests
    @merge_requests = @issue.referenced_merge_requests(current_user)
    @closed_by_merge_requests = @issue.closed_by_merge_requests(current_user)

    respond_to do |format|
      format.json do
        render json: {
          html: view_to_html_string('projects/issues/_merge_requests')
        }
      end
    end
  end

  def related_branches
136
    @related_branches = @issue.related_branches(current_user)
137 138 139 140 141 142 143 144 145 146

    respond_to do |format|
      format.json do
        render json: {
          html: view_to_html_string('projects/issues/_related_branches')
        }
      end
    end
  end

147 148 149 150 151 152 153 154 155 156 157 158
  def can_create_branch
    can_create = current_user &&
      can?(current_user, :push_code, @project) &&
      @issue.can_be_worked_on?(current_user)

    respond_to do |format|
      format.json do
        render json: { can_create_branch: can_create }
      end
    end
  end

N
Nihad Abbasov 已提交
159
  protected
G
gitlabhq 已提交
160 161

  def issue
162
    @noteable = @issue ||= @project.issues.find_by(iid: params[:id]) || redirect_old
G
gitlabhq 已提交
163
  end
164
  alias_method :subscribable_resource, :issue
165
  alias_method :issuable, :issue
166
  alias_method :awardable, :issue
167
  alias_method :spammable, :issue
D
Dmitriy Zaporozhets 已提交
168

169 170 171 172
  def authorize_read_issue!
    return render_404 unless can?(current_user, :read_issue, @issue)
  end

D
Dmitriy Zaporozhets 已提交
173
  def authorize_update_issue!
174
    return render_404 unless can?(current_user, :update_issue, @issue)
D
Dmitriy Zaporozhets 已提交
175 176
  end

D
Dmitriy Zaporozhets 已提交
177 178
  def authorize_admin_issues!
    return render_404 unless can?(current_user, :admin_issue, @project)
D
Dmitriy Zaporozhets 已提交
179
  end
180 181

  def module_enabled
F
Felipe Artur 已提交
182
    return render_404 unless @project.feature_available?(:issues, current_user) && @project.default_issues_tracker?
183
  end
184

185
  def redirect_to_external_issue_tracker
186
    external = @project.external_issue_tracker
187

188 189 190 191 192
    return unless external

    if action_name == 'new'
      redirect_to external.new_issue_path
    else
193
      redirect_to external.project_path
194
    end
195 196
  end

197 198 199 200 201 202
  # 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 已提交
203
    issue = @project.issues.find_by(id: params[:id])
204 205

    if issue
206
      redirect_to issue_path(issue)
207 208 209 210
    else
      raise ActiveRecord::RecordNotFound.new
    end
  end
211 212

  def issue_params
213
    params.require(:issue).permit(
214
      :title, :assignee_id, :position, :description, :confidential,
215
      :milestone_id, :due_date, :state_event, :task_num, :lock_version, label_ids: []
216 217
    )
  end
G
gitlabhq 已提交
218
end