forks_controller.rb 1.81 KB
Newer Older
1
class Projects::ForksController < Projects::ApplicationController
2
  include ContinueParams
3

4
  # Authorize
5 6
  before_action :require_non_empty_project
  before_action :authorize_download_code!
7
  before_action :authenticate_user!, only: [:new, :create]
8

9
  def index
10
    base_query = project.forks.includes(:creator)
11

12
    forks                = ForkProjectsFinder.new(project, params: params.merge(search: params[:filter_projects]), current_user: current_user).execute
13
    @total_forks_count   = base_query.size
14
    @private_forks_count = @total_forks_count - forks.size
15 16
    @public_forks_count  = @total_forks_count - @private_forks_count

17
    @forks = forks.page(params[:page])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18 19 20 21 22 23 24 25 26 27

    respond_to do |format|
      format.html

      format.json do
        render json: {
          html: view_to_html_string("projects/forks/_projects", projects: @forks)
        }
      end
    end
28 29
  end

30
  def new
31 32
    @namespaces = current_user.manageable_namespaces
    @namespaces.delete(@project.namespace)
33 34 35
  end

  def create
Vinnie Okada's avatar
Vinnie Okada committed
36
    namespace = Namespace.find(params[:namespace_key])
37

Douwe Maan's avatar
Douwe Maan committed
38 39 40 41
    @forked_project = namespace.projects.find_by(path: project.path)
    @forked_project = nil unless @forked_project && @forked_project.forked_from_project == project

    @forked_project ||= ::Projects::ForkService.new(project, current_user, namespace: namespace).execute
42 43

    if @forked_project.saved? && @forked_project.forked?
44
      if @forked_project.import_in_progress?
45
        redirect_to project_import_path(@forked_project, continue: continue_params)
46
      else
47 48 49
        if continue_params
          redirect_to continue_params[:to], notice: continue_params[:notice]
        else
50
          redirect_to project_path(@forked_project), notice: "The project '#{@forked_project.name}' was successfully forked."
51
        end
52
      end
53 54 55 56 57
    else
      render :error
    end
  end
end