新增:组织数据修复任务

This commit is contained in:
yystopf 2022-10-15 11:29:23 +08:00
parent 6383384287
commit 630a7aadeb
2 changed files with 40 additions and 0 deletions

View File

@ -70,4 +70,15 @@ class Team < ApplicationRecord
end
end
def to_gitea_hash
{
can_create_org_repo: self.can_create_org_project,
description: self.description || "",
includes_all_repositories: self.includes_all_project,
name: self.name,
permission: self.authorize,
units: self.team_units.pluck(:unit_type).map{|i| "repo.#{i}"}
}
end
end

View File

@ -4,6 +4,35 @@ desc "初始化数据同步到gitea平台"
# 再同步项目及项目成员
namespace :sync_data_to_gitea do
desc "同步组织数据"
# 同步组织成员,并仅保留最高权限
task organizations: :environment do
Organization.includes(:organization_users, teams: [:team_users, :team_projects]).find_each do |org|
ActiveRecord::Base.transaction do
org.teams.each do |team|
if team.gtid.blank?
gteam = $gitea_client.post_orgs_teams_by_org(org.login, {body: team.to_gitea_hash.to_json}) rescue nil
team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil?
end
team.team_users.each do |teamuser|
userlogin = teamuser&.user&.login
next if ($gitea_client.get_teams_members_by_id_username(team.gtid, userlogin) rescue nil)
tu_result = $gitea_client.put_teams_members_by_id_username(team.gtid, userlogin) rescue nil
raise ActiveRecord::Rollback if tu_result.nil?
end
team.team_projects.each do |teamp|
tp_result = $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, org.login, teamp&.project.identifier) rescue nil
raise ActiveRecord::Rollback if tp_result.nil?
end
end
org.organization_users.each do |user|
next if ($gitea_client.get_orgs_members_by_org_username(org.login, user.login) rescue nil)
user.destroy!
end
end
end
end
desc "同步用户"
task users: :environment do
users = User.where.not(mail: [nil, ""], type: 'Anonymous').or(User.where.not(login: [nil, ""])).distinct