修改代码

This commit is contained in:
sylor_huang@126.com 2020-03-17 18:33:47 +08:00
parent 28802cb833
commit f13db9e369
1 changed files with 84 additions and 47 deletions

View File

@ -19,8 +19,7 @@ class SyncForgeJob < ApplicationJob
if new_user.present?
ActiveRecord::Base.transaction do
begin
Watcher&.where(user_id: user_old_id).update_all(user_id: new_user.id)
ProjectTrend&.where(user_id: user_old_id).update_all(user_id: new_user.id)
sync_roles(roles_params, platform)
if all_target_params.present?
all_target_params.each do |project|
@ -39,6 +38,13 @@ class SyncForgeJob < ApplicationJob
sync_projects(new_user, user_old_id,target_params, platform)
end
end
Issue.select(:id, :assigned_to_id).where(assigned_to_id: user_old_id)&.update_all(assigned_to_id: new_user.id)
Issue.select(:id, :author_id).where(author_id: user_old_id)&.update_all(author_id: new_user.id)
Journal.select(:id,:user_id).where(user_id: user_old_id)&.update_all(user_id: new_user.id)
Watcher.select(:id,:user_id).where(user_id: user_old_id)&.update_all(user_id: new_user.id)
ProjectTrend.select(:id,:user_id).where(user_id: user_old_id)&.update_all(user_id: new_user.id)
PullRequest.select(:id,:user_id).where(user_id: user_old_id)&.update_all(user_id: new_user.id)
Version.select(:id,:user_id).where(user_id: user_old_id)&.update_all(user_id: new_user.id)
rescue Exception => e
Rails.logger.info("#######_______forge_new_user_sync_failed___#########{e}")
raise ActiveRecord::Rollback
@ -133,29 +139,36 @@ class SyncForgeJob < ApplicationJob
project_identifier = repo_params["identifier"]
end
new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id))
if new_project.save!(:validate => false)
if project_identifier.present?
repository_params = {
hidden: project["is_public"],
user_id: new_user.id,
identifier: project_identifier
}
Repositories::CreateService.new(new_user, new_project, repository_params).call
if Project.exists?(identifier: project_identifier)
failed_dic = "public/sync_failed_users.dic"
File.open(failed_dic,"a") do |file|
file.puts "[\nTime---#{Time.now}\nproject_info---#{project}\n---]\n "
end
else
new_project = Project.new(project&.except!(*keys_to_delete).merge(user_id: new_user.id))
if new_project.save!(:validate => false)
if project_identifier.present?
repository_params = {
hidden: project["is_public"],
user_id: new_user.id,
identifier: project_identifier
}
Repositories::CreateService.new(new_user, new_project, repository_params).call
end
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))
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, 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
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
end
@ -247,7 +260,7 @@ 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_to_delete = %w(id created_on project_id)
ActiveRecord::Base.transaction do
begin
if members_params.present?
@ -256,7 +269,7 @@ class SyncForgeJob < ApplicationJob
member_user = m["member_user"]
member_user_exten = m["member_extension"]
member_roles = m["member_roles"]
member_issues = m["member_issues"]
# member_issues = m["member_issues"]
if member_user.present?
u = sync_user(member_user,member_user_exten, platform)
if u.present? && member.present?
@ -264,7 +277,7 @@ class SyncForgeJob < ApplicationJob
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!(:validate => false)
sync_user_issues(project_id, u.id, member["user_id"],member_issues, platform)
# sync_user_issues(project_id,member_issues, platform)
sync_member_roles(new_member.id, member_roles,platform)
end
end
@ -304,9 +317,9 @@ class SyncForgeJob < ApplicationJob
end
#同步该用户的issues
def sync_user_issues(project_id, new_user_id,old_user_id,issues_all_params,platform)
def sync_user_issues(project_id, issues_all_params,platform)
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 created_on updated_on)
ActiveRecord::Base.transaction do
begin
if issues_all_params.present?
@ -314,17 +327,11 @@ class SyncForgeJob < ApplicationJob
issue_params = is_params["issue_params"]
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)
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
end
issue = Issue.new(issue_params&.except!(*issue_to_delete).merge(project_id: project_id, author_id: new_user_id, assigned_to_id: assgin_user))
issue = Issue.new(issue_params&.except!(*issue_to_delete).merge(project_id: project_id))
if issue.save!(:validate => false)
sync_journals(new_user_id, issue.id, jours_params, platform)
sync_journals(issue.id, jours_params, platform)
sync_commit_issues(issue.id,project_id, commit_params, platform)
else
Rails.logger.info("############______.errors.full_messages_____________##########{issue.errors.full_messages}")
@ -341,8 +348,8 @@ class SyncForgeJob < ApplicationJob
end
def sync_journals(user_id, issue_id, jours_params,platform)
Rails.logger.info("#######______sync_journals_start__########")
def sync_journals(issue_id, jours_params,platform)
Rails.logger.info("#######______sync_journals_start__#######")
jour_to_delete = %w(id created_on journalized_id)
ActiveRecord::Base.transaction do
@ -350,12 +357,18 @@ class SyncForgeJob < ApplicationJob
if jours_params.present?
Journal.transaction do
jours_params.each do |i|
if i.present?
i = i["journal"] if old_version_source.include?(platform) #trustie上需要
new_journal = Journal.new(i&.except!(*jour_to_delete).merge(journalized_id: issue_id, user_id: user_id))
new_journal.save(:validate => false)
end
new_journal = i["journal"]
new_journal_detail = i["j_details"]
if new_journal.present?
new_journal = new_journal["journal"] if old_version_source.include?(platform) #trustie上需要
new_journal = Journal.new(new_journal&.except!(*jour_to_delete).merge(journalized_id: issue_id))
if new_journal.save(:validate => false)
if new_journal_detail.present?
sync_journal_details(new_journal_detail, new_journal.id, platform)
end
end
end
end
end
end
@ -368,6 +381,30 @@ class SyncForgeJob < ApplicationJob
end
end
def sync_journal_details(jours_params,journal_id, platform)
Rails.logger.info("#######______sync_journal_detail_start__######")
jour_to_delete = %w(id journal_id)
ActiveRecord::Base.transaction do
begin
JournalDetail.transaction do
jours_params.each do |i|
if i.present?
i = i["journal_detail"] if old_version_source.include?(platform) #trustie上需要
new_journal_detail = JournalDetail.new(i&.except!(*jour_to_delete).merge(journal_id: journal_id))
new_journal_detail.save(:validate => false)
end
end
end
Rails.logger.info("#######______sync_journal__detail_end__########")
rescue Exception => e
Rails.logger.info("#######________sync_journal___detail_failed__#########{e}")
raise ActiveRecord::Rollback
end
end
end
def sync_commit_issues(issue_id, project_id, commit_params,platform)
Rails.logger.info("#######______sync_commit_issues_start__########")
@ -398,7 +435,7 @@ class SyncForgeJob < ApplicationJob
def sync_pull_requests(project_id, user_id, pull_params,platform)
Rails.logger.info("#######______sync_project_pull_requests_start__########")
commit_to_delete = %w(id user_id project_id created_at updated_at jenkins_output)
commit_to_delete = %w(id project_id created_at updated_at jenkins_output)
ActiveRecord::Base.transaction do
begin
if pull_params.present?
@ -406,7 +443,7 @@ class SyncForgeJob < ApplicationJob
pull_params.each do |i|
if i.present?
i = i["pull_request"] if old_version_source.include?(platform) #trustie上需要
newpr = PullRequest.new(i&.except!(*commit_to_delete).merge(user_id: user_id, project_id: project_id))
newpr = PullRequest.new(i&.except!(*commit_to_delete).merge(project_id: project_id))
newpr.save(:validate => false)
end
@ -450,7 +487,7 @@ class SyncForgeJob < ApplicationJob
def sync_versions(project_id, new_user_id,version_params,platform)
Rails.logger.info("#######______sync_project_versions_start__########")
version_to_delete = %w(id created_on updated_on project_id user_id)
version_to_delete = %w(id created_on updated_on project_id)
ActiveRecord::Base.transaction do
begin
if version_params.present?
@ -458,8 +495,8 @@ class SyncForgeJob < ApplicationJob
version_params.each do |i|
if i.present?
i = i["version"] if old_version_source.include?(platform) #trustie上需要
new_v = Version.new(i&.except!(*version_to_delete).merge(user_id: new_user_id, project_id: project_id))
new_v.save(:validate => false)
new_v = Version.new(i&.except!(*version_to_delete).merge(project_id: project_id))
new_v.save!(:validate => false)
end
end
end