labels_controller.rb 3.66 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 :find_labels, only: [:index, :set_priorities, :remove_priority]
7
  before_action :authorize_read_label!
8 9 10
  before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update,
                                                 :generate, :destroy, :remove_priority,
                                                 :set_priorities]
randx's avatar
randx committed
11 12 13 14

  respond_to :js, :html

  def index
15 16
    @prioritized_labels = @available_labels.prioritized(@project)
    @labels = @available_labels.unprioritized(@project).page(params[:page])
17 18 19 20

    respond_to do |format|
      format.html
      format.json do
21
        render json: @available_labels.as_json(only: [:id, :title, :color])
22 23
      end
    end
randx's avatar
randx committed
24 25
  end

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

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

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

  def edit
  end

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

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

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
70 71
  def destroy
    @label.destroy
72
    @labels = find_labels
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
73

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

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

87
      if label.unprioritize!(project)
88 89
        format.json { render json: label }
      else
90
        format.json { head :unprocessable_entity }
91 92 93 94
      end
    end
  end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
95
  def set_priorities
96
    Label.transaction do
97 98
      available_labels_ids = @available_labels.where(id: params[:label_ids]).pluck(:id)
      label_ids = params[:label_ids].select { |id| available_labels_ids.include?(id.to_i) }
99

100 101 102
      label_ids.each_with_index do |label_id, index|
        label = @available_labels.find(label_id)
        label.prioritize!(project, index)
103 104 105
      end
    end

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

randx's avatar
randx committed
111 112 113
  protected

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

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

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

128
  def find_labels
129
    @available_labels ||= LabelsFinder.new(current_user, project_id: @project.id).execute.includes(:priorities)
130 131
  end

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