From 3516cde5553c138c43f5e376e6f3f0602eaf1404 Mon Sep 17 00:00:00 2001 From: "Gan Yi.ang" Date: Sat, 28 Nov 2015 10:25:53 +0800 Subject: [PATCH] =?UTF-8?q?sync=5Fgitlab=5Fuser=E5=8A=A0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/sync_gitlab_user.rake | 39 ++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/tasks/sync_gitlab_user.rake b/lib/tasks/sync_gitlab_user.rake index fb84f5f08..9259353c0 100644 --- a/lib/tasks/sync_gitlab_user.rake +++ b/lib/tasks/sync_gitlab_user.rake @@ -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