summaryrefslogtreecommitdiff
path: root/app/services/projects/fork_service.rb
blob: de6dc38cc8e345f87c22445f4622a2a11086f349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Projects
  class ForkService < BaseService
    def execute
      new_params = {
        forked_from_project_id: @project.id,
        visibility_level:       allowed_visibility_level,
        description:            @project.description,
        name:                   @project.name,
        path:                   @project.path,
        shared_runners_enabled: @project.shared_runners_enabled,
        builds_enabled:         @project.builds_enabled,
        namespace_id:           @params[:namespace].try(:id) || current_user.namespace.id
      }

      if @project.avatar.present? && @project.avatar.image?
        new_params[:avatar] = @project.avatar
      end

      new_project = CreateService.new(current_user, new_params).execute
      new_project
    end

    private

    def allowed_visibility_level
      project_level = @project.visibility_level

      if Gitlab::VisibilityLevel.non_restricted_level?(project_level)
        project_level
      else
        Gitlab::VisibilityLevel.highest_allowed_level
      end
    end
  end
end