From 8b14ee3c72b6c810b480792485b771d24b5ad12c Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 26 Oct 2022 14:49:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E7=BB=84=E7=BB=87=E4=B8=8B=E9=A1=B9=E7=9B=AE=E6=9D=83=E9=99=90?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E6=9B=B4=E6=94=B9=E7=BB=84=E7=BB=87=E4=B8=8B?= =?UTF-8?q?=E6=9D=83=E9=99=90=E5=90=8C=E6=AD=A5=E8=87=B3gitea?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/concerns/project_operable.rb | 119 ++++++++++++++++++---- app/services/projects/transfer_service.rb | 1 + lib/tasks/sync_data_to_gitea.rake | 4 +- 3 files changed, 105 insertions(+), 19 deletions(-) diff --git a/app/models/concerns/project_operable.rb b/app/models/concerns/project_operable.rb index ee361d2cf..7a16f25fe 100644 --- a/app/models/concerns/project_operable.rb +++ b/app/models/concerns/project_operable.rb @@ -24,23 +24,62 @@ module ProjectOperable if self.owner.is_a?(Organization) case role_name when 'Manager' + # 构建相应的团队 team = self.owner.teams.admin.take - team = team.nil? ? Team.build(self.user_id, 'admin', '管理员', '', 'admin', false, false) : team - TeamProject.build(self.user_id, team.id, self.id) - OrganizationUser.build(self.user_id, user_id) + if team.nil? + team = Team.build(self.user_id, 'admin', '管理员', '', 'admin', false, false) + gteam = $gitea_client.post_orgs_teams_by_org(self.owner.login, {body: team.to_gitea_hash.to_json}) rescue nil + team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil? + end + + # 设置项目在团队中的访问权限 + team_project = TeamProject.build(self.user_id, team.id, self.id) + tp_result = $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, self.owner.login, self.identifier) rescue nil + + # 新增对应的团队成员 team_user = TeamUser.build(self.user_id, user_id, team.id) + $gitea_client.put_teams_members_by_id_username(team&.gtid, team_user.user&.login) rescue nil # 新增新的 + + # 确保组织成员中有该用户 + OrganizationUser.build(self.user_id, user_id) when 'Developer' + # 构建相应的团队 team = self.owner.teams.write.take - team = team.nil? ? Team.build(self.user_id, 'developer', '开发者', '', 'write', false, false) : team - TeamProject.build(self.user_id, team.id, self.id) - OrganizationUser.build(self.user_id, user_id) + if team.nil? + team = Team.build(self.user_id, 'developer', '开发者', '', 'write', false, false) + gteam = $gitea_client.post_orgs_teams_by_org(self.owner.login, {body: team.to_gitea_hash.to_json}) rescue nil + team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil? + end + + # 设置项目在团队中的访问权限 + team_project = TeamProject.build(self.user_id, team.id, self.id) + tp_result = $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, self.owner.login, self.identifier) rescue nil + + # 新增对应的团队成员 team_user = TeamUser.build(self.user_id, user_id, team.id) + $gitea_client.put_teams_members_by_id_username(team&.gtid, team_user.user&.login) rescue nil # 新增新的 + + # 确保组织成员中有该用户 + OrganizationUser.build(self.user_id, user_id) when 'Reporter' + # 构建相应的团队 team = self.owner.teams.read.take - team = team.nil? ? Team.build(self.user_id, 'reporter', '报告者', '', 'read', false, false) : team - TeamProject.build(self.user_id, team.id, self.id) - OrganizationUser.build(self.user_id, user_id) + if team.nil? + team = Team.build(self.user_id, 'reporter', '报告者', '', 'read', false, false) + gteam = $gitea_client.post_orgs_teams_by_org(self.owner.login, {body: team.to_gitea_hash.to_json}) rescue nil + team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil? + end + + # 设置项目在团队中的访问权限 + team_project = TeamProject.build(self.user_id, team.id, self.id) + tp_result = $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, self.owner.login, self.identifier) rescue nil + + # 新增对应的团队成员 team_user = TeamUser.build(self.user_id, user_id, team.id) + $gitea_client.put_teams_members_by_id_username(team&.gtid, team_user.user&.login) rescue nil # 新增新的 + + # 确保组织成员中有该用户 + OrganizationUser.build(self.user_id, user_id) end end member = members.create!(user_id: user_id, team_user_id: team_user&.id) @@ -71,26 +110,70 @@ module ProjectOperable def change_member_role!(user_id, role) member = self.member(user_id) + # 所有者为组织,并且该用户属于组织成员 if self.owner.is_a?(Organization) && member.team_user.present? case role&.name when 'Manager' + # 构建相应的团队 team = self.owner.teams.admin.take - team = team.nil? ? Team.build(self.user_id, 'admin', '管理员', '', 'admin', false, false) : team - TeamProject.build(self.user_id, team.id, self.id) + if team.nil? + team = Team.build(self.user_id, 'admin', '管理员', '', 'admin', false, false) + gteam = $gitea_client.post_orgs_teams_by_org(self.owner.login, {body: team.to_gitea_hash.to_json}) rescue nil + team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil? + end + + # 设置项目在团队中的访问权限 + team_project = TeamProject.build(self.user_id, team.id, self.id) + tp_result = $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, self.owner.login, self.identifier) rescue nil + + # 更改对应的团队成员 + team_user = member.team_user + $gitea_client.delete_teams_members_by_id_username(team_user.team.gtid, team_user.user&.login) rescue nil # 移除旧的 + $gitea_client.put_teams_members_by_id_username(team&.gtid, team_user.user&.login) rescue nil # 新增新的 + team_user.update_column(:team_id, team.id) + + # 确保组织成员中有该用户 OrganizationUser.build(self.user_id, user_id) - team_user = member.team_user.update(team_id: team&.id) when 'Developer' + # 构建相应的团队 team = self.owner.teams.write.take - team = team.nil? ? Team.build(self.user_id, 'developer', '开发者', '', 'write', false, false) : team - TeamProject.build(self.user_id, team.id, self.id) + if team.nil? + team = Team.build(self.user_id, 'developer', '开发者', '', 'write', false, false) + gteam = $gitea_client.post_orgs_teams_by_org(self.owner.login, {body: team.to_gitea_hash.to_json}) rescue nil + team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil? + end + # 设置项目在团队中的访问权限 + team_project = TeamProject.build(self.user_id, team.id, self.id) + $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, self.owner.login, self.identifier) rescue nil + + # 更改对应的团队成员 + team_user = member.team_user + $gitea_client.delete_teams_members_by_id_username(team_user.team.gtid, team_user.user&.login) rescue nil # 移除旧的 + $gitea_client.put_teams_members_by_id_username(team&.gtid, team_user.user&.login) rescue nil # 新增新的 + team_user.update_column(:team_id, team.id) + OrganizationUser.build(self.user_id, user_id) - team_user = member.team_user.update(team_id: team&.id) when 'Reporter' + # 构建相应的团队 team = self.owner.teams.read.take - team = team.nil? ? Team.build(self.user_id, 'reporter', '报告者', '', 'read', false, false) : team - TeamProject.build(self.user_id, team.id, self.id) + if team.nil? + team = Team.build(self.user_id, 'reporter', '报告者', '', 'read', false, false) + gteam = $gitea_client.post_orgs_teams_by_org(self.owner.login, {body: team.to_gitea_hash.to_json}) rescue nil + team.update_attributes!({gtid: gteam["id"]}) unless gteam.nil? + end + + # 设置项目在团队中的访问权限 + team_project = TeamProject.build(self.user_id, team.id, self.id) + tp_result = $gitea_client.put_teams_repos_by_id_org_repo(team.gtid, self.owner.login, self.identifier) rescue nil + + # 更改对应的团队成员 + team_user = member.team_user + $gitea_client.delete_teams_members_by_id_username(team_user.team.gtid, team_user.user&.login) rescue nil # 移除旧的 + $gitea_client.put_teams_members_by_id_username(team&.gtid, team_user.user&.login) rescue nil # 新增新的 + team_user.update_column(:team_id, team.id) + + # 确保组织成员中有该用户 OrganizationUser.build(self.user_id, user_id) - team_user = member.team_user.update(team_id: team&.id) end end member.member_roles.last.update_attributes!(role: role) diff --git a/app/services/projects/transfer_service.rb b/app/services/projects/transfer_service.rb index 157263962..5df5e9f1e 100644 --- a/app/services/projects/transfer_service.rb +++ b/app/services/projects/transfer_service.rb @@ -24,6 +24,7 @@ class Projects::TransferService < ApplicationService private def update_owner + # 转移项目需要移除原来的权限 project.members.map{|m| m.destroy! if m.user_id == owner.id || project.member(new_owner.id) || (new_owner.is_a?(Organization) && new_owner.is_member?(m.user_id)) } project.set_owner_permission(new_owner) project.update!(user_id: new_owner.id) diff --git a/lib/tasks/sync_data_to_gitea.rake b/lib/tasks/sync_data_to_gitea.rake index 8b8dd0ab0..b2352201b 100644 --- a/lib/tasks/sync_data_to_gitea.rake +++ b/lib/tasks/sync_data_to_gitea.rake @@ -16,8 +16,10 @@ namespace :sync_data_to_gitea do end team.team_users.each do |teamuser| userlogin = teamuser&.user&.login + ou = org.organization_users.find_by(user_id: teamuser.user_id) + OrganizationUser.build(org.id, teamuser.user_id) unless ou.present? 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 + 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|