milestones_controller.rb 3.65 KB
Newer Older
1
class Projects::MilestonesController < Projects::ApplicationController
2
  include Gitlab::Utils::StrongMemoize
3 4
  include MilestoneActions

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

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

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

  respond_to :html

  def index
17
    @sort = params[:sort] || 'due_date_asc'
18
    @milestones = milestones.sort_by_attribute(@sort)
19

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

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

  def edit
    respond_with(@milestone)
  end

  def show
45
    @project_namespace = @project.namespace.becomes(Namespace)
46 47 48 49

    respond_to do |format|
      format.html
    end
50 51 52
  end

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

Felipe Artur's avatar
Felipe Artur committed
55
    if @milestone.valid?
56
      redirect_to project_milestone_path(@project, @milestone)
57 58 59 60 61 62
    else
      render "new"
    end
  end

  def update
63
    @milestone = Milestones::UpdateService.new(project, current_user, milestone_params).execute(milestone)
64 65 66

    respond_to do |format|
      format.js
67
      format.html do
68
        if @milestone.valid?
69
          redirect_to project_milestone_path(@project, @milestone)
70 71 72 73 74 75 76
        else
          render :edit
        end
      end
    end
  end

77
  def promote
78
    promoted_milestone = Milestones::PromoteService.new(project, current_user).execute(milestone)
79

80
    flash[:notice] = "#{milestone.title} promoted to <a href=\"#{group_milestone_path(project.group, promoted_milestone.iid)}\"><u>group milestone</u></a>.".html_safe
81 82 83 84 85 86 87 88
    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
89 90 91 92
  rescue Milestones::PromoteService::PromoteMilestoneError => error
    redirect_to milestone, alert: error.message
  end

93
  def destroy
94 95
    return access_denied! unless can?(current_user, :admin_milestone, @project)

96
    Milestones::DestroyService.new(project, current_user).execute(milestone)
97 98

    respond_to do |format|
Lin Jen-Shin's avatar
Lin Jen-Shin committed
99
      format.html { redirect_to namespace_project_milestones_path, status: :see_other }
100
      format.js { head :ok }
101 102 103 104 105
    end
  end

  protected

Felipe Artur's avatar
Felipe Artur committed
106
  def milestones
107
    strong_memoize(:milestones) do
Felipe Artur's avatar
Felipe Artur committed
108 109 110 111
      MilestonesFinder.new(search_params).execute
    end
  end

112
  def milestone
skv's avatar
skv committed
113
    @milestone ||= @project.milestones.find_by!(iid: params[:id])
114 115 116 117 118 119
  end

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

120
  def milestone_params
121
    params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
122
  end
123 124

  def search_params
125
    if request.format.json? && @project.group && can?(current_user, :read_group, @project.group)
126
      groups = @project.group.self_and_ancestors_ids
127 128
    end

129
    params.permit(:state).merge(project_ids: @project.id, group_ids: groups)
130
  end
131
end