BigW Consortium Gitlab

milestones_controller.rb 2.85 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]
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]
12 13 14 15

  respond_to :html

  def index
16
    @sort = params[:sort] || 'due_date_asc'
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 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 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 72
        else
          render :edit
        end
      end
    end
  end

  def destroy
73 74
    return access_denied! unless can?(current_user, :admin_milestone, @project)

75
    Milestones::DestroyService.new(project, current_user).execute(milestone)
76 77

    respond_to do |format|
78
      format.html { redirect_to namespace_project_milestones_path, status: 302 }
79
      format.js { head :ok }
80 81 82 83 84
    end
  end

  protected

Felipe Artur committed
85 86 87 88 89 90 91 92 93 94 95 96
  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

97
  def milestone
skv committed
98
    @milestone ||= @project.milestones.find_by!(iid: params[:id])
99 100 101 102 103 104
  end

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

105
  def milestone_params
106
    params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
107
  end
108
end