BigW Consortium Gitlab

forks_controller.rb 1.9 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

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

11
    @forks               = base_query.merge(ProjectsFinder.new.execute(current_user))
12 13 14 15 16
    @total_forks_count   = base_query.size
    @private_forks_count = @total_forks_count - @forks.size
    @public_forks_count  = @total_forks_count - @private_forks_count

    @sort  = params[:sort] || 'id_desc'
17
    @forks = @forks.search(params[:filter_projects]) if params[:filter_projects].present?
18
    @forks = @forks.order_by(@sort).page(params[:page])
19 20 21 22 23 24 25 26 27 28

    respond_to do |format|
      format.html

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

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

  def create
Vinnie Okada committed
37
    namespace = Namespace.find(params[:namespace_key])
38

39 40 41 42
    @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
43 44

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