debian-mirror-gitlab/app/services/projects/fork_service.rb

67 lines
2.1 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module Projects
class ForkService < BaseService
include Gitlab::ShellAdapter
def execute
2015-04-26 12:48:37 +05:30
@from_project = @project
project_params = {
visibility_level: @from_project.visibility_level,
description: @from_project.description,
}
project = Project.new(project_params)
2014-09-02 18:07:02 +05:30
project.name = @from_project.name
project.path = @from_project.path
2015-04-26 12:48:37 +05:30
project.creator = @current_user
if @from_project.avatar.present? && @from_project.avatar.image?
project.avatar = @from_project.avatar
end
if namespace = @params[:namespace]
project.namespace = namespace
else
project.namespace = @current_user.namespace
end
unless @current_user.can?(:create_projects, project.namespace)
project.errors.add(:namespace, 'insufficient access rights')
return project
end
2014-09-02 18:07:02 +05:30
# 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
2015-04-26 12:48:37 +05:30
project.team << [@current_user, :master, @current_user]
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
#Now fork the repo
unless gitlab_shell.fork_repository(@from_project.path_with_namespace, project.namespace.path)
2015-04-26 12:48:37 +05:30
raise 'forking failed in gitlab-shell'
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2014-09-02 18:07:02 +05:30
project.ensure_satellite_exists
end
2015-04-26 12:48:37 +05:30
if @from_project.gitlab_ci?
ForkRegistrationWorker.perform_async(@from_project.id, project.id, @current_user.private_token)
end
2014-09-02 18:07:02 +05:30
rescue => ex
2015-04-26 12:48:37 +05:30
project.errors.add(:base, 'Fork transaction failed.')
2014-09-02 18:07:02 +05:30
project.destroy
end
else
2015-04-26 12:48:37 +05:30
project.errors.add(:base, 'Invalid fork destination')
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
project
2014-09-02 18:07:02 +05:30
end
end
end