labels_controller.rb 3.38 KB
Newer Older
1
class Projects::LabelsController < Projects::ApplicationController
2 3
  include ToggleSubscriptionAction

4
  before_action :module_enabled
5
  before_action :label, only: [:edit, :update, :destroy]
6
  before_action :authorize_read_label!
7
  before_action :authorize_admin_labels!, only: [
8
    :new, :create, :edit, :update, :generate, :destroy, :remove_priority, :set_priorities
9
  ]
randx's avatar
randx committed
10 11 12 13

  respond_to :js, :html

  def index
14 15 16
    @project_labels = project_labels
    @prioritized_labels = project_labels.prioritized
    @group_labels = @project.group.labels.unprioritized if @project.group.present?
Alfredo Sumaran's avatar
Alfredo Sumaran committed
17
    @labels = @project.labels.unprioritized.page(params[:page])
18 19 20 21

    respond_to do |format|
      format.html
      format.json do
22
        render json: @project_labels
23 24
      end
    end
randx's avatar
randx committed
25 26
  end

27 28 29 30 31 32 33 34
  def new
    @label = @project.labels.new
  end

  def create
    @label = @project.labels.create(label_params)

    if @label.valid?
35 36 37 38
      respond_to do |format|
        format.html { redirect_to namespace_project_labels_path(@project.namespace, @project) }
        format.json { render json: @label }
      end
39
    else
40 41 42 43
      respond_to do |format|
        format.html { render 'new' }
        format.json { render json: { message: @label.errors.messages }, status: 400 }
      end
44 45 46 47 48 49 50 51
    end
  end

  def edit
  end

  def update
    if @label.update_attributes(label_params)
Vinnie Okada's avatar
Vinnie Okada committed
52
      redirect_to namespace_project_labels_path(@project.namespace, @project)
53 54 55 56 57
    else
      render 'edit'
    end
  end

58
  def generate
59
    Gitlab::IssuesLabels.generate(@project)
60

61
    if params[:redirect] == 'issues'
Vinnie Okada's avatar
Vinnie Okada committed
62
      redirect_to namespace_project_issues_path(@project.namespace, @project)
63
    elsif params[:redirect] == 'merge_requests'
Vinnie Okada's avatar
Vinnie Okada committed
64 65
      redirect_to namespace_project_merge_requests_path(@project.namespace,
                                                        @project)
66
    else
Vinnie Okada's avatar
Vinnie Okada committed
67
      redirect_to namespace_project_labels_path(@project.namespace, @project)
68
    end
69 70
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
71 72
  def destroy
    @label.destroy
73
    @project_labels = project_labels
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74

75
    respond_to do |format|
Vinnie Okada's avatar
Vinnie Okada committed
76 77 78 79
      format.html do
        redirect_to(namespace_project_labels_path(@project.namespace, @project),
                    notice: 'Label was removed')
      end
80
      format.js
81
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82 83
  end

84
  def remove_priority
85
    respond_to do |format|
86 87
      label = project_labels.find(params[:id])

88
      if label.update_attribute(:priority, nil)
89 90 91 92 93 94 95 96
        format.json { render json: label }
      else
        message = label.errors.full_messages.uniq.join('. ')
        format.json { render json: { message: message }, status: :unprocessable_entity }
      end
    end
  end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
97
  def set_priorities
98 99
    Label.transaction do
      params[:label_ids].each_with_index do |label_id, index|
100
        label = project_labels.find_by_id(label_id)
101 102 103 104
        label.update_attribute(:priority, index) if label
      end
    end

105
    respond_to do |format|
106
      format.json { render json: { message: 'success' } }
107 108 109
    end
  end

randx's avatar
randx committed
110 111 112
  protected

  def module_enabled
113
    unless @project.feature_available?(:issues, current_user) || @project.feature_available?(:merge_requests, current_user)
114 115
      return render_404
    end
randx's avatar
randx committed
116
  end
117 118

  def label_params
Alfredo Sumaran's avatar
Alfredo Sumaran committed
119
    params.require(:label).permit(:title, :description, :color)
120 121 122
  end

  def label
123
    @label ||= @project.labels.find(params[:id])
124
  end
125
  alias_method :subscribable_resource, :label
126 127 128 129

  def authorize_admin_labels!
    return render_404 unless can?(current_user, :admin_label, @project)
  end
randx's avatar
randx committed
130
end