BigW Consortium Gitlab

forks_controller.rb 1.96 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               = base_query.merge(ProjectsFinder.new.execute(current_user))
13 14 15 16 17
    @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'
18
    @forks = @forks.search(params[:filter_projects]) if params[:filter_projects].present?
19
    @forks = @forks.order_by(@sort).page(params[:page])
20 21 22 23 24 25 26 27 28 29

    respond_to do |format|
      format.html

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

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

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

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

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