fork_context.rb
1.17 KB
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
36
37
module Projects
class ForkContext < BaseContext
include Gitlab::ShellAdapter
def initialize(project, user)
@from_project, @current_user = project, user
end
def execute
project = Project.new
project.initialize_dup(@from_project)
project.name = @from_project.name
project.path = @from_project.path
project.namespace = current_user.namespace
Project.transaction do
#First save the DB entries as they can be rolled back if the repo fork fails
project.creator = current_user
project.build_forked_project_link(forked_to_project_id: project.id, forked_from_project_id: @from_project.id)
if project.save
project.users_projects.create(project_access: UsersProject::MASTER, user: current_user)
end
#Now fork the repo
unless gitlab_shell.fork_repository(@from_project.path_with_namespace, project.namespace.path)
raise "forking failed in gitlab-shell"
end
project.ensure_satellite_exists
end
project
rescue => ex
project.errors.add(:base, "Can't fork project. Please try again later")
project.destroy
end
end
end