新增:分支同步历史记录以及创建同步分支接口

This commit is contained in:
yystopf 2024-04-19 17:26:20 +08:00
parent 240e086232
commit 0f5066f943
5 changed files with 63 additions and 10 deletions

View File

@ -8,6 +8,9 @@ class Api::V1::Projects::SyncRepositoriesController < Api::V1::BaseController
def create
@sync_repository1, @sync_repository2, @sync_repository_branch1, @sync_repository_branch2 = Api::V1::Projects::SyncRepositories::CreateService.call(@project, sync_repository_params)
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def sync
@ -20,16 +23,22 @@ class Api::V1::Projects::SyncRepositoriesController < Api::V1::BaseController
@sync_repository_branches.each do |item|
TouchSyncJob.perform_later(item)
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def unbind
return render_error("请输入正确的同步项目ID") unless params[:sync_repository_ids].present?
return render_error("请输入正确的同步仓库ID") unless params[:sync_repository_ids].present?
@sync_repositories = SyncRepository.where(id: params[:sync_repository_ids].split(","))
@sync_repositories.each do |repo|
Reposync::DeleteRepoService.call(repo.repo_name)
repo.destroy
end
render_ok
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def change_enable
@ -48,13 +57,46 @@ class Api::V1::Projects::SyncRepositoriesController < Api::V1::BaseController
else
render_error("更新失败!")
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def create_branch
return render_error("请输入正确的同步仓库ID") unless params[:sync_repository_ids].present?
return render_error("请输入正确的Gitlink分支名称") unless params[:gitlink_branch_name].present?
return render_error("请输入正确的外部仓库分支名称") unless params[:external_branch_name].present?
return render_error("请输入正确的首次同步方向") unless params[:first_sync_direction].present?
params[:sync_repository_ids].split(",").each do |id|
repo = SyncRepository.find_by_id id
branch = Reposync::CreateSyncBranchService.call(repo.repo_name, params[:gitlink_branch_name], params[:external_branch_name])
return render_error(branch[2]) if branch[0].to_i !=0
SyncRepositoryBranch.create!(sync_repository_id: id, gitlink_branch_name: params[:gitlink_branch_name], external_branch_name: params[:external_branch_name], reposync_branch_id: branch[1]['id'])
TouchSyncJob.perform_later(branch) if params[:first_sync_direction].to_i == repo.sync_direction
end
render_ok
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def branches
return render_error("请输入正确的同步项目ID") unless params[:sync_repository_ids].present?
@sync_repository_branches = SyncRepositoryBranch.where(sync_repository_id: params[:sync_repository_ids].split(",")).order(updated_at: :desc)
return render_error("请输入正确的同步仓库ID") unless params[:sync_repository_ids].present?
@sync_repository_branches = SyncRepositoryBranch.where(sync_repository_id: params[:sync_repository_ids].split(","))
@sync_repository_branches = @sync_repository_branches.ransack(gitlink_branch_name_or_external_branch_name_cont: params[:branch_name]).result if params[:branch_name].present?
@group_sync_repository_branch = @sync_repository_branches.group(:gitlink_branch_name, :external_branch_name).count
@group_sync_repository_branch = @sync_repository_branches.group(:gitlink_branch_name, :external_branch_name).select("max(updated_at) as updated_at, gitlink_branch_name, external_branch_name").sort_by{|i|i.updated_at}
end
def history
return render_error("请输入正确的同步分支ID") unless params[:reposync_branch_ids]
@reposync_branch_logs = []
params[:reposync_branch_ids].split(",").each do |branch_id|
branch = SyncRepositoryBranch.find_by(reposync_branch_id: branch_id)
repo = branch.sync_repository
_, logs, _ = Reposync::GetLogsService.call(repo.repo_name, branch_id)
@reposync_branch_logs += logs
end
end
private

View File

@ -91,6 +91,7 @@ class Reposync::ClientService < ApplicationService
else
puts "[reposync][ERROR] code: #{body["code_status"]}"
puts "[reposync][ERROR] message: #{body["msg"]}"
return [body["code_status"], body["data"], body["msg"]]
end
end
end

View File

@ -1,11 +1,11 @@
json.total_count @group_sync_repository_branch.keys.count
json.sync_repository_branches @group_sync_repository_branch.each do |key|
json.gitlink_branch_name key[0][0]
json.external_branch_name key[0][1]
branches = @sync_repository_branches.where(gitlink_branch_name: key[0][0], external_branch_name: key[0][1]).order(updated_at: :desc)
json.total_count @group_sync_repository_branch.count
json.sync_repository_branches @group_sync_repository_branch.each do |item|
json.gitlink_branch_name item.gitlink_branch_name
json.external_branch_name item.external_branch_name
branches = @sync_repository_branches.where(gitlink_branch_name: item.gitlink_branch_name, external_branch_name: item.external_branch_name).order(updated_at: :desc)
branch = branches.first
json.type branch&.sync_repository&.type
json.sync_time branch.sync_time
json.sync_time branch.sync_time.strftime("%Y-%m-%d %H:%M:%S")
json.sync_status branch.sync_status
json.enable branch.enable
json.reposync_branch_ids branches.pluck(:reposync_branch_id)

View File

@ -0,0 +1,8 @@
json.total_count @reposync_branch_logs.count
json.logs @reposync_branch_logs.each do |log|
type = log['repo_name'].start_with?('gitee') ? 'gitee' : 'github'
json.change_from log['sync_direct'] == "to_inter" ? type : 'gitlink'
json.commit_id log['commit_id']
json.sync_time log['update_at']
json.log log['log']
end

View File

@ -84,6 +84,8 @@ defaults format: :json do
get :branches
post :change_enable
post :unbind
get :history
post :create_branch
end
end
resource :dataset, only: [:create, :update, :show]