用户的迁移
This commit is contained in:
parent
3978b14e2a
commit
19d070dd0c
|
@ -0,0 +1,160 @@
|
|||
#project的includes应该是包括(:versions, :project_score, :repository,:commits, issues: [:journals,:commit_issues]),但是repository表,commits表,commit_issues表没有,trustie上有。
|
||||
#
|
||||
# 运行示例: bundle exec rails runner "SyncUserForgeTask.new.call()"
|
||||
|
||||
|
||||
class SyncUserForgeTask
|
||||
def call
|
||||
Rails.logger.info("##########_______sync_start________###########")
|
||||
all_users = User.where("login is not null and id = ?",25728)
|
||||
# 这里再把所有用户循环
|
||||
all_users.find_each do |u|
|
||||
# u = User.find(36390) 测试的
|
||||
user_params = u.as_json
|
||||
user_extension_params = u.try(:user_extensions).as_json || []
|
||||
target_params = []
|
||||
if u.projects.present?
|
||||
projects = u.projects.includes(:versions,:project_score, issues: [:journals],owner: [:user_extensions], members: [roles: [:member_roles]])
|
||||
projects.find_each do |p|
|
||||
member_params = get_members(p.members,p)
|
||||
project_params = p.as_json
|
||||
issue_params = get_issues(p, u.id)
|
||||
versions_params = get_versions(p)
|
||||
project_score_params = get_scores(p)
|
||||
pull_request_params = get_prs(p.id)
|
||||
repo_params = get_repository(p) #repository的内容
|
||||
commit_params = get_commits(p.id)
|
||||
relate_project_params = {
|
||||
project_params: project_params,
|
||||
member_params: member_params,
|
||||
issue_params: issue_params,
|
||||
versions_params: versions_params,
|
||||
project_score_params: project_score_params,
|
||||
pull_request_params: pull_request_params,
|
||||
repo_params: repo_params,
|
||||
commit_params: commit_params
|
||||
}
|
||||
target_params.push(relate_project_params)
|
||||
end
|
||||
end
|
||||
|
||||
roles = Role.all.as_json
|
||||
sync_params = {
|
||||
target_params: target_params,
|
||||
platform: "trustie",
|
||||
roles: roles,
|
||||
user_params: {user_params: user_params, user_extension_params: user_extension_params}
|
||||
}
|
||||
SyncUserForgeJob.new.perform(sync_params.to_json)
|
||||
# Delayed::Job.enqueue(SyncUserForgeJob.new(sync_params.to_json))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#工单的内容
|
||||
def get_issues(project, user_id)
|
||||
issues_params = []
|
||||
issues = project.issues.where(author_id: user_id)
|
||||
if issues.present?
|
||||
issues.find_each do |i|
|
||||
issue_json = i.as_json
|
||||
jours_params = get_issue_journal(i, user_id)
|
||||
commit_params = get_issue_commit(i)
|
||||
issues_params.push(
|
||||
{issue_params: issue_json,
|
||||
jours_params: jours_params,
|
||||
commit_params: commit_params
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
issues_params
|
||||
end
|
||||
|
||||
#里程碑的内容
|
||||
def get_versions(project)
|
||||
versions_params = []
|
||||
versions = project.versions
|
||||
versions.find_each do |i|
|
||||
versions_params.push(i.as_json)
|
||||
end
|
||||
versions_params
|
||||
end
|
||||
|
||||
#project_score的内容
|
||||
def get_scores(project)
|
||||
project.try(:project_score).as_json || []
|
||||
end
|
||||
|
||||
def get_prs(project_id)
|
||||
pr_params = []
|
||||
prs = PullRequest.where(project_id: project_id)
|
||||
prs.find_each do |i|
|
||||
pr_params.push(i.as_json)
|
||||
end
|
||||
pr_params
|
||||
end
|
||||
|
||||
def get_commits(project_id)
|
||||
pr_params = []
|
||||
prs = Commit.where(project_id: project_id)
|
||||
prs.find_each do |i|
|
||||
pr_params.push(i.as_json)
|
||||
end
|
||||
pr_params
|
||||
end
|
||||
|
||||
|
||||
#repository的内容
|
||||
def get_repository(project)
|
||||
repo_params = []
|
||||
repo = project.repository
|
||||
repo_params.push(repo.as_json) if repo.present?
|
||||
repo_params
|
||||
end
|
||||
|
||||
def get_issue_journal(issue, user_id)
|
||||
jour_params = []
|
||||
jours = issue.journals.where(user_id: user_id)
|
||||
if jours.present?
|
||||
jours.find_each do |j|
|
||||
jour_params.push(j.as_json)
|
||||
end
|
||||
end
|
||||
|
||||
jour_params
|
||||
end
|
||||
|
||||
def get_issue_commit(issue)
|
||||
commit_params = []
|
||||
commits = CommitIssues.where(issue_id: issue.id)
|
||||
if commits.present?
|
||||
commits.find_each do |j|
|
||||
commit_params.push(j.as_json)
|
||||
end
|
||||
end
|
||||
commit_params
|
||||
end
|
||||
|
||||
def get_members(members,project)
|
||||
member_params = []
|
||||
if members.present?
|
||||
members.find_each do |m|
|
||||
member_user = m.user.as_json
|
||||
member_extension = m.user.try(:user_extensions).as_json || []
|
||||
member_role = m.member_roles.as_json
|
||||
member_issues = get_issues(project, m.user_id)
|
||||
get_member = {
|
||||
member: m.as_json,
|
||||
member_user: member_user,
|
||||
member_extension: member_extension,
|
||||
member_roles: member_role,
|
||||
member_issues: member_issues
|
||||
}
|
||||
member_params.push(get_member)
|
||||
end
|
||||
end
|
||||
member_params
|
||||
end
|
||||
end
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
|
||||
class SyncUserForgeJob
|
||||
require 'uri'
|
||||
require 'net/http'
|
||||
|
||||
# def initialize(params)
|
||||
# @params = params
|
||||
# end
|
||||
|
||||
def perform(params)
|
||||
Rails.logger.info("#######____sync_user_to_forge_start__########")
|
||||
begin
|
||||
api_host = "#{Redmine::Configuration['sync_forge_url']}"
|
||||
url = "http://127.0.0.1:3000/api/sync_forge"
|
||||
Rails.logger.info("#######_________response__sync__url_____#########{url}")
|
||||
|
||||
sync_json = {
|
||||
"sync_params": params
|
||||
}
|
||||
Rails.logger.info("#######_________response__sync_json_____#########{sync_json}")
|
||||
|
||||
uri = URI.parse(url)
|
||||
if api_host
|
||||
http = Net::HTTP.new(uri.hostname, uri.port)
|
||||
|
||||
if api_host.include?("https://")
|
||||
http.use_ssl = true
|
||||
end
|
||||
|
||||
response = http.send_request('POST', uri.path, sync_json.to_json, {'Content-Type' => 'application/json'})
|
||||
Rails.logger.info("#######_________response__sync__end_____#########{response.body}")
|
||||
end
|
||||
rescue => e
|
||||
Rails.logger.info("#######____sync_user_to_forge_failed__########{e}")
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue