BigW Consortium Gitlab

labels_controller.rb 3.18 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 committed
10 11 12 13

  respond_to :js, :html

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

    respond_to do |format|
      format.html
      format.json do
Phil Hughes committed
20
        render json: @project.labels
21 22
      end
    end
randx committed
23 24
  end

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

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

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

  def edit
  end

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

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

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

Dmitriy Zaporozhets committed
69 70 71
  def destroy
    @label.destroy

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

81
  def remove_priority
82
    respond_to do |format|
83
      if label.update_attribute(:priority, nil)
84 85 86 87 88 89 90 91
        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

92
  def set_priorities
93 94 95 96 97 98 99
    Label.transaction do
      params[:label_ids].each_with_index do |label_id, index|
        label = @project.labels.find_by_id(label_id)
        label.update_attribute(:priority, index) if label
      end
    end

100
    respond_to do |format|
101
      format.json { render json: { message: 'success' } }
102 103 104
    end
  end

randx committed
105 106 107
  protected

  def module_enabled
108
    unless @project.feature_available?(:issues, current_user) || @project.feature_available?(:merge_requests, current_user)
109 110
      return render_404
    end
randx committed
111
  end
112 113

  def label_params
114
    params.require(:label).permit(:title, :description, :color)
115 116 117
  end

  def label
118
    @label ||= @project.labels.find(params[:id])
119
  end
120
  alias_method :subscribable_resource, :label
121 122 123 124

  def authorize_admin_labels!
    return render_404 unless can?(current_user, :admin_label, @project)
  end
randx committed
125
end