milestones_controller.rb 3.24 KB
Newer Older
1
class Projects::MilestonesController < Projects::ApplicationController
2 3
  include MilestoneActions

4
  before_action :check_issuables_available!
5
  before_action :milestone, only: [:edit, :update, :destroy, :show, :merge_requests, :participants, :labels, :promote]
6 7

  # Allow read any milestone
8
  before_action :authorize_read_milestone!
9 10

  # Allow admin milestone
11
  before_action :authorize_admin_milestone!, except: [:index, :show, :merge_requests, :participants, :labels, :promote]
12 13 14 15

  respond_to :html

  def index
16
    @sort = params[:sort] || 'due_date_asc'
Felipe Artur's avatar
Felipe Artur committed
17
    @milestones = milestones.sort(@sort)
18

19
    respond_to do |format|
20
      format.html do
21
        @project_namespace = @project.namespace.becomes(Namespace)
Felipe Artur's avatar
Felipe Artur committed
22 23 24 25
        # 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
26
        @milestones = @milestones.page(params[:page])
27
      end
28
      format.json do
29
        render json: @milestones.to_json(methods: :name)
30 31
      end
    end
32 33 34 35 36 37 38 39 40 41 42 43
  end

  def new
    @milestone = @project.milestones.new
    respond_with(@milestone)
  end

  def edit
    respond_with(@milestone)
  end

  def show
44
    @project_namespace = @project.namespace.becomes(Namespace)
45 46 47
  end

  def create
48
    @milestone = Milestones::CreateService.new(project, current_user, milestone_params).execute
49

Felipe Artur's avatar
Felipe Artur committed
50
    if @milestone.valid?
51
      redirect_to project_milestone_path(@project, @milestone)
52 53 54 55 56 57
    else
      render "new"
    end
  end

  def update
58
    @milestone = Milestones::UpdateService.new(project, current_user, milestone_params).execute(milestone)
59 60 61

    respond_to do |format|
      format.js
62
      format.html do
63
        if @milestone.valid?
64
          redirect_to project_milestone_path(@project, @milestone)
65 66 67 68 69 70 71
        else
          render :edit
        end
      end
    end
  end

72 73 74 75 76 77 78 79
  def promote
    promoted_milestone = Milestones::PromoteService.new(project, current_user).execute(milestone)
    flash[:notice] = "Milestone has been promoted to group milestone."
    redirect_to group_milestone_path(project.group, promoted_milestone.iid)
  rescue Milestones::PromoteService::PromoteMilestoneError => error
    redirect_to milestone, alert: error.message
  end

80
  def destroy
81 82
    return access_denied! unless can?(current_user, :admin_milestone, @project)

83
    Milestones::DestroyService.new(project, current_user).execute(milestone)
84 85

    respond_to do |format|
86
      format.html { redirect_to namespace_project_milestones_path, status: 302 }
87
      format.js { head :ok }
88 89 90 91 92
    end
  end

  protected

Felipe Artur's avatar
Felipe Artur committed
93 94 95 96 97 98 99 100 101 102 103 104
  def milestones
    @milestones ||= begin
      if @project.group && can?(current_user, :read_group, @project.group)
        group = @project.group
      end

      search_params = params.merge(project_ids: @project.id, group_ids: group&.id)

      MilestonesFinder.new(search_params).execute
    end
  end

105
  def milestone
skv's avatar
skv committed
106
    @milestone ||= @project.milestones.find_by!(iid: params[:id])
107 108 109 110 111 112
  end

  def authorize_admin_milestone!
    return render_404 unless can?(current_user, :admin_milestone, @project)
  end

113
  def milestone_params
114
    params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
115
  end
116
end