From 1533e5e737d0d4d1a94a2c4c3d8852a690b1d184 Mon Sep 17 00:00:00 2001 From: chenjing Date: Tue, 4 Jul 2023 16:44:32 +0800 Subject: [PATCH] add fork to orgs --- app/controllers/forks_controller.rb | 34 ++++++++++++++++++++----- app/services/projects/fork_service.rb | 2 +- app/views/forks/fork_list.json.jbuilder | 16 ++++++++++++ config/routes.rb | 6 ++++- 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 app/views/forks/fork_list.json.jbuilder diff --git a/app/controllers/forks_controller.rb b/app/controllers/forks_controller.rb index 7d7db9350..6ad8db7c5 100644 --- a/app/controllers/forks_controller.rb +++ b/app/controllers/forks_controller.rb @@ -2,18 +2,40 @@ class ForksController < ApplicationController before_action :require_login before_action :require_profile_completed, only: [:create] before_action :load_project - before_action :authenticate_project!, :authenticate_user! + before_action :authenticate_user! + before_action :authenticate_project!, only: [:create] + + def fork_list + @user = current_user + @organizations = current_user.organizations + + end def create - @new_project = Projects::ForkService.new(current_user, @project, params[:organization], params[:new_name], params[:new_identifier]).call + target_owner = if params[:organization].present? && @organization + @organization + else + current_user + end + @new_project = Projects::ForkService.new(target_owner, @project, params[:organization], params[:new_name], params[:new_identifier]).call + if @new_project == false + render_result(-1, "已fork过一次该项目,无法再次进行fork") + end end private def authenticate_project! - 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: (params[:new_identifier] || @project.identifier)) - render_result(0, "fork失败,你已拥有了这个项目") + if params[:organization].present? + return render_forbidden('参数错误,当organization存在时不允许fork重命名') if params[:new_identifier].present? || params[:new_name].present? + @organization = Organization.find_by(login:params[:organization]) + return render_forbidden('仓库不存在') unless @organization.present? + return render_forbidden('你没有权限操作') unless @organization.is_admin?(current_user.id) + end + + if params[:organization].blank? && Project.exists?(user_id: current_user.id, identifier: (params[:new_identifier] || @project.identifier)) + render_result(-1, "fork失败,您已拥有了这个项目") + elsif @organization && Project.exists?(user_id: [@organization.id], identifier: (params[:new_identifier] || @project.identifier)) + render_result(-1, "fork失败,组织已拥有了这个项目") end # return if current_user != @project.owner # render_result(-1, "自己不能fork自己的项目") diff --git a/app/services/projects/fork_service.rb b/app/services/projects/fork_service.rb index 3e6a153fb..a46b8d86a 100644 --- a/app/services/projects/fork_service.rb +++ b/app/services/projects/fork_service.rb @@ -18,7 +18,7 @@ class Projects::ForkService < ApplicationService :license_id, :ignore_id, {repository: [:identifier, :hidden]}] result = Gitea::Repository::ForkService.new(@project.owner, @target_owner, @project.identifier, @organization, @new_identifier).call - + return false if result['clone_url'].nil? clone_project.owner = @target_owner clone_project.forked_from_project_id = @project.id clone_project.gpid = result['id'] diff --git a/app/views/forks/fork_list.json.jbuilder b/app/views/forks/fork_list.json.jbuilder new file mode 100644 index 000000000..185b8e4fc --- /dev/null +++ b/app/views/forks/fork_list.json.jbuilder @@ -0,0 +1,16 @@ +json.user do + json.id @user.id + json.type @user.type + json.name @user.real_name + json.login @user.login + json.image_url url_to_avatar(@user) + json.forked Project.exists?(user_id: @user.id, forked_from_project_id: @project.id) +end +json.organizations @organizations do |organization| + json.forked Project.exists?(user_id: organization.id, forked_from_project_id: @project.id) + json.id organization.id + json.name organization.login + json.nickname organization.nickname.blank? ? organization.name : organization.nickname + json.avatar_url url_to_avatar(organization) + json.created_at organization.created_on.strftime("%Y-%m-%d") +end diff --git a/config/routes.rb b/config/routes.rb index 0d04c498a..8750508ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -625,7 +625,11 @@ Rails.application.routes.draw do end resources :hooks - resources :forks, only: [:create] + resources :forks, only: [:create] do + collection do + get :fork_list + end + end resources :project_trends, :path => :activity, only: [:index, :create] resources :issue_tags, :path => :labels, only: [:create, :edit, :update, :destroy, :index] resources :version_releases, :path => :releases, only: [:index,:new, :show, :create, :edit, :update, :destroy]