2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-23 12:47:22 -04:00
|
|
|
class Projects::LabelsController < Projects::ApplicationController
|
2016-03-15 12:18:47 -04:00
|
|
|
include ToggleSubscriptionAction
|
|
|
|
|
2017-06-20 07:13:04 -04:00
|
|
|
before_action :check_issuables_available!
|
2016-11-02 07:58:59 -04:00
|
|
|
before_action :label, only: [:edit, :update, :destroy, :promote]
|
2016-11-01 13:02:58 -04:00
|
|
|
before_action :find_labels, only: [:index, :set_priorities, :remove_priority, :toggle_subscription]
|
2015-06-17 10:33:51 -04:00
|
|
|
before_action :authorize_read_label!
|
2016-09-20 10:03:41 -04:00
|
|
|
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update,
|
|
|
|
:generate, :destroy, :remove_priority,
|
|
|
|
:set_priorities]
|
2017-06-08 06:44:33 -04:00
|
|
|
before_action :authorize_admin_group_labels!, only: [:promote]
|
2012-08-24 06:05:40 -04:00
|
|
|
|
|
|
|
respond_to :js, :html
|
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :issue_tracking
|
|
|
|
|
2012-08-24 06:05:40 -04:00
|
|
|
def index
|
2016-03-16 14:08:35 -04:00
|
|
|
respond_to do |format|
|
2021-04-07 11:09:18 -04:00
|
|
|
format.html do
|
|
|
|
@prioritized_labels = @available_labels.prioritized(@project)
|
|
|
|
@labels = @available_labels.unprioritized(@project).page(params[:page])
|
|
|
|
# preload group, project, and subscription data
|
|
|
|
Preloaders::LabelsPreloader.new(@prioritized_labels, current_user, @project).preload_all
|
|
|
|
Preloaders::LabelsPreloader.new(@labels, current_user, @project).preload_all
|
|
|
|
end
|
2016-03-16 14:08:35 -04:00
|
|
|
format.json do
|
2017-05-05 18:47:32 -04:00
|
|
|
render json: LabelSerializer.new.represent_appearance(@available_labels)
|
2016-03-16 14:08:35 -04:00
|
|
|
end
|
|
|
|
end
|
2012-08-24 06:05:40 -04:00
|
|
|
end
|
|
|
|
|
2014-07-30 06:12:11 -04:00
|
|
|
def new
|
|
|
|
@label = @project.labels.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-03-29 07:45:15 -04:00
|
|
|
@label = Labels::CreateService.new(label_params).execute(project: @project)
|
2014-07-30 06:12:11 -04:00
|
|
|
|
|
|
|
if @label.valid?
|
2016-09-22 12:07:57 -04:00
|
|
|
respond_to do |format|
|
2017-06-29 13:06:35 -04:00
|
|
|
format.html { redirect_to project_labels_path(@project) }
|
2016-09-22 12:07:57 -04:00
|
|
|
format.json { render json: @label }
|
|
|
|
end
|
2014-07-30 06:12:11 -04:00
|
|
|
else
|
2016-09-22 12:07:57 -04:00
|
|
|
respond_to do |format|
|
2016-09-20 10:03:41 -04:00
|
|
|
format.html { render :new }
|
2018-07-02 06:43:06 -04:00
|
|
|
format.json { render json: { message: @label.errors.messages }, status: :bad_request }
|
2016-09-22 12:07:57 -04:00
|
|
|
end
|
2014-07-30 06:12:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-03-29 07:45:15 -04:00
|
|
|
@label = Labels::UpdateService.new(label_params).execute(@label)
|
|
|
|
|
|
|
|
if @label.valid?
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_labels_path(@project)
|
2014-07-30 06:12:11 -04:00
|
|
|
else
|
2016-09-20 10:03:41 -04:00
|
|
|
render :edit
|
2014-07-30 06:12:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-07 10:57:29 -04:00
|
|
|
def generate
|
2013-05-07 12:26:41 -04:00
|
|
|
Gitlab::IssuesLabels.generate(@project)
|
2013-05-07 10:57:29 -04:00
|
|
|
|
2014-04-14 21:12:07 -04:00
|
|
|
if params[:redirect] == 'issues'
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_issues_path(@project)
|
2014-04-14 21:12:07 -04:00
|
|
|
elsif params[:redirect] == 'merge_requests'
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_merge_requests_path(@project)
|
2014-06-04 04:08:21 -04:00
|
|
|
else
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_labels_path(@project)
|
2014-04-14 21:12:07 -04:00
|
|
|
end
|
2013-05-07 10:57:29 -04:00
|
|
|
end
|
|
|
|
|
2014-07-30 08:15:39 -04:00
|
|
|
def destroy
|
|
|
|
@label.destroy
|
2016-09-29 00:03:28 -04:00
|
|
|
@labels = find_labels
|
2014-07-30 08:15:39 -04:00
|
|
|
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to project_labels_path(@project),
|
2019-11-17 07:06:19 -05:00
|
|
|
status: :found,
|
2017-06-06 18:45:16 -04:00
|
|
|
notice: 'Label was removed'
|
2014-07-30 08:15:39 -04:00
|
|
|
end
|
|
|
|
|
2016-05-13 11:26:18 -04:00
|
|
|
def remove_priority
|
2016-05-02 19:19:46 -04:00
|
|
|
respond_to do |format|
|
2016-09-29 00:03:28 -04:00
|
|
|
label = @available_labels.find(params[:id])
|
2016-09-19 23:09:57 -04:00
|
|
|
|
2016-10-17 14:34:22 -04:00
|
|
|
if label.unprioritize!(project)
|
2016-05-02 19:19:46 -04:00
|
|
|
format.json { render json: label }
|
|
|
|
else
|
2016-10-14 17:32:44 -04:00
|
|
|
format.json { head :unprocessable_entity }
|
2016-05-02 19:19:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-06-02 16:21:15 -04:00
|
|
|
def set_priorities
|
2016-05-13 11:26:18 -04:00
|
|
|
Label.transaction do
|
2016-10-17 14:34:22 -04:00
|
|
|
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) }
|
2016-09-29 00:03:28 -04:00
|
|
|
|
2016-10-17 14:34:22 -04:00
|
|
|
label_ids.each_with_index do |label_id, index|
|
|
|
|
label = @available_labels.find(label_id)
|
|
|
|
label.prioritize!(project, index)
|
2016-05-13 11:26:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-02 19:19:46 -04:00
|
|
|
respond_to do |format|
|
2016-05-13 11:26:18 -04:00
|
|
|
format.json { render json: { message: 'success' } }
|
2016-05-02 19:19:46 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-05-02 19:19:46 -04:00
|
|
|
|
2016-11-02 07:58:59 -04:00
|
|
|
def promote
|
|
|
|
promote_service = Labels::PromoteService.new(@project, @current_user)
|
|
|
|
|
|
|
|
begin
|
|
|
|
return render_404 unless promote_service.execute(@label)
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-07-16 06:49:01 -04:00
|
|
|
flash[:notice] = flash_notice_for(@label, @project.group)
|
2016-11-02 07:58:59 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2018-07-02 06:43:06 -04:00
|
|
|
redirect_to(project_labels_path(@project), status: :see_other)
|
2018-03-01 17:09:25 -05:00
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: { url: project_labels_path(@project) }
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
|
|
|
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
|
2017-06-29 13:06:35 -04:00
|
|
|
redirect_to(project_labels_path(@project),
|
2019-03-27 12:52:52 -04:00
|
|
|
notice: _('Failed to promote label due to internal error. Please contact administrators.'))
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-16 06:49:01 -04:00
|
|
|
def flash_notice_for(label, group)
|
2018-09-25 23:45:43 -04:00
|
|
|
''.html_safe + "#{label.title} promoted to " + view_context.link_to('<u>group label</u>'.html_safe, group_labels_path(group)) + '.'
|
2018-07-16 06:49:01 -04:00
|
|
|
end
|
|
|
|
|
2012-08-24 06:05:40 -04:00
|
|
|
protected
|
|
|
|
|
2014-07-30 06:12:11 -04:00
|
|
|
def label_params
|
2016-06-02 17:33:53 -04:00
|
|
|
params.require(:label).permit(:title, :description, :color)
|
2014-07-30 06:12:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def label
|
2016-03-15 12:18:47 -04:00
|
|
|
@label ||= @project.labels.find(params[:id])
|
2014-07-30 06:12:11 -04:00
|
|
|
end
|
2016-11-01 13:02:58 -04:00
|
|
|
|
|
|
|
def subscribable_resource
|
|
|
|
@available_labels.find(params[:id])
|
|
|
|
end
|
2014-07-30 06:12:11 -04:00
|
|
|
|
2016-09-29 00:03:28 -04:00
|
|
|
def find_labels
|
2018-04-04 11:40:29 -04:00
|
|
|
@available_labels ||=
|
2018-07-20 10:19:58 -04:00
|
|
|
LabelsFinder.new(current_user,
|
|
|
|
project_id: @project.id,
|
2020-12-23 01:10:22 -05:00
|
|
|
include_ancestor_groups: true,
|
2018-09-10 07:38:29 -04:00
|
|
|
search: params[:search],
|
2018-09-27 10:19:28 -04:00
|
|
|
subscribed: params[:subscribed],
|
2018-09-10 07:38:29 -04:00
|
|
|
sort: sort).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort
|
|
|
|
@sort ||= params[:sort] || 'name_asc'
|
2016-09-20 10:03:41 -04:00
|
|
|
end
|
|
|
|
|
2014-07-30 06:12:11 -04:00
|
|
|
def authorize_admin_labels!
|
|
|
|
return render_404 unless can?(current_user, :admin_label, @project)
|
|
|
|
end
|
2016-11-02 07:58:59 -04:00
|
|
|
|
2017-06-08 06:44:33 -04:00
|
|
|
def authorize_admin_group_labels!
|
|
|
|
return render_404 unless can?(current_user, :admin_label, @project.group)
|
2016-11-02 07:58:59 -04:00
|
|
|
end
|
2012-08-24 06:05:40 -04:00
|
|
|
end
|