From f2cdba29eaa2727a825f9094f44174f45e79d219 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 3 Feb 2023 13:45:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=88=86=E6=94=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/projects/branches_controller.rb | 11 ++++ .../v1/projects/branches/create_service.rb | 7 ++- .../branches/update_default_branch_service.rb | 55 +++++++++++++++++++ config/routes/api.rb | 3 +- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 app/services/api/v1/projects/branches/update_default_branch_service.rb diff --git a/app/controllers/api/v1/projects/branches_controller.rb b/app/controllers/api/v1/projects/branches_controller.rb index 33346573c..f16aa8373 100644 --- a/app/controllers/api/v1/projects/branches_controller.rb +++ b/app/controllers/api/v1/projects/branches_controller.rb @@ -15,6 +15,17 @@ class Api::V1::Projects::BranchesController < Api::V1::BaseController @result_object = Api::V1::Projects::Branches::CreateService.call(@project, branch_params, current_user&.gitea_token) end + before_action :require_manager_above, only: [:update_default_branch] + + def update_default_branch + @result_object = Api::V1::Projects::Branches::UpdateDefaultBranchService.call(@project, params[:default_branch], current_user&.gitea_token) + if @result_object + return render_ok + else + return render_error('更新默认分支失败!') + end + end + private def branch_params params.require(:branch).permit(:new_branch_name, :old_branch_name) diff --git a/app/services/api/v1/projects/branches/create_service.rb b/app/services/api/v1/projects/branches/create_service.rb index 2837dfcf4..eae3779f8 100644 --- a/app/services/api/v1/projects/branches/create_service.rb +++ b/app/services/api/v1/projects/branches/create_service.rb @@ -18,7 +18,7 @@ class Api::V1::Projects::Branches::CreateService < ApplicationService def call raise Error, errors.full_messages.join(",") unless valid? - check_new_branch_exist + check_branch_exist excute_data_to_gitea gitea_data @@ -43,9 +43,10 @@ class Api::V1::Projects::Branches::CreateService < ApplicationService raise Error, '创建分支失败!' unless @gitea_data.is_a?(Hash) end - def check_new_branch_exist + def check_branch_exist result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil raise Error, '查询分支名称失败!' unless result.is_a?(Hash) - raise Error, '分支已存在!' if result['branch_name'].include?(@new_branch_name) + raise Error, '旧分支不存在!' if !result['branch_name'].include?(@old_branch_name) + raise Error, '新分支已存在!' if result['branch_name'].include?(@new_branch_name) end end \ No newline at end of file diff --git a/app/services/api/v1/projects/branches/update_default_branch_service.rb b/app/services/api/v1/projects/branches/update_default_branch_service.rb new file mode 100644 index 000000000..5c220aa5d --- /dev/null +++ b/app/services/api/v1/projects/branches/update_default_branch_service.rb @@ -0,0 +1,55 @@ +class Api::V1::Projects::Branches::UpdateDefaultBranchService < ApplicationService + include ActiveModel::Model + + attr_accessor :project, :token, :owner, :repo, :new_default_branch + attr_accessor :gitea_data + + validates :new_default_branch, presence: true + + def initialize(project, new_default_branch, token=nil) + @project = project + @owner = project&.owner.login + @repo = project&.identifier + @new_default_branch = new_default_branch + @token = token + end + + def call + raise Error, errors.full_messages.join(",") unless valid? + + check_branch_exist + update_project + excute_data_to_gitea + + gitea_data + end + + private + def request_params + { + access_token: token + } + end + + def request_body + { + default_branch: new_default_branch + } + end + + def update_project + @project.attributes = request_body + raise Error, '更新默认分支失败!' unless @project.save + end + + def excute_data_to_gitea + @gitea_data = $gitea_client.patch_repos_by_owner_repo(owner, repo, {body: request_body.to_json, query: request_params}) rescue nil + raise Error, '更新默认分支失败!' unless @gitea_data.is_a?(Hash) + end + + def check_branch_exist + result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil + raise Error, '查询分支名称失败!' unless result.is_a?(Hash) + raise Error, '新默认分支不存在!' if !result['branch_name'].include?(@new_default_branch) + end +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 19531526e..92f18e2d7 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -48,7 +48,8 @@ defaults format: :json do end resources :branches, only:[:index, :create] do collection do - get :all + get :all + patch :update_default_branch end end resources :commits, only: [:index]