2013-06-23 16:47:22 +00:00
|
|
|
class Projects::LabelsController < Projects::ApplicationController
|
2016-03-15 16:18:47 +00:00
|
|
|
include ToggleSubscriptionAction
|
|
|
|
|
2015-04-16 12:03:37 +00:00
|
|
|
before_action :module_enabled
|
2016-03-15 16:18:47 +00:00
|
|
|
before_action :label, only: [:edit, :update, :destroy]
|
2016-09-29 04:03:28 +00:00
|
|
|
before_action :find_labels, only: [:index, :set_priorities, :remove_priority]
|
2015-06-17 14:33:51 +00:00
|
|
|
before_action :authorize_read_label!
|
2016-09-20 14:03:41 +00:00
|
|
|
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update,
|
|
|
|
:generate, :destroy, :remove_priority,
|
|
|
|
:set_priorities]
|
2012-08-24 10:05:40 +00:00
|
|
|
|
|
|
|
respond_to :js, :html
|
|
|
|
|
|
|
|
def index
|
2016-10-14 21:32:44 +00:00
|
|
|
@prioritized_labels = @available_labels.prioritized(@project)
|
|
|
|
@labels = @available_labels.unprioritized(@project).page(params[:page])
|
2016-03-16 18:08:35 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2016-10-11 19:33:55 +00:00
|
|
|
render json: @available_labels.as_json(only: [:id, :title, :color])
|
2016-03-16 18:08:35 +00:00
|
|
|
end
|
|
|
|
end
|
2012-08-24 10:05:40 +00:00
|
|
|
end
|
|
|
|
|
2014-07-30 10:12:11 +00:00
|
|
|
def new
|
|
|
|
@label = @project.labels.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@label = @project.labels.create(label_params)
|
|
|
|
|
|
|
|
if @label.valid?
|
2016-09-22 16:07:57 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to namespace_project_labels_path(@project.namespace, @project) }
|
|
|
|
format.json { render json: @label }
|
|
|
|
end
|
2014-07-30 10:12:11 +00:00
|
|
|
else
|
2016-09-22 16:07:57 +00:00
|
|
|
respond_to do |format|
|
2016-09-20 14:03:41 +00:00
|
|
|
format.html { render :new }
|
2016-09-22 16:07:57 +00:00
|
|
|
format.json { render json: { message: @label.errors.messages }, status: 400 }
|
|
|
|
end
|
2014-07-30 10:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @label.update_attributes(label_params)
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to namespace_project_labels_path(@project.namespace, @project)
|
2014-07-30 10:12:11 +00:00
|
|
|
else
|
2016-09-20 14:03:41 +00:00
|
|
|
render :edit
|
2014-07-30 10:12:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-07 14:57:29 +00:00
|
|
|
def generate
|
2013-05-07 16:26:41 +00:00
|
|
|
Gitlab::IssuesLabels.generate(@project)
|
2013-05-07 14:57:29 +00:00
|
|
|
|
2014-04-15 01:12:07 +00:00
|
|
|
if params[:redirect] == 'issues'
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to namespace_project_issues_path(@project.namespace, @project)
|
2014-04-15 01:12:07 +00:00
|
|
|
elsif params[:redirect] == 'merge_requests'
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to namespace_project_merge_requests_path(@project.namespace,
|
|
|
|
@project)
|
2014-06-04 08:08:21 +00:00
|
|
|
else
|
2015-01-24 18:02:58 +00:00
|
|
|
redirect_to namespace_project_labels_path(@project.namespace, @project)
|
2014-04-15 01:12:07 +00:00
|
|
|
end
|
2013-05-07 14:57:29 +00:00
|
|
|
end
|
|
|
|
|
2014-07-30 12:15:39 +00:00
|
|
|
def destroy
|
|
|
|
@label.destroy
|
2016-09-29 04:03:28 +00:00
|
|
|
@labels = find_labels
|
2014-07-30 12:15:39 +00:00
|
|
|
|
2014-08-13 16:47:03 +00:00
|
|
|
respond_to do |format|
|
2015-01-24 18:02:58 +00:00
|
|
|
format.html do
|
|
|
|
redirect_to(namespace_project_labels_path(@project.namespace, @project),
|
|
|
|
notice: 'Label was removed')
|
|
|
|
end
|
2014-09-01 09:09:07 +00:00
|
|
|
format.js
|
2014-08-13 16:47:03 +00:00
|
|
|
end
|
2014-07-30 12:15:39 +00:00
|
|
|
end
|
|
|
|
|
2016-05-13 15:26:18 +00:00
|
|
|
def remove_priority
|
2016-05-02 23:19:46 +00:00
|
|
|
respond_to do |format|
|
2016-09-29 04:03:28 +00:00
|
|
|
label = @available_labels.find(params[:id])
|
2016-09-20 03:09:57 +00:00
|
|
|
|
2016-10-14 21:32:44 +00:00
|
|
|
if label.priorities.where(project_id: project).delete_all
|
2016-05-02 23:19:46 +00:00
|
|
|
format.json { render json: label }
|
|
|
|
else
|
2016-10-14 21:32:44 +00:00
|
|
|
format.json { head :unprocessable_entity }
|
2016-05-02 23:19:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 20:21:15 +00:00
|
|
|
def set_priorities
|
2016-05-13 15:26:18 +00:00
|
|
|
Label.transaction do
|
2016-09-29 04:03:28 +00:00
|
|
|
label_ids = @available_labels.where(id: params[:label_ids]).pluck(:id)
|
|
|
|
|
2016-05-13 15:26:18 +00:00
|
|
|
params[:label_ids].each_with_index do |label_id, index|
|
2016-09-29 04:03:28 +00:00
|
|
|
next unless label_ids.include?(label_id.to_i)
|
2016-09-20 15:00:18 +00:00
|
|
|
|
2016-10-14 21:32:44 +00:00
|
|
|
label_priority = LabelPriority.find_or_initialize_by(project_id: @project.id, label_id: label_id)
|
|
|
|
label_priority.priority = index
|
|
|
|
label_priority.save!
|
2016-05-13 15:26:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-02 23:19:46 +00:00
|
|
|
respond_to do |format|
|
2016-05-13 15:26:18 +00:00
|
|
|
format.json { render json: { message: 'success' } }
|
2016-05-02 23:19:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-24 10:05:40 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def module_enabled
|
2016-08-01 22:31:21 +00:00
|
|
|
unless @project.feature_available?(:issues, current_user) || @project.feature_available?(:merge_requests, current_user)
|
2014-04-15 01:12:07 +00:00
|
|
|
return render_404
|
|
|
|
end
|
2012-08-24 10:05:40 +00:00
|
|
|
end
|
2014-07-30 10:12:11 +00:00
|
|
|
|
|
|
|
def label_params
|
2016-06-02 21:33:53 +00:00
|
|
|
params.require(:label).permit(:title, :description, :color)
|
2014-07-30 10:12:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def label
|
2016-03-15 16:18:47 +00:00
|
|
|
@label ||= @project.labels.find(params[:id])
|
2014-07-30 10:12:11 +00:00
|
|
|
end
|
2016-03-15 16:18:47 +00:00
|
|
|
alias_method :subscribable_resource, :label
|
2014-07-30 10:12:11 +00:00
|
|
|
|
2016-09-29 04:03:28 +00:00
|
|
|
def find_labels
|
|
|
|
@available_labels ||= LabelsFinder.new(current_user, project_id: @project.id).execute
|
2016-09-20 14:03:41 +00:00
|
|
|
end
|
|
|
|
|
2014-07-30 10:12:11 +00:00
|
|
|
def authorize_admin_labels!
|
|
|
|
return render_404 unless can?(current_user, :admin_label, @project)
|
|
|
|
end
|
2012-08-24 10:05:40 +00:00
|
|
|
end
|