forgeplus/app/tasks/check_gitea_user.rb

42 lines
1.9 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class CheckGiteaUser
# 运行示例: 检查哪些用户的gitea不存在bundle exec rails runner "CheckGiteaUser.new.call()"
def call
SyncLog.sync_log("=====begin to check gitea_user======")
all_users = User.where(type: "User", gitea_token: [nil, ""], gitea_uid: [nil, ""])
if all_users.present?
new_password = "12345678"
# EMAIL_REGEX = /^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9_\-.]+(\.[a-zA-Z0-9_-]+)+$/i
all_users.each do |user|
begin
SyncLog.sync_log("=====check_user_login_is:#{user.login}======")
user_mail = user&.mail.present? ? user.mail : "#{user.login}@example.com"
# unless user_mail.match(EMAIL_REGEX).present?
# user_mail = "#{user.login}@example.com"
# end
ActiveRecord::Base.transaction do
interactor = Gitea::RegisterInteractor.call({username: user.login, email: user_mail, password: new_password})
if interactor.success?
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.new(user.login, new_password).call
user.gitea_token = result['sha1']
user.gitea_uid = gitea_user['id']
if user.save!
SyncLog.sync_log("=================create_gitea_user_success_login==#{user.login}")
else
SyncLog.sync_log("=================create_gitea_user_success_login==#{user.login}")
end
else
SyncLog.sync_log("=============sync_to_user_failed,user_login====#{user.login}")
end
end
rescue => exception
SyncLog.sync_log("=================create_gitea_user_has_erros=#{user.login}===#{exception}")
end
end
end
SyncLog.sync_log("=====end_to_check_gitea_user=====")
end
end