You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/lib/tasks/gitlab.rake

120 lines
3.7 KiB

6 years ago
require 'trustie/gitlab/sync'
namespace :gitlab do
namespace :sync do
desc "sync users to gitlab"
task :users => :environment do
# User.where(username: 'root').find_each do |user|
s = Trustie::Gitlab::Sync.new
User.find_each do |user|
s.sync_user(user)
end
end
task :members => :environment do
projects = Project.where("gpid is not null")
s = Trustie::Gitlab::Sync.new
g = Gitlab.client
projects.each do |project|
begin
if project.members.count != g.team_members(project.gpid).count
s.only_members(project)
end
rescue => e
puts e
end
end
end
# 检查一个gitlab用户对应多个trustie用户的问题
task :useless_user => :environment do
# projects = Project.where(:id => [6286, 6292, 6293, 6294])
ids = User.find_by_sql("select gid from users where gid is not null group by gid having count(gid) > 1").map(&:gid)
users = User.where(:gid => ids)
g = Gitlab.client
# users = User.where(:gid => 8823)
users.each do |user|
puts "start"+ "#{user.id}"
# 保证用户没有创建过项目、实训及TPI实战
if (Member.where("project_id >0 and user_id =?", user.id).blank? || g.user(user.gid).try(:username) != user.login) && user.id < 11360
puts user.id
user.update_column(:gid, nil)
end
end
end
# 需要查出这样的用户并且删除Gitlab中这样的用户
task :sigle_useless_user => :environment do
users = User.where("gid is not null")
g = Gitlab.client
# users = User.where(:gid => 8823)
users.find_each do |user|
# 没有创建或者加入过项目
if Member.where("project_id >0 and user_id =?", user.id).blank? && g.user(user.gid).try(:username) != user.login
puts user.id
g.delete_user(user.gid)
user.update_column(:gid, nil)
end
end
end
# 检查gitlab中username和trustie不一致的问题
# 不一致的问题有一些是特殊符号导致的,比如:@、.等
task :username => :environment do
# projects = Project.where(:id => [6286, 6292, 6293, 6294])
users = User.where("gid is not null")
g = Gitlab.client
# users = User.where(:gid => 8823)
users.find_each do |user|
if g.user(user.gid).try(:username) != user.login
puts "start"+ "#{user.id}"
g.edit_user(user.gid, :username => user.login)
end
end
end
# 修复邮箱不一致的问题
task :mail => :environment do
users = User.where("gid is not null")
g = Gitlab.client
# users = User.where(:gid => 8823)
users.find_each do |user|
if g.user(user.gid).try(:email) != user.mail
puts "start"+ "#{user.id}"
g.edit_user_mail(user.gid, :email => user.mail)
end
end
end
desc "update user password"
task :password => :environment do
s = Trustie::Gitlab::Sync.new
s.change_password(1,'5188b7a65acf294ee7deceb397b6f9c62214ea50','dcb8d9fffabec60c2d0d1030b679fbbb')
end
desc "sync projects to gitlab"
task :projects => :environment do
s = Trustie::Gitlab::Sync.new
Project.where(id: ENV["PROJECT_ID"]).find_each do |project|
s.sync_project(project, path: ENV["REP_NAME"], import_url: project.repository.url)
end
end
desc "remove all projects"
task :remove_all_projects => :environment do
g = Gitlab.client
100.times do
g.projects(scope: 'all').each do |p|
puts p.id
begin
g.delete_project(p.id)
rescue => e
puts e
end
end
end
end
end
end