sync_gitlab_user加验证加日志
This commit is contained in:
parent
e9caf0980a
commit
3516cde555
|
@ -6,32 +6,55 @@ namespace :gitlab do
|
|||
users = User.find_by_sql("select * from users where gid is null")
|
||||
s = Trustie::Gitlab::Sync.new
|
||||
g = Gitlab.client
|
||||
logger = Logger.new('./log/add_gid.log', 'daily') #按天生成
|
||||
logger_i = Logger.new('./log/task_add_gid.log', 'daily')
|
||||
logger_e = Logger.new('./log/task_add_gid_error.log', 'daily')
|
||||
NAMESPACE_REGEX_STR = '(?:[a-zA-Z0-9_\.][a-zA-Z0-9_\-\.]*[a-zA-Z0-9_\-]|[a-zA-Z0-9_])'.freeze
|
||||
MAIL_REGEX_STR ='^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$'.freeze
|
||||
error_cnt=0
|
||||
success_cnt=0
|
||||
p "同步Gitlab用户开始..."
|
||||
users.each do |user|
|
||||
info="userid=#{user.id},mail=#{user.mail},login(username)=#{user.login},name=#{user.show_name}"
|
||||
# username不能有特殊字符,不能有保留字符,邮箱特殊字符(比如‘>’)导致search失败的,name不能为空串
|
||||
if !(user.login =~ /\A#{NAMESPACE_REGEX_STR}\Z/.freeze)
|
||||
logger_e.error("#{info},error=校验失败:username特殊字符")
|
||||
error_cnt+=1
|
||||
next
|
||||
elsif !(user.mail.downcase =~ /#{MAIL_REGEX_STR}/)
|
||||
logger_e.error("#{info},error=校验失败:邮箱特殊字符")
|
||||
error_cnt+=1
|
||||
next
|
||||
elsif user.show_name.lstrip.rstrip.blank?
|
||||
logger_e.error("#{info},error=校验失败:name不正常")
|
||||
error_cnt+=1
|
||||
next
|
||||
end
|
||||
begin
|
||||
us = g.get("/users?search=#{user.mail}")
|
||||
puts user.mail
|
||||
if us.blank?
|
||||
puts "55555555555555555"
|
||||
s.sync_user(user)
|
||||
logger_i.info("sync_remote_user:id=#{user.id},mail=#{user.mail}")
|
||||
success_cnt+=1
|
||||
else
|
||||
# 解决查询的时候出现多值的情况,比如:123@163.com和g123@163.com
|
||||
puts "66666666666666666666"
|
||||
puts user.id
|
||||
if Array === us
|
||||
us.each do |u|
|
||||
if u.email == user.mail
|
||||
user.gid = u.id
|
||||
user.save
|
||||
# !爆炸方法抛出异常
|
||||
user.save!
|
||||
logger_i.info("sync_local_user:#{info}")
|
||||
success_cnt+=1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
rescue => e
|
||||
logger.error("userid=#{user.id},mail=#{user.mail},login=#{user.login},error=#{e}")
|
||||
logger_e.error("#{info},error=#{e}")
|
||||
puts e
|
||||
end
|
||||
end
|
||||
p "同步Gitlab用户结束..."
|
||||
p "同步成功#{success_cnt},未完成#{error_cnt}"
|
||||
end
|
||||
|
||||
task :sync_members => :environment do
|
||||
|
|
Loading…
Reference in New Issue