mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-17 18:25:56 +08:00
新增: 创建分支接口
This commit is contained in:
27
app/services/api/v1/projects/branches/all_list_service.rb
Normal file
27
app/services/api/v1/projects/branches/all_list_service.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
class Api::V1::Projects::Branches::AllListService < ApplicationService
|
||||
|
||||
attr_accessor :project, :token, :owner, :repo
|
||||
attr_accessor :gitea_data
|
||||
|
||||
def initialize(project, token=nil)
|
||||
@project = project
|
||||
@owner = project&.owner.login
|
||||
@repo = project&.identifier
|
||||
@token = token
|
||||
end
|
||||
|
||||
def call
|
||||
$gitea_client.token = token unless token.blank?
|
||||
load_gitea_data
|
||||
|
||||
$gitea_client.token = nil unless token.blank?
|
||||
gitea_data
|
||||
rescue
|
||||
raise Error, "服务器错误,请联系系统管理员!"
|
||||
end
|
||||
|
||||
private
|
||||
def load_gitea_data
|
||||
@gitea_data = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo)
|
||||
end
|
||||
end
|
||||
47
app/services/api/v1/projects/branches/create_service.rb
Normal file
47
app/services/api/v1/projects/branches/create_service.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
class Api::V1::Projects::Branches::CreateService < ApplicationService
|
||||
include ActiveModel::Model
|
||||
|
||||
attr_accessor :project, :token, :owner, :repo, :old_branch_name, :new_branch_name
|
||||
attr_accessor :gitea_data
|
||||
|
||||
validates :new_branch_name, :old_branch_name, presence: :true
|
||||
|
||||
def initialize(project, params, token=nil)
|
||||
@project = project
|
||||
@owner = project&.owner.login
|
||||
@repo = project&.identifier
|
||||
@new_branch_name = params[:new_branch_name]
|
||||
@old_branch_name = params[:old_branch_name]
|
||||
@token = token
|
||||
end
|
||||
|
||||
def call
|
||||
raise Error, errors.full_messages.join(",") unless valid?
|
||||
$gitea_client.token = token unless token.blank?
|
||||
|
||||
check_new_branch_exist
|
||||
excute_data_to_gitea
|
||||
|
||||
$gitea_client.token = nil unless token.blank?
|
||||
gitea_data
|
||||
end
|
||||
|
||||
private
|
||||
def request_body
|
||||
{
|
||||
new_branch_name: new_branch_name,
|
||||
old_branch_name: old_branch_name,
|
||||
}
|
||||
end
|
||||
|
||||
def excute_data_to_gitea
|
||||
@gitea_data = $gitea_client.post_repos_branches_by_owner_repo(owner, repo, {body: request_body.to_json})
|
||||
raise Error, '创建分支失败!' unless @gitea_data.is_a?(Hash)
|
||||
end
|
||||
|
||||
def check_new_branch_exist
|
||||
result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo)
|
||||
raise Error, '查询分支名称失败!' unless result.is_a?(Hash)
|
||||
raise Error, '分支已存在!' if result['branch_name'].include?(@new_branch_name)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user