|
|
|
@@ -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 |