2014-09-02 18:07:02 +05:30
|
|
|
class Projects::LabelsController < Projects::ApplicationController
|
2016-06-02 11:05:42 +05:30
|
|
|
include ToggleSubscriptionAction
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :module_enabled
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :label, only: [:edit, :update, :destroy, :promote]
|
|
|
|
before_action :find_labels, only: [:index, :set_priorities, :remove_priority, :toggle_subscription]
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :authorize_read_label!
|
2016-11-03 12:29:30 +05:30
|
|
|
before_action :authorize_admin_labels!, only: [:new, :create, :edit, :update,
|
|
|
|
:generate, :destroy, :remove_priority,
|
|
|
|
:set_priorities]
|
2017-08-17 22:00:37 +05:30
|
|
|
before_action :authorize_admin_group!, only: [:promote]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
respond_to :js, :html
|
|
|
|
|
|
|
|
def index
|
2016-11-03 12:29:30 +05:30
|
|
|
@prioritized_labels = @available_labels.prioritized(@project)
|
|
|
|
@labels = @available_labels.unprioritized(@project).page(params[:page])
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
2017-08-17 22:00:37 +05:30
|
|
|
render json: LabelSerializer.new.represent_appearance(@available_labels)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@label = @project.labels.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-08-17 22:00:37 +05:30
|
|
|
@label = Labels::CreateService.new(label_params).execute(project: @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
if @label.valid?
|
2016-10-01 15:18:49 +05:30
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to namespace_project_labels_path(@project.namespace, @project) }
|
|
|
|
format.json { render json: @label }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2016-10-01 15:18:49 +05:30
|
|
|
respond_to do |format|
|
2016-11-03 12:29:30 +05:30
|
|
|
format.html { render :new }
|
2016-10-01 15:18:49 +05:30
|
|
|
format.json { render json: { message: @label.errors.messages }, status: 400 }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-08-17 22:00:37 +05:30
|
|
|
@label = Labels::UpdateService.new(label_params).execute(@label)
|
|
|
|
|
|
|
|
if @label.valid?
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to namespace_project_labels_path(@project.namespace, @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2016-11-03 12:29:30 +05:30
|
|
|
render :edit
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate
|
|
|
|
Gitlab::IssuesLabels.generate(@project)
|
|
|
|
|
|
|
|
if params[:redirect] == 'issues'
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to namespace_project_issues_path(@project.namespace, @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
elsif params[:redirect] == 'merge_requests'
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to namespace_project_merge_requests_path(@project.namespace,
|
|
|
|
@project)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2015-04-26 12:48:37 +05:30
|
|
|
redirect_to namespace_project_labels_path(@project.namespace, @project)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@label.destroy
|
2016-11-03 12:29:30 +05:30
|
|
|
@labels = find_labels
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
redirect_to(namespace_project_labels_path(@project.namespace, @project), notice: 'Label was removed')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def remove_priority
|
|
|
|
respond_to do |format|
|
2016-11-03 12:29:30 +05:30
|
|
|
label = @available_labels.find(params[:id])
|
|
|
|
|
|
|
|
if label.unprioritize!(project)
|
2016-06-16 23:09:34 +05:30
|
|
|
format.json { render json: label }
|
|
|
|
else
|
2016-11-03 12:29:30 +05:30
|
|
|
format.json { head :unprocessable_entity }
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_priorities
|
|
|
|
Label.transaction do
|
2016-11-03 12:29:30 +05:30
|
|
|
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) }
|
|
|
|
|
|
|
|
label_ids.each_with_index do |label_id, index|
|
|
|
|
label = @available_labels.find(label_id)
|
|
|
|
label.prioritize!(project, index)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: { message: 'success' } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
protected
|
|
|
|
|
|
|
|
def module_enabled
|
2016-09-29 09:46:39 +05:30
|
|
|
unless @project.feature_available?(:issues, current_user) || @project.feature_available?(:merge_requests, current_user)
|
2014-09-02 18:07:02 +05:30
|
|
|
return render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def label_params
|
2016-04-02 18:10:28 +05:30
|
|
|
params.require(:label).permit(:title, :description, :color)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def label
|
2016-06-02 11:05:42 +05:30
|
|
|
@label ||= @project.labels.find(params[:id])
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def subscribable_resource
|
|
|
|
@available_labels.find(params[:id])
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def find_labels
|
2016-11-24 13:41:30 +05:30
|
|
|
@available_labels ||= LabelsFinder.new(current_user, project_id: @project.id).execute
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def authorize_admin_labels!
|
|
|
|
return render_404 unless can?(current_user, :admin_label, @project)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def authorize_admin_group!
|
|
|
|
return render_404 unless can?(current_user, :admin_group, @project.group)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|