更改: 导入项目数据脚本
This commit is contained in:
parent
d474a421ab
commit
f5f73bfa68
|
@ -1,6 +1,28 @@
|
||||||
|
|
||||||
desc "导入导出issue、version、journal数据"
|
desc "导入导出issue、version、journal数据"
|
||||||
namespace :import_from_another_forge do
|
namespace :import_from_another_forge do
|
||||||
|
|
||||||
|
def find_or_create_by(login)
|
||||||
|
return nil unless login.present?
|
||||||
|
user = User.find_by(login: login)
|
||||||
|
return user if user.present?
|
||||||
|
|
||||||
|
user = User.new(admin: false, login: login, mail: "#{login}@forge.com", nickname: login, platform: 'forge' , type: "User")
|
||||||
|
user.password = "12345678"
|
||||||
|
user.activate
|
||||||
|
interactor = Gitea::RegisterInteractor.call({username: login, email: "#{login}@forge.com", password: "12345678"})
|
||||||
|
gitea_user = interactor.result
|
||||||
|
result = Gitea::User::GenerateTokenService.call(login, "12345678")
|
||||||
|
user.gitea_token = result['sha1']
|
||||||
|
user.gitea_uid = gitea_user[:body]['id']
|
||||||
|
user.save!
|
||||||
|
UserExtension.create!(user_id: user.id)
|
||||||
|
|
||||||
|
return user
|
||||||
|
rescue
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
desc "导出数据"
|
desc "导出数据"
|
||||||
# 执行示例 bundle exec rake "import_from_another_forge:export[110]"
|
# 执行示例 bundle exec rake "import_from_another_forge:export[110]"
|
||||||
# RAILS_ENV=production bundle exec rake "import_from_another_forge:export[110]"
|
# RAILS_ENV=production bundle exec rake "import_from_another_forge:export[110]"
|
||||||
|
@ -31,25 +53,6 @@ namespace :import_from_another_forge do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_or_create_by(login)
|
|
||||||
return nil unless login.present?
|
|
||||||
user = User.find_by(login: login)
|
|
||||||
return user if user.present?
|
|
||||||
|
|
||||||
user = User.new(admin: false, login: login, mail: "#{login}@forge.com", nickname: login, platform: 'forge' , type: "User")
|
|
||||||
user.password = "12345678"
|
|
||||||
user.activate
|
|
||||||
interactor = Gitea::RegisterInteractor.call({username: login, email: "#{login}@forge.com", password: "12345678"})
|
|
||||||
gitea_user = interactor.result
|
|
||||||
result = Gitea::User::GenerateTokenService.call(username, password)
|
|
||||||
user.gitea_token = result['sha1']
|
|
||||||
user.gitea_uid = gitea_user[:body]['id']
|
|
||||||
UserExtension.create!(user_id: user.id) if user.save!
|
|
||||||
|
|
||||||
return user
|
|
||||||
rescue
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
# 执行示例 bundle exec rake "import_from_another_forge:import[filepath, 365, ceshi_org]"
|
# 执行示例 bundle exec rake "import_from_another_forge:import[filepath, 365, ceshi_org]"
|
||||||
# RAILS_ENV=production bundle exec rake "import_from_another_forge:import[public/version_issue_journal_data.xlsx, 110]"
|
# RAILS_ENV=production bundle exec rake "import_from_another_forge:import[public/version_issue_journal_data.xlsx, 110]"
|
||||||
|
@ -114,25 +117,30 @@ namespace :import_from_another_forge do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
issue.fixed_version_id = row[12]
|
issue.fixed_version_id = version_hash["#{row[12]}"]
|
||||||
issue.save!
|
issue.save!
|
||||||
issue_hash["#{row[0]}"] = issue.id
|
issue_hash["#{row[0]}"] = issue.id
|
||||||
|
end
|
||||||
when 'journal'
|
when 'journal'
|
||||||
sheet.rows.each.with_index do |row, index|
|
sheet.rows.each.with_index do |row, index|
|
||||||
next if index == 0
|
next if index == 0
|
||||||
next if row[6].present? || row[8].present?
|
next if row[6].present? || row[8].present?
|
||||||
journal = Journal.new
|
journal = Journal.new
|
||||||
journal.journalized_type = row[1]
|
journal.journalized_type = row[1]
|
||||||
journal.journalized_id = row[2]
|
journal.journalized_id = issue_hash["#{row[2]}"]
|
||||||
journal.user = find_or_create_by(row[3])
|
journal.user = find_or_create_by(row[3])
|
||||||
journal.notes = row[4]
|
journal.notes = row[4]
|
||||||
journal.created_on = row[5]
|
journal.created_on = row[5]
|
||||||
journal.updated_on = row[9]
|
journal.updated_on = row[9]
|
||||||
|
journal.parent_id = journal_hash["#{row[6]}"]
|
||||||
|
journal.reply_id = journal_hash["#{row[8]}"]
|
||||||
journal.save!
|
journal.save!
|
||||||
journal_hash["#{row[0]}"] = journal.id
|
journal_hash["#{row[0]}"] = journal.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue