diff --git a/app/controllers/forks_controller.rb b/app/controllers/forks_controller.rb index c740c8b03..53b043f49 100644 --- a/app/controllers/forks_controller.rb +++ b/app/controllers/forks_controller.rb @@ -5,12 +5,12 @@ class ForksController < ApplicationController before_action :authenticate_project!, :authenticate_user! def create - @new_project = Projects::ForkService.new(current_user, @project, params[:organization]).call + @new_project = Projects::ForkService.new(current_user, @project, params[:organization], params[:new_name], params[:new_identifier]).call end private def authenticate_project! - if current_user&.id == @project.user_id + if current_user&.id == @project.user_id && (params[:new_identifier].blank? || params[:new_identifier] == @project.identifier) render_result(-1, "自己不能fork自己的项目") elsif Project.exists?(user_id: current_user.id, identifier: @project.identifier) render_result(0, "fork失败,你已拥有了这个项目") diff --git a/app/services/gitea/repository/fork_service.rb b/app/services/gitea/repository/fork_service.rb index c43a9ddf2..ca0c0fe68 100644 --- a/app/services/gitea/repository/fork_service.rb +++ b/app/services/gitea/repository/fork_service.rb @@ -1,5 +1,5 @@ class Gitea::Repository::ForkService < Gitea::ClientService - attr_reader :old_owner, :target_owner, :repo_name, :organization + attr_reader :old_owner, :target_owner, :repo_name, :organization, :new_identifier # old_owner: 被clone的项目(源项目)拥有者 # target_owner: clone后的醒目(新项目)的拥有者 @@ -7,10 +7,12 @@ class Gitea::Repository::ForkService < Gitea::ClientService # { # "organization": "string" #组织名称 # } - def initialize(old_owner, target_owner, repo_name, organization=nil) + def initialize(old_owner, target_owner, repo_name, organization=nil, new_identifier=nil) @old_owner = old_owner @target_owner = target_owner @repo_name = repo_name + @organization = organization + @new_identifier = new_identifier end def call @@ -24,6 +26,7 @@ class Gitea::Repository::ForkService < Gitea::ClientService def request_params hash = Hash.new.merge(token: target_owner.gitea_token) hash = hash.merge(data: {organization: organization}) if organization + hash = hash.merge(data: {name: new_identifier}) if new_identifier hash end diff --git a/app/services/projects/fork_service.rb b/app/services/projects/fork_service.rb index f8b38df47..3e6a153fb 100644 --- a/app/services/projects/fork_service.rb +++ b/app/services/projects/fork_service.rb @@ -1,10 +1,12 @@ class Projects::ForkService < ApplicationService - attr_reader :target_owner, :project, :organization + attr_reader :target_owner, :project, :organization, :new_name, :new_identifier - def initialize(target_owner, project, organization=nil) + def initialize(target_owner, project, organization=nil, new_name=nil, new_identifier=nil) @target_owner = target_owner @project = project @organization = organization + @new_name = new_name + @new_identifier = new_identifier end def call @@ -15,11 +17,13 @@ class Projects::ForkService < ApplicationService :rep_identifier, :project_category_id, :project_language_id, :license_id, :ignore_id, {repository: [:identifier, :hidden]}] - result = Gitea::Repository::ForkService.new(@project.owner, @target_owner, @project.identifier, @organization).call + result = Gitea::Repository::ForkService.new(@project.owner, @target_owner, @project.identifier, @organization, @new_identifier).call clone_project.owner = @target_owner clone_project.forked_from_project_id = @project.id clone_project.gpid = result['id'] + clone_project.name = @new_name if @new_name.present? + clone_project.identifier = @new_identifier if @new_identifier.present? clone_project.save! new_repository = clone_project.repository diff --git a/lib/tasks/batch_forked_project.rake b/lib/tasks/batch_forked_project.rake index 4cb50a77a..e0fea6f5b 100644 --- a/lib/tasks/batch_forked_project.rake +++ b/lib/tasks/batch_forked_project.rake @@ -15,7 +15,7 @@ namespace :batch_forked_project do user = User.find_by(login: username) next if user.blank? next if Project.exists?(user_id: user.id, identifier: project.identifier) - new_project = Projects::ForkService.new(user, project, nil).call + new_project = Projects::ForkService.new(user, project, nil, nil, nil).call random_num = rand(5..20) members = user_logins.sample(random_num) members.each do |m|