sync_gitlab_user加验证加日志

This commit is contained in:
Gan Yi.ang 2015-11-28 10:25:53 +08:00
parent e9caf0980a
commit 3516cde555
1 changed files with 31 additions and 8 deletions

View File

@ -6,32 +6,55 @@ namespace :gitlab do
users = User.find_by_sql("select * from users where gid is null") users = User.find_by_sql("select * from users where gid is null")
s = Trustie::Gitlab::Sync.new s = Trustie::Gitlab::Sync.new
g = Gitlab.client 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| 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 begin
us = g.get("/users?search=#{user.mail}") us = g.get("/users?search=#{user.mail}")
puts user.mail
if us.blank? if us.blank?
puts "55555555555555555"
s.sync_user(user) s.sync_user(user)
logger_i.info("sync_remote_user:id=#{user.id},mail=#{user.mail}")
success_cnt+=1
else else
# 解决查询的时候出现多值的情况,比如:123@163.com和g123@163.com
puts "66666666666666666666"
puts user.id
if Array === us if Array === us
us.each do |u| us.each do |u|
if u.email == user.mail if u.email == user.mail
user.gid = u.id user.gid = u.id
user.save # !爆炸方法抛出异常
user.save!
logger_i.info("sync_local_user:#{info}")
success_cnt+=1
end end
end end
end end
end end
rescue => e rescue => e
logger.error("userid=#{user.id},mail=#{user.mail},login=#{user.login},error=#{e}") logger_e.error("#{info},error=#{e}")
puts e puts e
end end
end end
p "同步Gitlab用户结束..."
p "同步成功#{success_cnt},未完成#{error_cnt}"
end end
task :sync_members => :environment do task :sync_members => :environment do