diff --git a/app/models/team.rb b/app/models/team.rb index b831cd069..eacb96a0d 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -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 diff --git a/lib/tasks/sync_data_to_gitea.rake b/lib/tasks/sync_data_to_gitea.rake index 85df9c69a..8b8dd0ab0 100644 --- a/lib/tasks/sync_data_to_gitea.rake +++ b/lib/tasks/sync_data_to_gitea.rake @@ -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