修改迁移的问题

This commit is contained in:
sylor_huang@126.com 2020-03-13 16:53:03 +08:00
parent 98c5372d61
commit b9bd07541e
6 changed files with 132 additions and 120 deletions

View File

@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
include RenderExpand
include RenderHelper
include ControllerRescueHandler
include LaboratoryHelper
# include LaboratoryHelper
include GitHelper
include LoggerHelper
include LoginHelper

View File

@ -1,12 +1,13 @@
class Gitea::UserForm
include ActiveModel::Model
EMAIL_REGEX = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
EMAIL_REGEX = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_\-.]+(\.[a-zA-Z0-9_-]+)+$/
include ActiveModel::Model
attr_accessor :username, :email, :password
validates :username, presence: true
validates :email, presence: true, format: { with: EMAIL_REGEX, multiline: true }
validates :password, presence: true
validate :check_username, :check_email

View File

@ -6,68 +6,36 @@ class SyncForgeJob < ApplicationJob
Rails.logger.info("#######______sync__start__########")
sync_params = ActiveSupport::JSON.decode(sync_params)
keys_to_delete = %w(id created_on updated_on platform)
keys_other_delete = %w(id created_at updated_at user_id)
all_target_params = sync_params["target_params"]
roles_params = sync_params["roles"]
user_params = sync_params["user_params"]["user_params"]
owner_extension_params = sync_params["user_params"]["user_extension_params"]
platform = sync_params["platform"]
user_params = user_params["user"] if old_version_source.include?(platform) #trustie上需要
if user_params.present?
owner_params = user_params&.except!(*keys_to_delete)
user_password = random_password
if User.exists?(login: owner_params["login"])
new_user = User.find_by(login: owner_params["login"])
else
new_user = User.new(owner_params.merge(platform: platform))
interactor = Gitea::RegisterInteractor.call({username: owner_params["login"], email: owner_params["mail"], password: user_password})
if interactor.success?
gitea_user = interactor.result
new_user.gitea_uid = gitea_user['id']
else
response = Gitea::User::GetTokenService.new("#{owner_params["login"]}").call
if response.status == 200
user_id = JSON.parse(response.body)["id"]
new_user.gitea_uid = user_id
else
new_user.gitea_uid = ""
end
end
if new_user.gitea_uid.present?
result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call
new_user.gitea_token = result['sha1']
end
if new_user.save!
owner_extension_params = owner_extension_params["user_extensions"] if old_version_source.include?(platform) #trustie上需要
if owner_extension_params.present?
owner_extension_params = owner_extension_params&.except!(*keys_other_delete).merge(user_id: new_user.id)
UserExtension.create!(owner_extension_params)
end
end
end
new_user = sync_user(user_params, owner_extension_params,platform)
if new_user.present?
ActiveRecord::Base.transaction do
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)
all_target_params.each do |project|
target_params = {
project_params: project["project_params"],
member_params: project["member_params"],
issue_params: project["issue_params"],
versions_params: project["versions_params"],
project_score_params: project["project_score_params"],
pull_request_params: project["pull_request_params"],
repo_params: project["repo_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)
if all_target_params.present?
all_target_params.each do |project|
target_params = {
project_params: project["project_params"],
member_params: project["member_params"],
issue_params: project["issue_params"],
versions_params: project["versions_params"],
project_score_params: project["project_score_params"],
pull_request_params: project["pull_request_params"],
repo_params: project["repo_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)
end
end
rescue Exception => e
Rails.logger.info("#######_________user__sync__failed__#########{e}")
@ -86,6 +54,49 @@ class SyncForgeJob < ApplicationJob
private
def sync_user(owner_params,owner_extension_params,platform)
Rails.logger.info("#######______sync_user_start__########")
keys_other_delete = %w(id created_at updated_at user_id)
keys_to_delete = %w(id created_on updated_on platform)
owner_params = owner_params["user"] if old_version_source.include?(platform) #trustie上需要
owner_params = owner_params&.except!(*keys_to_delete)
user_password = random_password
if User.exists?(login: owner_params["login"])
new_user = User.find_by(login: owner_params["login"])
else
new_user = User.new(owner_params.merge(platform: platform))
interactor = Gitea::RegisterInteractor.call({username: owner_params["login"], email: owner_params["mail"], password: user_password})
if interactor.success?
gitea_user = interactor.result
new_user.gitea_uid = gitea_user['id']
else
response = Gitea::User::GetTokenService.new("#{owner_params["login"]}").call
if response.status == 200
user_id = JSON.parse(response.body)["id"]
new_user.gitea_uid = user_id
else
new_user.gitea_uid = ""
end
end
if new_user.gitea_uid.present?
result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call
new_user.gitea_token = result['sha1']
end
if new_user.save!
if owner_extension_params.present?
owner_extension_params = owner_extension_params["user_extensions"] if old_version_source.include?(platform) #trustie上需要
owner_extension_params = owner_extension_params&.except!(*keys_other_delete).merge(user_id: new_user.id)
UserExtension.create!(owner_extension_params)
end
end
end
Rails.logger.info("#######______sync_user_end__########")
new_user
end
def random_password
[*('a'..'z'),*(0..9),*('A'..'Z')].shuffle[0..8].join
end
@ -108,16 +119,12 @@ class SyncForgeJob < ApplicationJob
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?
unless Project.exists?(identifier: project["identifier"])
project = project["project"] if old_version_source.include?(platform)
unless Project.exists?(user_id: new_user.id, identifier: project["identifier"])
new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id))
if new_project.save!
if repo_params.present?
unless Repository.exists?(user_id: new_user.id,identifier: project["identifier"]) || repo_params.blank?
repository_params = {
hidden: project["is_public"],
user_id: new_user.id,
@ -127,10 +134,11 @@ class SyncForgeJob < ApplicationJob
# 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?
project_score = project_score["project_score"] if old_version_source.include?(platform) #trustie上需要
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)
@ -157,8 +165,11 @@ class SyncForgeJob < ApplicationJob
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?
if r.present?
r = r["watcher"] if old_version_source.include?(platform)
new_wathcer = Watcher.new(r&.except!(*roles_other_delete).merge(watchable_id: project_id))
new_wathcer.save(:validate => false)
end
end
end
end
@ -180,8 +191,11 @@ class SyncForgeJob < ApplicationJob
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?
if r.present?
r = r["praise_tread"] if old_version_source.include?(platform) #trustie上需要
new_tread = PraiseTread.new(r&.except!(*roles_other_delete).merge(praise_tread_object_id: project_id))
new_tread.save(:validate => false)
end
end
end
end
@ -203,9 +217,11 @@ class SyncForgeJob < ApplicationJob
if roles.present?
Role.transaction do
roles.each do |r|
r = r["role"] if old_version_source.include?(platform) #trustie上需要
unless Role.exists?(name: r["name"])
Role.create!(r&.except!(*roles_other_delete)) if r.present?
if r.present?
r = r["role"] if old_version_source.include?(platform) #trustie上需要
unless Role.exists?(name: r["name"])
Role.create!(r&.except!(*roles_other_delete))
end
end
end
end
@ -223,8 +239,6 @@ class SyncForgeJob < ApplicationJob
def sync_members(project_id,members_params,platform)
Rails.logger.info("#######______sync_members_start__#######")
member_to_delete = %w(id created_on user_id project_id)
member_user_delete = %w(id created_on updated_on platform)
keys_other_delete = %w(id created_at updated_at user_id)
ActiveRecord::Base.transaction do
begin
if members_params.present?
@ -234,34 +248,16 @@ class SyncForgeJob < ApplicationJob
member_user_exten = m["member_extension"]
member_roles = m["member_roles"]
member_issues = m["member_issues"]
member_user = member_user["user"] if old_version_source.include?(platform) #trustie上需要
if member_user.present?
unless User.exists?(login: member_user["login"])
u = User.new(member_user&.except!(*member_user_delete).merge(platform: platform))
user_password = random_password
interactor = Gitea::RegisterInteractor.call({username: member_user["login"], email: member_user["mail"], password: user_password})
if interactor.success?
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.new(member_user["login"], user_password).call
u.gitea_token = result['sha1']
u.gitea_uid = gitea_user['id']
if u.save!
member_user_exten = member_user_exten["user_extensions"] if old_version_source.include?(platform) #trustie上需要
if member_user_exten.present?
UserExtension.create!(member_user_exten&.except!(*keys_other_delete).merge(user_id: u.id))
end
u = sync_user(member_user,member_user_exten, platform)
if u.present? && member.present?
member = member["member"] if old_version_source.include?(platform) #trustie上需要
unless Member.exists?(user_id: u.id, project_id: project_id)
new_member = Member.new(member&.except!(*member_to_delete).merge(project_id: project_id, user_id: u.id))
if new_member.save!
sync_user_issues(project_id, u.id, member["user_id"],member_issues, platform)
sync_member_roles(new_member.id, member_roles,platform)
end
member = member["member"] if old_version_source.include?(platform) #trustie上需要
if member.present?
new_member = Member.new(member&.except!(*member_to_delete).merge(project_id: project_id, user_id: u.id))
if new_member.save!
sync_user_issues(project_id, u.id, member["user_id"],member_issues, platform)
sync_member_roles(new_member.id, member_roles,platform)
end
end
else
raise Error, "gitea的项目创建失败"
end
end
end
@ -283,8 +279,8 @@ class SyncForgeJob < ApplicationJob
if members.present?
MemberRole.transaction do
members.each do |m|
m = m["member_role"] if old_version_source.include?(platform) #trustie上需要
if m.present?
m = m["member_role"] if old_version_source.include?(platform) #trustie上需要
role_id = Role.select(:id,:position)&.where(position: m["role_id"])&.first&.id
MemberRole.create!(m&.except!(*member_role_delete).merge(member_id: member_id, role_id: role_id)) if m.present?
end
@ -310,8 +306,9 @@ class SyncForgeJob < ApplicationJob
jours_params = is_params["jours_params"]
commit_params = is_params["commit_params"]
Issue.select(:id, :assigned_to_id).where(assigned_to_id: old_user_id)&.update_all(assigned_to_id: new_user_id)
issue_params = issue_params["issue"] if old_version_source.include?(platform) #trustie上需要
if issue_params.present?
issue_params = issue_params["issue"] if old_version_source.include?(platform) #trustie上需要
assgin_user = issue_params["assigned_to_id"]
if issue_params["assigned_to_id"].to_i == old_user_id
assgin_user = new_user_id
@ -344,8 +341,11 @@ class SyncForgeJob < ApplicationJob
if jours_params.present?
Journal.transaction do
jours_params.each do |i|
i = i["journal"] if old_version_source.include?(platform) #trustie上需要
Journal.create!(i&.except!(*jour_to_delete).merge(journalized_id: issue_id, user_id: user_id)) if i.present?
if i.present?
i = i["journal"] if old_version_source.include?(platform) #trustie上需要
Journal.create!(i&.except!(*jour_to_delete).merge(journalized_id: issue_id, user_id: user_id))
end
end
end
end
@ -367,8 +367,11 @@ class SyncForgeJob < ApplicationJob
if commit_params.present?
CommitIssue.transaction do
commit_params.each do |i|
i = i["commit_issues"] if old_version_source.include?(platform) #trustie上需要
CommitIssue.create!(i&.except!(*commit_to_delete).merge(issue_id: issue_id, project_id: project_id)) if i.present?
if i.present?
i = i["commit_issues"] if old_version_source.include?(platform) #trustie上需要
CommitIssue.create!(i&.except!(*commit_to_delete).merge(issue_id: issue_id, project_id: project_id))
end
end
end
end
@ -390,8 +393,11 @@ class SyncForgeJob < ApplicationJob
if pull_params.present?
PullRequest.transaction do
pull_params.each do |i|
i = i["pull_request"] if old_version_source.include?(platform) #trustie上需要
PullRequest.create!(i&.except!(*commit_to_delete).merge(user_id: user_id, project_id: project_id)) if i.present?
if i.present?
i = i["pull_request"] if old_version_source.include?(platform) #trustie上需要
PullRequest.create!(i&.except!(*commit_to_delete).merge(user_id: user_id, project_id: project_id))
end
end
end
end
@ -412,8 +418,11 @@ class SyncForgeJob < ApplicationJob
if commit_params.present?
Commit.transaction do
commit_params.each do |i|
i = i["commit"] if old_version_source.include?(platform) #trustie上需要
Commit.create!(i&.except!(*commit_to_delete).merge(repository_id: repository_id, project_id: project_id)) if i.present?
if i.present?
i = i["commit"] if old_version_source.include?(platform) #trustie上需要
Commit.create!(i&.except!(*commit_to_delete).merge(repository_id: repository_id, project_id: project_id))
end
end
end
end
@ -434,8 +443,10 @@ class SyncForgeJob < ApplicationJob
if version_params.present?
Version.transaction do
version_params.each do |i|
i = i["version"] if old_version_source.include?(platform) #trustie上需要
Version.create!(i&.except!(*version_to_delete).merge(user_id: new_user_id, project_id: project_id)) if i.present?
if i.present?
i = i["version"] if old_version_source.include?(platform) #trustie上需要
Version.create!(i&.except!(*version_to_delete).merge(user_id: new_user_id, project_id: project_id))
end
end
end
end

View File

@ -8,16 +8,16 @@ module Searchable::Dependents::User
private
def check_searchable_dependents
if firstname_previously_changed? || lastname_previously_changed? || user_extension&.school_id_previously_changed?
# reindex shixun
created_shixuns.each(&:reindex)
# reindex course
manage_courses.each(&:reindex)
# reindex subject
created_subjects.each(&:reindex)
subjects.each(&:reindex)
end
# if firstname_previously_changed? || lastname_previously_changed? || user_extension&.school_id_previously_changed?
# # reindex shixun
# created_shixuns.each(&:reindex)
#
# # reindex course
# manage_courses.each(&:reindex)
#
# # reindex subject
# created_subjects.each(&:reindex)
# subjects.each(&:reindex)
# end
end
end

View File

@ -3,7 +3,7 @@ class User < ApplicationRecord
include Likeable
include BaseModel
include ProjectOperable
include Searchable::Dependents::User
# include Searchable::Dependents::User
# Account statuses
STATUS_ANONYMOUS = 0

View File

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