修改迁移时gitea数据创建成功,但是本地没创建成功的问题

This commit is contained in:
sylor_huang@126.com 2020-03-12 19:46:59 +08:00
parent 06f74401b9
commit 934595c6a4
2 changed files with 102 additions and 62 deletions

View File

@ -8,40 +8,47 @@ class SyncForgeJob < ApplicationJob
keys_to_delete = %w(id created_on updated_on platform) keys_to_delete = %w(id created_on updated_on platform)
keys_other_delete = %w(id created_at updated_at user_id) keys_other_delete = %w(id created_at updated_at user_id)
ActiveRecord::Base.transaction do
begin
all_target_params = sync_params["target_params"] all_target_params = sync_params["target_params"]
roles_params = sync_params["roles"] roles_params = sync_params["roles"]
user_params = sync_params["user_params"]["user_params"] user_params = sync_params["user_params"]["user_params"]
owner_extension_params = sync_params["user_params"]["user_extension_params"] owner_extension_params = sync_params["user_params"]["user_extension_params"]
platform = sync_params["platform"] platform = sync_params["platform"]
sync_roles(roles_params, platform) user_params = user_params["user"] if old_version_source.include?(platform) #trustie上需要
user_params = user_params["user"] if platform == "trustie" #trustie上需要
if user_params.present? if user_params.present?
owner_params = user_params&.except!(*keys_to_delete) owner_params = user_params&.except!(*keys_to_delete)
user_password = random_password
if User.exists?(login: owner_params["login"]) if User.exists?(login: owner_params["login"])
new_user = User.find_by(login: owner_params["login"]) new_user = User.find_by(login: owner_params["login"])
else else
new_user = User.new(owner_params.merge(platform: platform)) new_user = User.new(owner_params.merge(platform: platform))
user_password = random_password
interactor = Gitea::RegisterInteractor.call({username: owner_params["login"], email: owner_params["mail"], password: user_password}) interactor = Gitea::RegisterInteractor.call({username: owner_params["login"], email: owner_params["mail"], password: user_password})
if interactor.success? if interactor.success?
gitea_user = interactor.result 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 result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call
new_user.gitea_token = result['sha1'] new_user.gitea_token = result['sha1']
new_user.gitea_uid = gitea_user['id'] end
if new_user.save! end
owner_extension_params = owner_extension_params["user_extensions"] if platform == "trustie" #trustie上需要 if new_user.gitea_uid.present? && new_user.save!
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 ActiveRecord::Base.transaction do
else begin
raise Error, "gitea创建用户失败" sync_roles(roles_params, platform)
end
end
all_target_params.each do |project| all_target_params.each do |project|
target_params = { target_params = {
project_params: project["project_params"], project_params: project["project_params"],
@ -55,12 +62,19 @@ class SyncForgeJob < ApplicationJob
} }
sync_projects(new_user, user_params["id"],target_params, platform) sync_projects(new_user, user_params["id"],target_params, platform)
end end
end
rescue Exception => e rescue Exception => e
Rails.logger.info("#######_________user__sync__failed__#########{e}") Rails.logger.info("#######_________user__sync__failed__#########{e}")
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
end end
end end
else
Rails.logger.info("############___________________########{owner_params["login"]}创建失败")
end
end
end
def old_version_source
%w(trustie)
end end
private private
@ -83,7 +97,7 @@ 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 platform == "trustie" #trustie上需要 project = project["project"] if old_version_source.include?(platform) #trustie上需要
if project.present? if project.present?
new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id)) new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id))
if new_project.save! if new_project.save!
@ -93,7 +107,7 @@ class SyncForgeJob < ApplicationJob
identifier: project["identifier"] identifier: project["identifier"]
} }
Repositories::CreateService.new(new_user, new_project, repository_params).call Repositories::CreateService.new(new_user, new_project, repository_params).call
project_score = project_score["project_score"] if platform == "trustie" #trustie上需要 project_score = project_score["project_score"] if old_version_source.include?(platform) #trustie上需要
if project_score.present? if project_score.present?
ProjectScore.create!(project_score&.except!(*score_to_delete).merge(project_id: new_project.id)) ProjectScore.create!(project_score&.except!(*score_to_delete).merge(project_id: new_project.id))
end end
@ -120,7 +134,7 @@ class SyncForgeJob < ApplicationJob
if roles.present? if roles.present?
Role.transaction do Role.transaction do
roles.each do |r| roles.each do |r|
r = r["role"] if platform == "trustie" #trustie上需要 r = r["role"] if old_version_source.include?(platform) #trustie上需要
unless Role.exists?(name: r["name"]) unless Role.exists?(name: r["name"])
Role.create!(r&.except!(*roles_other_delete)) if r.present? Role.create!(r&.except!(*roles_other_delete)) if r.present?
end end
@ -151,7 +165,7 @@ class SyncForgeJob < ApplicationJob
member_user_exten = m["member_extension"] member_user_exten = m["member_extension"]
member_roles = m["member_roles"] member_roles = m["member_roles"]
member_issues = m["member_issues"] member_issues = m["member_issues"]
member_user = member_user["user"] if platform == "trustie" #trustie上需要 member_user = member_user["user"] if old_version_source.include?(platform) #trustie上需要
if member_user.present? if member_user.present?
unless User.exists?(login: member_user["login"]) unless User.exists?(login: member_user["login"])
@ -164,12 +178,12 @@ class SyncForgeJob < ApplicationJob
u.gitea_token = result['sha1'] u.gitea_token = result['sha1']
u.gitea_uid = gitea_user['id'] u.gitea_uid = gitea_user['id']
if u.save! if u.save!
member_user_exten = member_user_exten["user_extensions"] if platform == "trustie" #trustie上需要 member_user_exten = member_user_exten["user_extensions"] if old_version_source.include?(platform) #trustie上需要
if member_user_exten.present? if member_user_exten.present?
UserExtension.create!(member_user_exten&.except!(*keys_other_delete).merge(user_id: u.id)) UserExtension.create!(member_user_exten&.except!(*keys_other_delete).merge(user_id: u.id))
end end
end end
member = member["member"] if platform == "trustie" #trustie上需要 member = member["member"] if old_version_source.include?(platform) #trustie上需要
if member.present? if member.present?
new_member = Member.new(member&.except!(*member_to_delete).merge(project_id: project_id, user_id: u.id)) new_member = Member.new(member&.except!(*member_to_delete).merge(project_id: project_id, user_id: u.id))
if new_member.save! if new_member.save!
@ -199,9 +213,8 @@ class SyncForgeJob < ApplicationJob
begin begin
if members.present? if members.present?
MemberRole.transaction do MemberRole.transaction do
Rails.logger.info("##########____________members__________________########{members}")
members.each do |m| members.each do |m|
m = m["member_role"] if platform == "trustie" #trustie上需要 m = m["member_role"] if old_version_source.include?(platform) #trustie上需要
if m.present? if m.present?
role_id = Role.select(:id,:position)&.where(position: m["role_id"])&.first&.id 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? MemberRole.create!(m&.except!(*member_role_delete).merge(member_id: member_id, role_id: role_id)) if m.present?
@ -218,7 +231,7 @@ class SyncForgeJob < ApplicationJob
#同步该用户的issues #同步该用户的issues
def sync_user_issues(project_id, new_user_id,old_user_id,issues_all_params,platform) def sync_user_issues(project_id, new_user_id,old_user_id,issues_all_params,platform)
Rails.logger.info("#######______sync_issues_start__##########{issues_all_params}") Rails.logger.info("#######______sync_issues_start__#########")
issue_to_delete = %w(id project_id author_id created_on updated_on assigned_to_id) issue_to_delete = %w(id project_id author_id created_on updated_on assigned_to_id)
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
begin begin
@ -228,7 +241,7 @@ class SyncForgeJob < ApplicationJob
jours_params = is_params["jours_params"] jours_params = is_params["jours_params"]
commit_params = is_params["commit_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.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 platform == "trustie" #trustie上需要 issue_params = issue_params["issue"] if old_version_source.include?(platform) #trustie上需要
if issue_params.present? if issue_params.present?
assgin_user = issue_params["assigned_to_id"] assgin_user = issue_params["assigned_to_id"]
if issue_params["assigned_to_id"].to_i == old_user_id if issue_params["assigned_to_id"].to_i == old_user_id
@ -262,7 +275,7 @@ class SyncForgeJob < ApplicationJob
if jours_params.present? if jours_params.present?
Journal.transaction do Journal.transaction do
jours_params.each do |i| jours_params.each do |i|
i = i["journal"] if platform == "trustie" #trustie上需要 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? Journal.create!(i&.except!(*jour_to_delete).merge(journalized_id: issue_id, user_id: user_id)) if i.present?
end end
end end
@ -285,7 +298,7 @@ class SyncForgeJob < ApplicationJob
if commit_params.present? if commit_params.present?
CommitIssue.transaction do CommitIssue.transaction do
commit_params.each do |i| commit_params.each do |i|
i = i["commit_issues"] if platform == "trustie" #trustie上需要 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? CommitIssue.create!(i&.except!(*commit_to_delete).merge(issue_id: issue_id, project_id: project_id)) if i.present?
end end
end end
@ -308,7 +321,7 @@ class SyncForgeJob < ApplicationJob
if pull_params.present? if pull_params.present?
PullRequest.transaction do PullRequest.transaction do
pull_params.each do |i| pull_params.each do |i|
i = i["pull_request"] if platform == "trustie" #trustie上需要 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? PullRequest.create!(i&.except!(*commit_to_delete).merge(user_id: user_id, project_id: project_id)) if i.present?
end end
end end
@ -330,7 +343,7 @@ class SyncForgeJob < ApplicationJob
if commit_params.present? if commit_params.present?
Commit.transaction do Commit.transaction do
commit_params.each do |i| commit_params.each do |i|
i = i["commit"] if platform == "trustie" #trustie上需要 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? Commit.create!(i&.except!(*commit_to_delete).merge(repository_id: repository_id, project_id: project_id)) if i.present?
end end
end end
@ -352,7 +365,7 @@ class SyncForgeJob < ApplicationJob
if version_params.present? if version_params.present?
Version.transaction do Version.transaction do
version_params.each do |i| version_params.each do |i|
i = i["version"] if platform == "trustie" #trustie上需要 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? Version.create!(i&.except!(*version_to_delete).merge(user_id: new_user_id, project_id: project_id)) if i.present?
end end
end end

View File

@ -0,0 +1,27 @@
class Gitea::User::GetTokenService < Gitea::ClientService
attr_reader :username
def initialize(username)
@username = username
end
def call
params = {}
url = "/users/#{username}".freeze
params = params.merge(data: request_params)
get(url, params)
end
private
# def token_params
# {
# username: username,
# password: password
# }
# end
def request_params
{ username: username }
end
end