diff --git a/app/controllers/api/v1/projects/branches_controller.rb b/app/controllers/api/v1/projects/branches_controller.rb index f16aa8373..5d685a4a9 100644 --- a/app/controllers/api/v1/projects/branches_controller.rb +++ b/app/controllers/api/v1/projects/branches_controller.rb @@ -9,12 +9,21 @@ class Api::V1::Projects::BranchesController < Api::V1::BaseController @result_object = Api::V1::Projects::Branches::AllListService.call(@project, current_user&.gitea_token) end - before_action :require_operate_above, only: [:create] + before_action :require_operate_above, only: [:create, :destroy] def create @result_object = Api::V1::Projects::Branches::CreateService.call(@project, branch_params, current_user&.gitea_token) end + def destroy + @result_object = Api::V1::Projects::Branches::DeleteService.call(@project, params[:id], current_user&.gitea_token) + if @result_object + return render_ok + else + return render_error('删除分支失败!') + end + end + before_action :require_manager_above, only: [:update_default_branch] def update_default_branch diff --git a/app/services/api/v1/projects/branches/delete_service.rb b/app/services/api/v1/projects/branches/delete_service.rb new file mode 100644 index 000000000..c6ff5f80e --- /dev/null +++ b/app/services/api/v1/projects/branches/delete_service.rb @@ -0,0 +1,42 @@ +class Api::V1::Projects::Branches::DeleteService < ApplicationService + include ActiveModel::Model + + attr_accessor :project, :token, :owner, :repo, :branch_name + attr_accessor :gitea_data + + validates :branch_name, presence: true + + def initialize(project, branch_name, token=nil) + @project = project + @owner = project&.owner.login + @repo = project&.identifier + @branch_name = branch_name + @token = token + end + + def call + raise Error, errors.full_messages.join(",") unless valid? + + # check_branch_exist + excute_data_to_gitea + + true + end + + private + def request_params + { + access_token: token + } + end + + def excute_data_to_gitea + @gitea_data = $gitea_client.delete_repos_branches_by_owner_repo_branch(owner, repo, branch_name, {query: request_params}) rescue nil + 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?(@branch_name) + end +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 92f18e2d7..6fb9f3500 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -46,7 +46,7 @@ defaults format: :json do get :hooktasks end end - resources :branches, only:[:index, :create] do + resources :branches, only:[:index, :create, :destroy] do collection do get :all patch :update_default_branch