From 630a7aadeb8f8681155b316ff8575627f9ac1098 Mon Sep 17 00:00:00 2001 From: yystopf Date: Sat, 15 Oct 2022 11:29:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E7=BB=84=E7=BB=87?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/team.rb | 11 +++++++++++ lib/tasks/sync_data_to_gitea.rake | 29 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) 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