新增:同步仓库更新信息接口

This commit is contained in:
yystopf 2024-04-24 16:11:32 +08:00
parent 9c32cd8769
commit a273c1cfcf
4 changed files with 85 additions and 0 deletions

View File

@ -13,6 +13,15 @@ class Api::V1::Projects::SyncRepositoriesController < Api::V1::BaseController
tip_exception(e.message)
end
def update_info
return render_error("请输入正确的同步仓库ID") unless params[:sync_repository_ids].present?
Api::V1::Projects::SyncRepositories::UpdateService.call(@project, params[:sync_repository_ids] , sync_repository_update_params)
render_ok
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def sync
return render_error("请输入正确的同步方向!") if params[:sync_direction].blank?
@sync_repositories = SyncRepository.where(project: @project, sync_direction: params[:sync_direction])
@ -107,4 +116,8 @@ class Api::V1::Projects::SyncRepositoriesController < Api::V1::BaseController
params.permit(:type, :external_token, :external_repo_address, :sync_granularity, :external_branch_name, :gitlink_branch_name, :first_sync_direction)
end
def sync_repository_update_params
params.permit(:external_token, :external_repo_address)
end
end

View File

@ -0,0 +1,40 @@
class Api::V1::Projects::SyncRepositories::UpdateService < ApplicationService
include ActiveModel::Model
attr_reader :project, :external_token, :external_repo_address, :sync_repositories
attr_accessor :sync_repository1, :sync_repository2
validates :external_repo_address, format: { with: CustomRegexp::URL_REGEX, multiline: true, message: "地址格式不正确" }
validates :external_token, presence: true
#Api::V1::Projects::SyncRepositories::UpdateService.call(Project.last, "21,22", {external_repo_address: "https://github.com/viletyy/testdevops.git", external_token:"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"})
def initialize(project, sync_repository_ids, params)
@project = project
@sync_repositories = SyncRepository.where(project_id: project.id, id: sync_repository_ids.split(","))
@external_token = params[:external_token]
@external_repo_address = params[:external_repo_address]
end
def call
raise Error, errors.full_messages.join(",") unless valid?
update_sync_repository
end
private
def update_sync_repository
@sync_repositories.each do |repo|
Reposync::UpdateRepoAddrService.call(repo&.repo_name, internal_repo_address, internal_token, external_repo_address, external_token)
repo.update_attributes!({external_repo_address: external_repo_address})
end
end
def internal_repo_address
"#{EduSetting.get("gitlink_repo_domain")}/#{project.owner&.login}/#{project.identifier}.git"
end
def internal_token
EduSetting.get("gitlink_admin_token")
end
end

View File

@ -0,0 +1,31 @@
class Reposync::UpdateRepoAddrService < Reposync::ClientService
attr_accessor :repo_name, :internal_repo_address, :inter_token, :external_repo_address, :exter_token
def initialize(repo_name, internal_repo_address, inter_token, external_repo_address, exter_token)
@repo_name = repo_name
@internal_repo_address = internal_repo_address
@inter_token = inter_token
@external_repo_address = external_repo_address
@exter_token = exter_token
end
def call
result = put(url, request_params)
response = render_response(result)
end
private
def request_params
Hash.new.merge(data: {
internal_repo_address: internal_repo_address,
inter_token: inter_token,
external_repo_address: external_repo_address,
exter_token: exter_token
}.stringify_keys)
end
def url
"/cerobot/sync/repo/#{repo_name}/repo_addr".freeze
end
end

View File

@ -80,6 +80,7 @@ defaults format: :json do
scope module: :projects do
resources :sync_repositories, only: [:create, :index] do
collection do
post :update_info
post :sync
get :branches
post :change_enable