导入gitee issue

This commit is contained in:
xxq250 2023-03-22 16:05:31 +08:00
parent 8c1cc9ace8
commit 7cc0dfd08d
1 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,128 @@
namespace :batch_add_issues do
desc "batch_add_issues"
task gitee: :environment do
project_id = ENV['project_id']
puts "project_id=================#{project_id}"
next if project_id.blank?
project = Project.find project_id
# count = 100000
# if ENV['count'].present?
# count = ENV['count']
# end
total_count = 2066 + 499 + 16675 + 451
puts "total_count==========#{total_count}"
if total_count > 100
total_page = (total_count / 100) + 1
total_page.times do |i|
add_issues_to_project(project, i + 1)
end
else
add_issues_to_project(project, 1)
end
end
def add_issues_to_project(project,page)
# curl -X GET --header 'Content-Type: application/json;charset=UTF-8' 'https://gitee.com/api/v5/repos/mindspore/mindspore/issues?access_token=5ccebd935915fb6cfcae634b161047a2&state=open&sort=created&direction=desc&page=1&per_page=10'
api_url = "https://gitee.com/api/v5/repos/mindspore/mindspore/issues?access_token=5ccebd935915fb6cfcae634b161047a2&state=all&sort=created&direction=desc&page=#{page}&per_page=100"
uri = URI.parse(api_url)
response = Net::HTTP.get_response(uri)
puts "gitee api response.code ===== #{response.code}"
lists = JSON.parse(response.body)
puts "lists.size =====#{lists.size}"
# "1" => "新增",
# "2" => "正在解决",
# "3" => "已解决",
# "5" => "关闭",
# "6" => "拒绝"
# Issue的状态: open开启的, progressing(进行中), closed关闭的, rejected拒绝的。 默认: open
lists.each do |issue|
created_issue = Issue.find_by(project_id: project.id, subject: issue['title'])
unless created_issue.present?
priority = [1, 2, 3, 4].include?(issue['priority'].to_i) ? issue['priority'].to_i : 2
issue_status = ["", "open", "progressing", "", "", "closed", "rejected"].index(issue['state'])
issue_status = 1 if issue_status.nil?
user_login = issue['user']['login']
issue_created_at = issue['created_at']
issue_updated_at = issue['updated_at']
user = User.find_by(login: user_login)
unless user.present?
username = user_login
email = "#{username}@gitlink.org.cn"
phone = ""
password = "a12345678"
user = User.new(nickname: user_login, login: username, mail: email, password: password, type: 'User', phone: phone)
interactor = Gitea::RegisterInteractor.call({ username: username, email: email, password: password })
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.call(username, password)
user.gitea_token = result['sha1']
user.gitea_uid = gitea_user[:body]['id']
user.created_on = issue_created_at
user.updated_on = issue_created_at
user.is_test = true
user.save!
UserExtension.create!(user_id: user.id)
puts "import_user batch success: phone #{phone} email: #{email}"
end
issue_params = {
:status_id => issue_status,
:priority_id => priority,
# :milestone_id,
# :branch_name,
# :start_date,
# :due_date,
:subject => issue['title'],
:description => issue['body'],
# :blockchain_token_num,
:issue_tag_ids => [],
:assigner_ids => [],
:attachment_ids => [],
:receivers_login => []
}
created_issue = Api::V1::Issues::CreateService.call(project, issue_params, user)
created_issue.update_columns(created_on: issue_created_at, updated_on: issue_updated_at)
end
issue_number = issue['number']
comment_api_url = "https://gitee.com/api/v5/repos/mindspore/mindspore/issues/#{issue_number}/comments?access_token=5ccebd935915fb6cfcae634b161047a2&page=1&per_page=100&order=asc"
comment_uri = URI.parse(comment_api_url)
comment_response = Net::HTTP.get_response(comment_uri)
comment_lists = JSON.parse(comment_response.body)
comment_lists.each do |comment|
next if Journal.find_by(journalized_id: created_issue.id, journalized_type: 'Issue', notes: comment['body']).present?
user_login = comment['user']['login']
comment_created_at = comment['created_at']
comment_updated_at = comment['updated_at']
comment_user = User.find_by(login: user_login)
unless comment_user.present?
username = user_login
email = "#{username}@gitlink.org.cn"
phone = ""
password = "a12345678"
comment_user = User.new(nickname: user_login, login: username, mail: email, password: password, type: 'User', phone: phone)
interactor = Gitea::RegisterInteractor.call({ username: username, email: email, password: password })
gitea_user = interactor.result
result = Gitea::User::GenerateTokenService.call(username, password)
comment_user.gitea_token = result['sha1']
comment_user.gitea_uid = gitea_user[:body]['id']
comment_user.created_on = comment_created_at
comment_user.updated_on = comment_created_at
comment_user.save!
UserExtension.create!(user_id: comment_user.id)
end
journal_params = {:notes => comment['body'],
:attachment_ids => [],
:receivers_login => []}
object_result = Api::V1::Issues::Journals::CreateService.call(created_issue, journal_params, comment_user)
object_result.update_columns(created_on: comment_created_at, updated_on: comment_updated_at)
end
end
end
end