From f06a1f63ddcc2e1947f17b6d9f2c7f44cf0d44e9 Mon Sep 17 00:00:00 2001 From: viletyy Date: Sat, 6 Feb 2021 15:40:44 +0800 Subject: [PATCH] [ADD]project transfer [FIX] [FIX] --- .gitignore | 1 + .../gitea/repository/transfer_service.rb | 27 +++++++++++ app/services/projects/transfer_service.rb | 45 +++++++++++++++++++ public/react/build | 1 - 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 app/services/gitea/repository/transfer_service.rb create mode 100644 app/services/projects/transfer_service.rb delete mode 160000 public/react/build diff --git a/.gitignore b/.gitignore index 836852ea0..77104d20a 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ public/react/yarn.lock /.idea/* # Ignore react node_modules +public/react/* /public/react/.cache /public/react/node_modules/ /public/react/config/stats.json diff --git a/app/services/gitea/repository/transfer_service.rb b/app/services/gitea/repository/transfer_service.rb new file mode 100644 index 000000000..358ac9421 --- /dev/null +++ b/app/services/gitea/repository/transfer_service.rb @@ -0,0 +1,27 @@ +class Gitea::Repository::TransferService < Gitea::ClientService + attr_reader :token, :owner, :repo, :new_owner + + def initialize(token, owner, repo, new_owner) + @token = token + @owner = owner + @repo = repo + @new_owner = new_owner + end + + def call + response = post(url, request_params) + render_status(response) + end + + private + def request_params + transfer_params = { + new_owner: new_owner + } + Hash.new.merge(token: token, data: transfer_params) + end + + def url + "/repos/#{owner}/#{repo}/transfer".freeze + end +end \ No newline at end of file diff --git a/app/services/projects/transfer_service.rb b/app/services/projects/transfer_service.rb new file mode 100644 index 000000000..f9b5c5700 --- /dev/null +++ b/app/services/projects/transfer_service.rb @@ -0,0 +1,45 @@ +class Projects::TransferService < ApplicationService + attr_accessor :project, :owner, :new_owner + + def initialize(project, new_owner) + @project = project + @owner = project.owner + @new_owner = new_owner + end + + def call + Rails.logger.info("###### Project transfer_service begin ######") + ActiveRecord::Base.transaction do + gitea_update_owner + update_owner + update_visit_teams + end + + Rails.logger.info("##### Project transfer_service end ######") + + @project.reload + end + + private + def update_owner + project.update!(user_id: new_owner.id) + end + + def update_visit_teams + if new_owner.is_a?(Organization) + new_owner.teams.where(includes_all_project: true).each do |team| + TeamProject.build(new_owner.id, team.id, project.id) + end + else + project.team_projects.each(&:destroy!) + end + end + + def gitea_update_owner + begin + Gitea::Repository::TransferService.call(owner&.gitea_token, owner&.login, project.identifier, new_owner&.login) + rescue Exception => e + Rails.logger.info("##### Project transfer_service, gitea transfer error #{e}") + end + end +end \ No newline at end of file diff --git a/public/react/build b/public/react/build deleted file mode 160000 index 6348a15cd..000000000 --- a/public/react/build +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6348a15cdb954862dc1b7b5f045a432bcfde7dc4