gitlab-org--gitlab-foss/app/controllers/projects/milestones_controller.rb

132 lines
3.6 KiB
Ruby
Raw Normal View History

class Projects::MilestonesController < Projects::ApplicationController
include Gitlab::Utils::StrongMemoize
2017-04-25 10:25:20 -04:00
include MilestoneActions
before_action :check_issuables_available!
before_action :milestone, only: [:edit, :update, :destroy, :show, :merge_requests, :participants, :labels, :promote]
2012-04-08 17:28:58 -04:00
# Allow read any milestone
before_action :authorize_read_milestone!
2012-04-08 17:28:58 -04:00
# Allow admin milestone
before_action :authorize_admin_milestone!, except: [:index, :show, :merge_requests, :participants, :labels, :promote]
2012-04-08 17:28:58 -04:00
respond_to :html
def index
@sort = params[:sort] || 'due_date_asc'
@milestones = milestones.sort_by_attribute(@sort)
respond_to do |format|
format.html do
@project_namespace = @project.namespace.becomes(Namespace)
2017-07-07 11:08:49 -04:00
# 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
@milestones = @milestones.page(params[:page])
end
format.json do
render json: @milestones.to_json(methods: :name)
end
end
2012-04-08 17:28:58 -04:00
end
def new
@milestone = @project.milestones.new
respond_with(@milestone)
end
def edit
respond_with(@milestone)
end
def show
@project_namespace = @project.namespace.becomes(Namespace)
respond_to do |format|
format.html
end
2012-04-08 17:28:58 -04:00
end
def create
@milestone = Milestones::CreateService.new(project, current_user, milestone_params).execute
2012-04-08 17:28:58 -04:00
2017-07-07 11:08:49 -04:00
if @milestone.valid?
redirect_to project_milestone_path(@project, @milestone)
2012-04-08 17:28:58 -04:00
else
render "new"
end
end
def update
@milestone = Milestones::UpdateService.new(project, current_user, milestone_params).execute(milestone)
2012-04-08 17:28:58 -04:00
respond_to do |format|
format.js
format.html do
2012-04-08 17:28:58 -04:00
if @milestone.valid?
redirect_to project_milestone_path(@project, @milestone)
2012-04-08 17:28:58 -04:00
else
render :edit
end
end
end
end
def promote
promoted_milestone = Milestones::PromoteService.new(project, current_user).execute(milestone)
2018-03-22 16:02:22 -04:00
flash[:notice] = "#{milestone.title} promoted to <a href=\"#{group_milestone_path(project.group, promoted_milestone.iid)}\">group milestone</a>.".html_safe
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
rescue Milestones::PromoteService::PromoteMilestoneError => error
redirect_to milestone, alert: error.message
end
2012-04-08 17:28:58 -04:00
def destroy
return access_denied! unless can?(current_user, :admin_milestone, @project)
Milestones::DestroyService.new(project, current_user).execute(milestone)
2012-04-08 17:28:58 -04:00
respond_to do |format|
2018-01-04 08:15:27 -05:00
format.html { redirect_to namespace_project_milestones_path, status: 303 }
format.js { head :ok }
2012-04-08 17:28:58 -04:00
end
end
protected
2017-07-07 11:08:49 -04:00
def milestones
strong_memoize(:milestones) do
2017-07-07 11:08:49 -04:00
MilestonesFinder.new(search_params).execute
end
end
2012-04-08 17:28:58 -04:00
def milestone
2014-01-19 13:55:59 -05:00
@milestone ||= @project.milestones.find_by!(iid: params[:id])
2012-04-08 17:28:58 -04:00
end
def authorize_admin_milestone!
return render_404 unless can?(current_user, :admin_milestone, @project)
end
def milestone_params
2016-11-15 12:48:30 -05:00
params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
end
def search_params
if request.format.json? && @project.group && can?(current_user, :read_group, @project.group)
groups = @project.group.self_and_ancestors_ids
end
params.permit(:state).merge(project_ids: @project.id, group_ids: groups)
end
2012-04-08 17:28:58 -04:00
end