新增:更改默认分支接口
This commit is contained in:
parent
3f78899c58
commit
f2cdba29ea
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue