FIX 解决镜像项目手动同步镜像不成功的问题

This commit is contained in:
jasder 2021-06-03 17:04:22 +08:00
parent 2b82b2c4f5
commit fcf8033fbf
5 changed files with 153 additions and 96 deletions

View File

@ -5,7 +5,27 @@ class SyncMirroredRepositoryJob < ApplicationJob
repo = Repository.find_by(id: repo_id)
current_user = User.find_by(id: user_id)
return if repo.blank? || current_user.blank?
result = Gitea::Repository::SyncMirroredService.new(repo.owner.login, repo.identifier, token: current_user.gitea_token).call
# TODO
# 先同步镜像库
if repo.config_accelerator?
puts "[gitea-accelerator]: ###### 镜像库开始同步 ######"
result = Gitea::Accelerator::SyncMirroredService.call(repo.identifier)
puts "[gitea-accelerator]: ###### 镜像库同步状态为 #{result[:status]}"
# TODO 暂时解决从镜像库镜像动作时间先执行的问题
# 避免了Gitea::Repository::SyncMirroredService先执行后加速器Gitea::Accelerator::SyncMirroredService
# 再执行的导致Gitea::Repository::SyncMirroredService执行镜像不是最新代码的问题
sleep 3.seconds
end
sync_common!(repo, current_user)
end
def sync_common!(repo, user)
result = Gitea::Repository::SyncMirroredService.call(repo.owner.login,
repo.identifier, token: user.gitea_token)
repo&.mirror.set_status! if result[:status] === 200
end
end

View File

@ -82,5 +82,8 @@ class Repository < ApplicationRecord
source_clone_url.blank? ? mirror_url : source_clone_url
end
def config_accelerator?
!source_clone_url.blank?
end
end

View File

@ -0,0 +1,96 @@
class Gitea::Accelerator::BaseService < ApplicationService
def post(url, params)
puts "[gitea] request params: #{params}"
puts "[gitea] access_username: #{access_username}"
puts "[gitea] access_password: #{access_password}"
conn.post do |req|
req.url full_url(url)
req.body = params.to_json
end
end
private
def conn
@client ||= begin
Faraday.new(url: domain) do |req|
req.request :url_encoded
req.headers['Content-Type'] = 'application/json'
req.response :logger # 显示日志
req.adapter Faraday.default_adapter
req.basic_auth(access_username, access_password)
end
end
@client
end
def base_url
accelerator["base_url"]
end
def domain
accelerator["domain"]
end
def api_url
[domain, base_url].join('')
end
def full_url(api_rest, action='post')
url = [api_url, api_rest].join('').freeze
url = action === 'get' ? url : URI.escape(url)
puts "[gitea] request url: #{url}"
url
end
def access_username
accelerator["access_key_id"]
end
def access_password
accelerator["access_key_secret"]
end
def access_uid
accelerator["access_admin_uid"]
end
def accelerator
Gitea.gitea_config[:accelerator]
end
def render_status(response)
puts "[gitea] response status: #{response.status}"
puts "[gitea] response body: #{response.body}"
case response.status
when 201
success
when 403
error('APIForbiddenError')
when 422
error('APIValidationError')
else
error("MigrateError")
end
end
def error(message)
{
status: :error,
message: message,
data: nil
}
end
def success(data=nil)
{
status: :success,
message: nil,
data: data
}
end
def check_accelerator!
accelerator.blank? || access_username.blank? || access_password.blank? || domain.blank?
end
end

View File

@ -1,4 +1,4 @@
class Gitea::Accelerator::MigrateService < ApplicationService
class Gitea::Accelerator::MigrateService < Gitea::Accelerator::BaseService
attr_reader :params
# params description:
@ -36,8 +36,8 @@ class Gitea::Accelerator::MigrateService < ApplicationService
render_status(response)
end
private
private
def request_params
{
uid: access_uid,
@ -52,97 +52,4 @@ class Gitea::Accelerator::MigrateService < ApplicationService
def url
"/repos/migrate".freeze
end
def post(url, params)
puts "[gitea] request params: #{params}"
puts "[gitea] access_username: #{access_username}"
puts "[gitea] access_password: #{access_password}"
conn.post do |req|
req.url full_url(url)
req.body = params.to_json
end
end
def conn
@client ||= begin
Faraday.new(url: domain) do |req|
req.request :url_encoded
req.headers['Content-Type'] = 'application/json'
req.response :logger # 显示日志
req.adapter Faraday.default_adapter
req.basic_auth(access_username, access_password)
end
end
@client
end
def base_url
accelerator["base_url"]
end
def domain
accelerator["domain"]
end
def api_url
[domain, base_url].join('')
end
def full_url(api_rest, action='post')
url = [api_url, api_rest].join('').freeze
url = action === 'get' ? url : URI.escape(url)
puts "[gitea] request url: #{url}"
url
end
def access_username
accelerator["access_key_id"]
end
def access_password
accelerator["access_key_secret"]
end
def access_uid
accelerator["access_admin_uid"]
end
def accelerator
Gitea.gitea_config[:accelerator]
end
def render_status(response)
puts "[gitea] response status: #{response.status}"
puts "[gitea] response body: #{response.body}"
case response.status
when 201
success
when 403
error('APIForbiddenError')
when 422
error('APIValidationError')
else
error("MigrateError")
end
end
def error(message)
{
status: :error,
message: message,
data: nil
}
end
def success(data=nil)
{
status: :success,
message: nil,
data: data
}
end
def check_accelerator!
accelerator.blank? || access_username.blank? || access_password.blank? || domain.blank?
end
end

View File

@ -0,0 +1,31 @@
# Sync a mirrored repository
class Gitea::Accelerator::SyncMirroredService < Gitea::Accelerator::BaseService
attr_reader :repo, :token
# repo *
# name of the repo to sync
# example:
# Gitea::Accelerator::SyncMirroredService.call(repo.identifier)
def initialize(repo, token=nil)
@repo = repo
@token = token
end
def call
return error('[gitea:] accelerator config missing') if check_accelerator!
response = post(url, request_params)
{status: response.status}
end
private
def request_params
Hash.new.merge(token: token).compact
end
def url
"/repos/#{access_username}/#{repo}/mirror-sync".freeze
end
end