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
2022-11-25 23:54:43 +05:30
REDIRECT_TARGETS = [ :new_release ] . freeze
2017-09-10 17:25:29 +05:30
before_action :check_issuables_available!
2021-01-03 14:25:43 +05:30
before_action :milestone , only : [ :edit , :update , :destroy , :show , :issues , :merge_requests , :participants , :labels , :promote ]
2022-11-25 23:54:43 +05:30
before_action :redirect_path , only : [ :new , :create ]
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
2021-01-03 14:25:43 +05:30
before_action :authorize_admin_milestone! , except : [ :index , :show , :issues , :merge_requests , :participants , :labels ]
2018-11-29 20:51:05 +05:30
# Allow to promote milestone
before_action :authorize_promote_milestone! , only : :promote
2014-09-02 18:07:02 +05:30
respond_to :html
2021-12-11 22:18:48 +05:30
feature_category :team_planning
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-03 14:25:43 +05:30
2014-09-02 18:07:02 +05:30
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
2022-06-21 17:19:12 +05:30
@milestone_states = Milestone . states_count ( @project )
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
2021-09-30 23:02:18 +05:30
render json : @milestones . to_json ( only : [ :id , :title , :due_date ] , methods : :name )
2016-06-02 11:05:42 +05:30
end
end
2014-09-02 18:07:02 +05:30
end
def new
2021-06-08 01:23:25 +05:30
@noteable = @milestone = @project . milestones . new
2014-09-02 18:07:02 +05:30
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?
2022-11-25 23:54:43 +05:30
if @redirect_path == :new_release
redirect_to new_project_release_path ( @project )
else
redirect_to project_milestone_path ( @project , @milestone )
end
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
2023-07-09 08:55:56 +05:30
rescue ActiveRecord :: StaleObjectError
respond_to do | format |
format . html do
@conflict = true
render :edit
end
format . json do
render json : {
errors : [
format (
_ ( " Someone edited this %{model_name} at the same time you did. Please refresh your browser and make sure your changes will not unintentionally remove theirs. " ) ,
model_name : _ ( 'milestone' )
)
]
} , status : :conflict
end
end
2014-09-02 18:07:02 +05:30
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
2022-08-27 11:52:29 +05:30
rescue Milestones :: PromoteService :: PromoteMilestoneError = > e
redirect_to milestone , alert : e . message
2018-03-17 18:26:18 +05:30
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
2022-11-25 23:54:43 +05:30
def redirect_path
path = params [ :redirect_path ] & . to_sym
@redirect_path = path if REDIRECT_TARGETS . include? ( path )
end
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
2021-06-08 01:23:25 +05:30
@noteable = @milestone || = @project . milestones . find_by! ( iid : params [ :id ] )
2014-09-02 18:07:02 +05:30
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
2023-07-09 08:55:56 +05:30
params . require ( :milestone )
. permit (
:description ,
:due_date ,
:lock_version ,
:start_date ,
:state_event ,
:title
)
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