diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-01-16 19:03:42 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-01-16 19:03:42 +0200 |
commit | 1d2bdb4d5880bb1ad39b62c81ff2971aa0cb3798 (patch) | |
tree | 8ac3215b2c25cb57b5f2198fd4d85c7bad968c2b /app/services/projects/fork_service.rb | |
parent | dba982403b7b894d2096ea61b89a247060eefe57 (diff) | |
download | gitlab-ce-1d2bdb4d5880bb1ad39b62c81ff2971aa0cb3798.tar.gz |
Move all Context classes into Services
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/services/projects/fork_service.rb')
-rw-r--r-- | app/services/projects/fork_service.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/app/services/projects/fork_service.rb b/app/services/projects/fork_service.rb new file mode 100644 index 00000000000..2f1c7b18aa0 --- /dev/null +++ b/app/services/projects/fork_service.rb @@ -0,0 +1,44 @@ +module Projects + class ForkService < BaseService + include Gitlab::ShellAdapter + + def initialize(project, user) + @from_project, @current_user = project, user + end + + def execute + project = @from_project.dup + project.name = @from_project.name + project.path = @from_project.path + project.namespace = current_user.namespace + project.creator = current_user + + # If the project cannot save, we do not want to trigger the project destroy + # as this can have the side effect of deleting a repo attached to an existing + # project with the same name and namespace + if project.valid? + begin + Project.transaction do + #First save the DB entries as they can be rolled back if the repo fork fails + 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 + rescue => ex + project.errors.add(:base, "Fork transaction failed.") + project.destroy + end + else + project.errors.add(:base, "Invalid fork destination") + end + project + + end + end +end |