修改迁移时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,61 +8,75 @@ class SyncForgeJob < ApplicationJob
keys_to_delete = %w(id created_on updated_on platform)
keys_other_delete = %w(id created_at updated_at user_id)
ActiveRecord::Base.transaction do
begin
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"]
sync_roles(roles_params, platform)
user_params = user_params["user"] if platform == "trustie" #trustie上需要
if user_params.present?
owner_params = user_params&.except!(*keys_to_delete)
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.exists?(login: owner_params["login"])
new_user = User.find_by(login: owner_params["login"])
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 = 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})
if interactor.success?
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call
new_user.gitea_token = result['sha1']
new_user.gitea_uid = gitea_user['id']
if new_user.save!
owner_extension_params = owner_extension_params["user_extensions"] if platform == "trustie" #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
else
raise Error, "gitea创建用户失败"
end
end
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"]
}
sync_projects(new_user, user_params["id"],target_params, platform)
new_user.gitea_uid = ""
end
end
rescue Exception => e
Rails.logger.info("#######_________user__sync__failed__#########{e}")
raise ActiveRecord::Rollback
if new_user.gitea_uid.present?
result = Gitea::User::GenerateTokenService.new(owner_params["login"], user_password).call
new_user.gitea_token = result['sha1']
end
end
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?
owner_extension_params = owner_extension_params&.except!(*keys_other_delete).merge(user_id: new_user.id)
UserExtension.create!(owner_extension_params)
end
ActiveRecord::Base.transaction do
begin
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"]
}
sync_projects(new_user, user_params["id"],target_params, platform)
end
rescue Exception => e
Rails.logger.info("#######_________user__sync__failed__#########{e}")
raise ActiveRecord::Rollback
end
end
else
Rails.logger.info("############___________________########{owner_params["login"]}创建失败")
end
end
end
def old_version_source
%w(trustie)
end
private
def random_password
@ -83,7 +97,7 @@ class SyncForgeJob < ApplicationJob
version_params = targets[:versions_params]
member_params = targets[:member_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?
new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id))
if new_project.save!
@ -93,7 +107,7 @@ class SyncForgeJob < ApplicationJob
identifier: project["identifier"]
}
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?
ProjectScore.create!(project_score&.except!(*score_to_delete).merge(project_id: new_project.id))
end
@ -120,7 +134,7 @@ class SyncForgeJob < ApplicationJob
if roles.present?
Role.transaction do
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"])
Role.create!(r&.except!(*roles_other_delete)) if r.present?
end
@ -151,7 +165,7 @@ 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 platform == "trustie" #trustie上需要
member_user = member_user["user"] if old_version_source.include?(platform) #trustie上需要
if member_user.present?
unless User.exists?(login: member_user["login"])
@ -164,12 +178,12 @@ class SyncForgeJob < ApplicationJob
u.gitea_token = result['sha1']
u.gitea_uid = gitea_user['id']
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?
UserExtension.create!(member_user_exten&.except!(*keys_other_delete).merge(user_id: u.id))
end
end
member = member["member"] if platform == "trustie" #trustie上需要
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!
@ -199,9 +213,8 @@ class SyncForgeJob < ApplicationJob
begin
if members.present?
MemberRole.transaction do
Rails.logger.info("##########____________members__________________########{members}")
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?
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?
@ -218,7 +231,7 @@ class SyncForgeJob < ApplicationJob
#同步该用户的issues
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)
ActiveRecord::Base.transaction do
begin
@ -228,7 +241,7 @@ 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 platform == "trustie" #trustie上需要
issue_params = issue_params["issue"] if old_version_source.include?(platform) #trustie上需要
if issue_params.present?
assgin_user = issue_params["assigned_to_id"]
if issue_params["assigned_to_id"].to_i == old_user_id
@ -262,7 +275,7 @@ class SyncForgeJob < ApplicationJob
if jours_params.present?
Journal.transaction do
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?
end
end
@ -285,7 +298,7 @@ class SyncForgeJob < ApplicationJob
if commit_params.present?
CommitIssue.transaction do
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?
end
end
@ -308,7 +321,7 @@ class SyncForgeJob < ApplicationJob
if pull_params.present?
PullRequest.transaction do
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?
end
end
@ -330,7 +343,7 @@ class SyncForgeJob < ApplicationJob
if commit_params.present?
Commit.transaction do
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?
end
end
@ -352,7 +365,7 @@ class SyncForgeJob < ApplicationJob
if version_params.present?
Version.transaction do
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?
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