From 8b809fb813c4801eb92ce509d56f21ca3126fced Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 13 Aug 2021 12:21:37 +0800 Subject: [PATCH] add: generate ceshi users --- .gitignore | 1 + Gemfile | 3 + Gemfile.lock | 2 + lib/tasks/produce_and_export_ceshi_user.rake | 61 ++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 lib/tasks/produce_and_export_ceshi_user.rake diff --git a/.gitignore b/.gitignore index d3bbe3094..19dfa07fe 100644 --- a/.gitignore +++ b/.gitignore @@ -86,3 +86,4 @@ redis_data/ Dockerfile dump.rdb .tags* +ceshi_user.xlsx \ No newline at end of file diff --git a/Gemfile b/Gemfile index 713eb860f..88621fdd7 100644 --- a/Gemfile +++ b/Gemfile @@ -128,3 +128,6 @@ gem 'harmonious_dictionary', '~> 0.0.1' gem 'parallel', '~> 1.19', '>= 1.19.1' gem 'letter_avatar' + +# 随机用户名 +gem 'ffaker' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index b1dc2cca7..d0ab70de7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -123,6 +123,7 @@ GEM execjs (2.7.0) faraday (0.15.4) multipart-post (>= 1.2, < 3) + ffaker (2.18.0) ffi (1.12.2) font-awesome-sass (4.7.0) sass (>= 3.2) @@ -452,6 +453,7 @@ DEPENDENCIES diffy enumerize faraday (~> 0.15.4) + ffaker font-awesome-sass (= 4.7.0) grape-entity (~> 0.7.1) groupdate (~> 4.1.0) diff --git a/lib/tasks/produce_and_export_ceshi_user.rake b/lib/tasks/produce_and_export_ceshi_user.rake new file mode 100644 index 000000000..02b132dd3 --- /dev/null +++ b/lib/tasks/produce_and_export_ceshi_user.rake @@ -0,0 +1,61 @@ +namespace :produce_and_export_ceshi_user do + desc "Produce ceshi user and Export to excel" + task call: :environment do + puts "=======Begin=======" + email = FFaker::Internet.unique.email + username = email.split("@")[0] + password = SecureRandom.base64[8..-1] + p = Axlsx::Package.new + p.workbook.add_worksheet(:name => '测试用户') do |sheet| + sheet.add_row ["用户名", "邮箱", "密码", "GiteaToken", "测试仓库", "测试仓库克隆地址"] + (1..100).to_a.each do |i| + while (User.find_by(login: username).present? || User.find_by(mail: email).present?) do + email = FFaker::Internet.unique.email + username = email.split("@")[0] + password = SecureRandom.base64[8..-1] + end + puts "=======Generate:[#{i}] Username: #{username}, Password: #{password}, Email: #{email}=======" + puts "=======Create User Begin====== " + + user = User.new(admin: false, login: username, mail: email, type: "User") + user.password = password + user.platform = 'forge' + user.activate + + next unless user.valid? + + interactor = Gitea::RegisterInteractor.call({username: username, email: email, password: password}) + if interactor.success? + gitea_user = interactor.result + result = Gitea::User::GenerateTokenService.call(username, password) + user.gitea_token = result['sha1'] + user.gitea_uid = gitea_user[:body]['id'] + if user.save! + UserExtension.create!(user_id: user.id) + end + end + + puts "=======Create User End====== " + DCODES = %W(1 2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) + code = DCODES.sample(8).join + project_params = { + user_id: user.id, + name: code, + repository_name: code, + project_category_id: ProjectCategory.pluck(:id).sample, + project_language_id: ProjectLanguage.pluck(:id).sample, + license_id: License.pluck(:id).sample, + ignore_id: Ignore.pluck(:id).sample, + private: true + } + project = Projects::CreateService.new(user, project_params).call + puts project.as_json + sheet.add_row [username, email, password, user.gitea_token, "#{username}/#{code}", project&.repository.url] + end + end + p.use_shared_strings = true + p.serialize('ceshi_user.xlsx') + + puts "=======END=======" + end +end \ No newline at end of file