199 lines
5.9 KiB
Ruby
199 lines
5.9 KiB
Ruby
#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 mail is not null and type = ?", "User")
|
||
# 这里再把所有用户循环
|
||
all_users.each do |u|
|
||
user_params = u.as_json
|
||
user_extension_params = u.try(:user_extensions).as_json || []
|
||
target_params = []
|
||
user_projects = Project.includes(:versions,:repository,:project_score, issues: [journals: :details],owner: [:user_extensions], members: [roles: [:member_roles]]).where(user_id: u.id)
|
||
|
||
if user_projects.present?
|
||
user_projects.each do |p|
|
||
identifier = p.try(:identifier)
|
||
if p.repository.present? && p.repository.try(:identifier).present?
|
||
identifier = p.repository.try(:identifier)
|
||
end
|
||
member_params = get_members(p.members,p)
|
||
watchers_params = get_watchers(p.watchers)
|
||
praise_trends_params = get_praises(p.id)
|
||
issue_params = get_issues(p, u.id)
|
||
project_params = p.as_json
|
||
project_params["project"]["identifier"] = identifier
|
||
project_params["project"]["watchers_count"] = watchers_params.size
|
||
project_params["project"]["issues_count"] = issue_params.size
|
||
project_params["project"]["praises_count"] = praise_trends_params.size
|
||
|
||
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,
|
||
watchers_params: watchers_params,
|
||
praise_trends_params: praise_trends_params
|
||
}
|
||
target_params.push(relate_project_params)
|
||
end
|
||
end
|
||
|
||
roles = Role.all.as_json
|
||
sync_params = {
|
||
user_params: {user_params: user_params, user_extension_params: user_extension_params},
|
||
target_params: target_params,
|
||
platform: "trustie",
|
||
roles: roles
|
||
|
||
}
|
||
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.includes(:journals)
|
||
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)
|
||
project.try(:repository).as_json || []
|
||
# 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
|
||
if jours.present?
|
||
jours.find_each do |j|
|
||
j_json = j.as_json
|
||
j_details = j.details.as_json
|
||
j_params = {journal: j_json, j_details: j_details}
|
||
jour_params.push(j_params)
|
||
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.includes(:member_roles,user: :user_extensions).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
|
||
|
||
def get_watchers(watchers)
|
||
watcher_params = []
|
||
if watchers.present?
|
||
watchers.find_each do |m|
|
||
watcher_params.push(m.as_json)
|
||
end
|
||
end
|
||
watcher_params
|
||
end
|
||
|
||
def get_praises(project_id)
|
||
prasie_params = []
|
||
praise_trends = PraiseTread.where(praise_tread_object_id: project_id, praise_tread_object_type: "Project")
|
||
if praise_trends.present?
|
||
praise_trends.find_each do |m|
|
||
prasie_params.push(m.as_json)
|
||
end
|
||
end
|
||
prasie_params
|
||
end
|
||
end |