sync_forge_job的修改

This commit is contained in:
sylor_huang@126.com 2020-03-13 14:17:12 +08:00
parent 934595c6a4
commit 98c5372d61
5 changed files with 120 additions and 28 deletions

View File

@ -1,4 +1,6 @@
class SyncForgeController < ApplicationController class SyncForgeController < ApplicationController
skip_before_action :user_setup
skip_before_action :check_sign
def create def create
SyncForgeJob.perform_later(params[:sync_params]) SyncForgeJob.perform_later(params[:sync_params])

View File

@ -39,15 +39,20 @@ class SyncForgeJob < ApplicationJob
result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call
new_user.gitea_token = result['sha1'] new_user.gitea_token = result['sha1']
end end
end
if new_user.gitea_uid.present? && new_user.save! if new_user.save!
owner_extension_params = owner_extension_params["user_extensions"] if old_version_source.include?(platform) #trustie上需要 owner_extension_params = owner_extension_params["user_extensions"] if old_version_source.include?(platform) #trustie上需要
if owner_extension_params.present? if owner_extension_params.present?
owner_extension_params = owner_extension_params&.except!(*keys_other_delete).merge(user_id: new_user.id) owner_extension_params = owner_extension_params&.except!(*keys_other_delete).merge(user_id: new_user.id)
UserExtension.create!(owner_extension_params) UserExtension.create!(owner_extension_params)
end
end end
end
if new_user.present?
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
begin begin
Watcher&.where(user_id: user_params["id"]).update_all(user_id: new_user.id)
ProjectTrend&.where(user_id: user_params["id"]).update_all(user_id: new_user.id)
sync_roles(roles_params, platform) sync_roles(roles_params, platform)
all_target_params.each do |project| all_target_params.each do |project|
target_params = { target_params = {
@ -58,7 +63,9 @@ class SyncForgeJob < ApplicationJob
project_score_params: project["project_score_params"], project_score_params: project["project_score_params"],
pull_request_params: project["pull_request_params"], pull_request_params: project["pull_request_params"],
repo_params: project["repo_params"], repo_params: project["repo_params"],
commit_params: project["commit_params"] commit_params: project["commit_params"],
watchers_params: project["watchers_params"],
praise_trends_params: project["praise_trends_params"]
} }
sync_projects(new_user, user_params["id"],target_params, platform) sync_projects(new_user, user_params["id"],target_params, platform)
end end
@ -97,25 +104,41 @@ class SyncForgeJob < ApplicationJob
version_params = targets[:versions_params] version_params = targets[:versions_params]
member_params = targets[:member_params] member_params = targets[:member_params]
project_score = targets[:project_score_params] project_score = targets[:project_score_params]
project = project["project"] if old_version_source.include?(platform) #trustie上需要 repo_params = targets[:repo_params]
praise_trends_params = targets[:praise_trends_params]
watchers_params = targets[:watchers_params]
if old_version_source.include?(platform) #trustie上需要
project = project["project"]
repo_params = repo_params["gitlab"]
end
if project.present? if project.present?
new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id)) unless Project.exists?(identifier: project["identifier"])
if new_project.save! new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id))
repository_params = { if new_project.save!
hidden: project["is_public"], if repo_params.present?
user_id: new_user.id, repository_params = {
identifier: project["identifier"] hidden: project["is_public"],
} user_id: new_user.id,
Repositories::CreateService.new(new_user, new_project, repository_params).call identifier: project["identifier"]
project_score = project_score["project_score"] if old_version_source.include?(platform) #trustie上需要 }
if project_score.present? Repositories::CreateService.new(new_user, new_project, repository_params).call
ProjectScore.create!(project_score&.except!(*score_to_delete).merge(project_id: new_project.id)) # SyncRepositoryJob.perform_later(new_user.login, project["identifier"]) #暂时不迁移版本库
end
project_score = project_score["project_score"] if old_version_source.include?(platform) #trustie上需要
if project_score.present?
ProjectScore.create!(project_score&.except!(*score_to_delete).merge(project_id: new_project.id))
end
sync_user_issues(new_project.id, new_user.id,old_user_id,issue_params, platform)
sync_members(new_project.id, member_params,platform)
sync_commits(new_project.id,new_project.gpid, commit_params,platform)
sync_pull_requests(new_project.id,new_user.id, pr_params,platform)
sync_versions(new_project.id, new_user.id, version_params,platform)
sync_watchers(new_project.id, watchers_params, platform)
sync_praises(new_project.id,praise_trends_params,platform)
end end
sync_user_issues(new_project.id, new_user.id,old_user_id,issue_params, platform)
sync_members(new_project.id, member_params,platform)
sync_commits(new_project.id,new_project.gpid, commit_params,platform)
sync_pull_requests(new_project.id,new_user.id, pr_params,platform)
sync_versions(new_project.id, new_user.id, version_params,platform)
end end
end end
end end
@ -125,6 +148,52 @@ class SyncForgeJob < ApplicationJob
end end
end end
def sync_watchers(project_id, watchers_params, platform)
Rails.logger.info("#######______sync_watchers_start__#######")
roles_other_delete = %w(id watchable_id)
ActiveRecord::Base.transaction do
begin
if watchers_params.present?
Watcher.transaction do
watchers_params.each do |r|
r = r["watcher"] if old_version_source.include?(platform) #trustie上需要
Watcher.create!(r&.except!(*roles_other_delete).merge(watchable_id: project_id)) if r.present?
end
end
end
Rails.logger.info("#######______sync_watchers_end__#######")
rescue Exception => e
Rails.logger.info("#######_______sync_watchers__failed__#########{e}")
raise ActiveRecord::Rollback
end
end
end
def sync_praises(project_id, praises_params, platform)
Rails.logger.info("#######______sync_praises_start__#######")
roles_other_delete = %w(id praise_tread_object_id created_at updated_at)
ActiveRecord::Base.transaction do
begin
if praises_params.present?
PraiseTread.transaction do
praises_params.each do |r|
r = r["praise_tread"] if old_version_source.include?(platform) #trustie上需要
PraiseTread.create!(r&.except!(*roles_other_delete).merge(praise_tread_object_id: project_id)) if r.present?
end
end
end
Rails.logger.info("#######______sync_praises_end__#######")
rescue Exception => e
Rails.logger.info("#######_______sync_praises__failed__#########{e}")
raise ActiveRecord::Rollback
end
end
end
def sync_roles(roles,platform) def sync_roles(roles,platform)
Rails.logger.info("#######______sync_roles_start__#######") Rails.logger.info("#######______sync_roles_start__#######")
roles_other_delete = %w(id) roles_other_delete = %w(id)

View File

@ -0,0 +1,20 @@
class SyncRepositoryJob < ApplicationJob
queue_as :default
def perform(login,identifier)
Rails.logger.info("#######__________begin_sync_repository_________####")
gitea_test_ssh = "sshpass -p '@pd:lk4+407' ssh -o StrictHostKeyChecking=no -p 30122 root@123.59.135.93"
gitea_jump_ssh = "sshpass -p '#pd:lk3+407' ssh -o StrictHostKeyChecking=no -p 30122 root@10.9.69.134"
gitea_user_repo = "/home/git/gitea-repositories/#{login}"
git_clone_bare = "git clone --bare http://git.trustie.net/#{login}/#{identifier}.git"
gitea_repos = system("#{gitea_test_ssh} && #{gitea_jump_ssh} && cd #{gitea_user_repo} && rm -rf #{identifier}.git && #{git_clone_bare}")
if gitea_repos
Rails.logger.info("########________gitea_clone_success__________##############")
else
Rails.logger.info("########________gitea_clone_failed_____user_loigin:_#{login}_identifier:__#{identifier}")
end
end
end

View File

@ -3,10 +3,10 @@ class UserExtension < ApplicationRecord
enum identity: { teacher: 0, student: 1, professional: 2, developer: 3, enterprise: 4, unselect: -1 } enum identity: { teacher: 0, student: 1, professional: 2, developer: 3, enterprise: 4, unselect: -1 }
belongs_to :user, touch: true belongs_to :user, touch: true
belongs_to :school, optional: true # belongs_to :school, optional: true
belongs_to :department, optional: true # belongs_to :department, optional: true
before_save :set_laboratory_school # before_save :set_laboratory_school
def identity_text def identity_text
I18n.t("user.identity.#{identity}") I18n.t("user.identity.#{identity}")

View File

@ -48,7 +48,8 @@ class Repositories::CreateService < ApplicationService
def gitea_repository_params def gitea_repository_params
hash = { hash = {
name: params[:identifier], name: params[:identifier],
private: !params[:hidden] private: !params[:hidden],
readme: "ReadMe"
# "auto_init": true, # "auto_init": true,
# "description": "string", # "description": "string",
# "gitignores": "string", # "gitignores": "string",