labels_controller.rb 4.69 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, :promote]
6
  before_action :find_labels, only: [:index, :set_priorities, :remove_priority, :toggle_subscription]
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]
11
  before_action :authorize_admin_group!, only: [:promote]
randx's avatar
randx committed
12 13 14 15

  respond_to :js, :html

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

    respond_to do |format|
      format.html
      format.json do
22
        render json: LabelSerializer.new.represent_appearance(@available_labels)
23 24
      end
    end
randx's avatar
randx committed
25 26
  end

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

  def create
32
    @label = Labels::CreateService.new(label_params).execute(project: @project)
33 34

    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
      respond_to do |format|
41
        format.html { render :new }
42 43
        format.json { render json: { message: @label.errors.messages }, status: 400 }
      end
44 45 46 47 48 49 50
    end
  end

  def edit
  end

  def update
51 52 53
    @label = Labels::UpdateService.new(label_params).execute(@label)

    if @label.valid?
Vinnie Okada's avatar
Vinnie Okada committed
54
      redirect_to namespace_project_labels_path(@project.namespace, @project)
55
    else
56
      render :edit
57 58 59
    end
  end

60
  def generate
61
    Gitlab::IssuesLabels.generate(@project)
62

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
73 74
  def destroy
    @label.destroy
75
    @labels = find_labels
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
76

77
    redirect_to(namespace_project_labels_path(@project.namespace, @project), notice: 'Label was removed')
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
78 79
  end

80
  def remove_priority
81
    respond_to do |format|
82
      label = @available_labels.find(params[:id])
83

84
      if label.unprioritize!(project)
85 86
        format.json { render json: label }
      else
87
        format.json { head :unprocessable_entity }
88 89 90 91
      end
    end
  end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
92
  def set_priorities
93
    Label.transaction do
94 95
      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) }
96

97 98 99
      label_ids.each_with_index do |label_id, index|
        label = @available_labels.find(label_id)
        label.prioritize!(project, index)
100 101 102
      end
    end

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

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  def promote
    promote_service = Labels::PromoteService.new(@project, @current_user)

    begin
      return render_404 unless promote_service.execute(@label)
      respond_to do |format|
        format.html do
          redirect_to(namespace_project_labels_path(@project.namespace, @project),
                      notice: 'Label was promoted to a Group Label')
        end
        format.js
      end
    rescue ActiveRecord::RecordInvalid => e
      Gitlab::AppLogger.error "Failed to promote label \"#{@label.title}\" to group label"
      Gitlab::AppLogger.error e

      respond_to do |format|
        format.html do
          redirect_to(namespace_project_labels_path(@project.namespace, @project),
                      notice: 'Failed to promote label due to internal error. Please contact administrators.')
        end
        format.js
      end
    end
  end

randx's avatar
randx committed
134 135 136
  protected

  def module_enabled
137
    unless @project.feature_available?(:issues, current_user) || @project.feature_available?(:merge_requests, current_user)
138 139
      return render_404
    end
randx's avatar
randx committed
140
  end
141 142

  def label_params
Alfredo Sumaran's avatar
Alfredo Sumaran committed
143
    params.require(:label).permit(:title, :description, :color)
144 145 146
  end

  def label
147
    @label ||= @project.labels.find(params[:id])
148
  end
149 150 151 152

  def subscribable_resource
    @available_labels.find(params[:id])
  end
153

154
  def find_labels
155
    @available_labels ||= LabelsFinder.new(current_user, project_id: @project.id).execute
156 157
  end

158 159 160
  def authorize_admin_labels!
    return render_404 unless can?(current_user, :admin_label, @project)
  end
161 162 163 164

  def authorize_admin_group!
    return render_404 unless can?(current_user, :admin_group, @project.group)
  end
randx's avatar
randx committed
165
end