支持用户fork自己的仓库

This commit is contained in:
xxqfamous 2023-06-07 10:54:36 +08:00
parent 14da90d601
commit 8f51955bd7
4 changed files with 15 additions and 8 deletions

View File

@ -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失败你已拥有了这个项目")

View File

@ -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

View File

@ -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

View File

@ -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|