mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-22 04:35:45 +08:00
新增:更改默认分支接口
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user