debian-mirror-gitlab/app/controllers/projects/milestones_controller.rb

150 lines
4.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class Projects::MilestonesController < Projects::ApplicationController
2018-11-08 19:23:39 +05:30
include Gitlab::Utils::StrongMemoize
2017-08-17 22:00:37 +05:30
include MilestoneActions
2017-09-10 17:25:29 +05:30
before_action :check_issuables_available!
2018-03-17 18:26:18 +05:30
before_action :milestone, only: [:edit, :update, :destroy, :show, :merge_requests, :participants, :labels, :promote]
2014-09-02 18:07:02 +05:30
# Allow read any milestone
2015-09-11 14:41:01 +05:30
before_action :authorize_read_milestone!
2014-09-02 18:07:02 +05:30
# Allow admin milestone
2018-11-29 20:51:05 +05:30
before_action :authorize_admin_milestone!, except: [:index, :show, :merge_requests, :participants, :labels]
# Allow to promote milestone
before_action :authorize_promote_milestone!, only: :promote
2014-09-02 18:07:02 +05:30
respond_to :html
def index
2017-08-17 22:00:37 +05:30
@sort = params[:sort] || 'due_date_asc'
2018-05-09 12:01:36 +05:30
@milestones = milestones.sort_by_attribute(@sort)
2017-08-17 22:00:37 +05:30
2016-06-02 11:05:42 +05:30
respond_to do |format|
format.html do
2017-09-10 17:25:29 +05:30
# We need to show group milestones in the JSON response
# so that people can filter by and assign group milestones,
# but we don't need to show them on the project milestones page itself.
@milestones = @milestones.for_projects
2016-06-02 11:05:42 +05:30
@milestones = @milestones.page(params[:page])
end
format.json do
render json: @milestones.to_json(methods: :name)
end
end
2014-09-02 18:07:02 +05:30
end
def new
@milestone = @project.milestones.new
respond_with(@milestone)
end
def edit
respond_with(@milestone)
end
def show
2018-05-09 12:01:36 +05:30
respond_to do |format|
format.html
end
2014-09-02 18:07:02 +05:30
end
def create
@milestone = Milestones::CreateService.new(project, current_user, milestone_params).execute
2017-09-10 17:25:29 +05:30
if @milestone.valid?
redirect_to project_milestone_path(@project, @milestone)
2014-09-02 18:07:02 +05:30
else
render "new"
end
end
def update
@milestone = Milestones::UpdateService.new(project, current_user, milestone_params).execute(milestone)
respond_to do |format|
format.js
format.html do
if @milestone.valid?
2017-09-10 17:25:29 +05:30
redirect_to project_milestone_path(@project, @milestone)
2014-09-02 18:07:02 +05:30
else
render :edit
end
end
end
end
2018-03-17 18:26:18 +05:30
def promote
2018-05-01 15:08:00 +05:30
promoted_milestone = Milestones::PromoteService.new(project, current_user).execute(milestone)
2018-11-29 20:51:05 +05:30
flash[:notice] = flash_notice_for(promoted_milestone, project_group)
2018-03-27 19:54:05 +05:30
respond_to do |format|
format.html do
redirect_to project_milestones_path(project)
end
format.json do
render json: { url: project_milestones_path(project) }
end
end
2018-03-17 18:26:18 +05:30
rescue Milestones::PromoteService::PromoteMilestoneError => error
redirect_to milestone, alert: error.message
end
2018-10-15 14:42:47 +05:30
def flash_notice_for(milestone, group)
2018-12-05 23:21:45 +05:30
''.html_safe + "#{milestone.title} promoted to " + view_context.link_to('<u>group milestone</u>'.html_safe, group_milestone_path(group, milestone.iid)) + '.'
2018-10-15 14:42:47 +05:30
end
2014-09-02 18:07:02 +05:30
def destroy
2015-09-11 14:41:01 +05:30
return access_denied! unless can?(current_user, :admin_milestone, @project)
2015-09-25 12:07:36 +05:30
Milestones::DestroyService.new(project, current_user).execute(milestone)
2014-09-02 18:07:02 +05:30
respond_to do |format|
2018-11-18 11:00:15 +05:30
format.html { redirect_to namespace_project_milestones_path, status: :see_other }
2016-06-02 11:05:42 +05:30
format.js { head :ok }
2014-09-02 18:07:02 +05:30
end
end
2017-09-10 17:25:29 +05:30
protected
2014-09-02 18:07:02 +05:30
2018-11-29 20:51:05 +05:30
def project_group
strong_memoize(:project_group) do
project.group
end
end
2017-09-10 17:25:29 +05:30
def milestones
2018-11-08 19:23:39 +05:30
strong_memoize(:milestones) do
2017-09-10 17:25:29 +05:30
MilestonesFinder.new(search_params).execute
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def milestone
@milestone ||= @project.milestones.find_by!(iid: params[:id])
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def authorize_admin_milestone!
return render_404 unless can?(current_user, :admin_milestone, @project)
end
2018-11-29 20:51:05 +05:30
def authorize_promote_milestone!
return render_404 unless can?(current_user, :admin_milestone, project_group)
end
2014-09-02 18:07:02 +05:30
def milestone_params
2017-08-17 22:00:37 +05:30
params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
2014-09-02 18:07:02 +05:30
end
2018-03-17 18:26:18 +05:30
def search_params
2018-11-29 20:51:05 +05:30
if request.format.json? && project_group && can?(current_user, :read_group, project_group)
2019-03-02 22:35:43 +05:30
groups = project_group.self_and_ancestors.select(:id)
2018-03-17 18:26:18 +05:30
end
2019-03-02 22:35:43 +05:30
params.permit(:state, :search_title).merge(project_ids: @project.id, group_ids: groups)
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end