mindspore 最近提交数同步

This commit is contained in:
xxq250 2023-04-19 15:52:33 +08:00
parent a13fc2a96f
commit 48207de001
1 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,60 @@
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 = 0
if ENV['count'].present?
count = ENV['count'].to_i
end
total_count = 3000
puts "total_count==========#{total_count}"
if total_count > 100
total_page = (total_count / 100) + 1
total_page.times do |i|
add_commits_to_project(project, i + 1 + count)
end
else
add_commits_to_project(project, 1)
end
end
def add_commits_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/commits?access_token=96a637aa055f15056e77e3cf11a67525&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}"
data = ""
lists.each do |commit|
# puts "commit==========#{commit}"
commiter = commit['commit']['author']
commit_sha = commit['sha']
next if CommitLog.find_by(commit_id: commit_sha).present?
ref = "master"
commit_message = commit['commit']['message'].to_s.size > 200 ? "Message Data too long" : commit['commit']['message'].to_s.gsub("/n","").gsub("\"","")
user = User.find_by(mail: commiter['email'])
user_id = user&.id || project.user_id
commit_date = Time.parse(commit['commit']['author']['date'])
commit_date_str = commit_date.strftime("%Y-%m-%d %H:%M:%S")
data += "(#{user_id},#{project.id},#{project.repository&.id},'#{project.identifier}','#{project.owner.name}/#{project.identifier}','#{commit_sha}','#{ref}',\"#{commit_message}\",'#{commit_date_str}','#{commit_date_str}'),"
end
data = data[0,data.length-1]
if data.present?
sql_connection = ActiveRecord::Base.connection
sql_connection.begin_db_transaction
sql = "INSERT INTO commit_logs (`user_id`, `project_id`, `repository_id`, `name`, `full_name`, `commit_id`, `ref`, `message`, `created_at`, `updated_at`) VALUES #{data}"
sql_connection.execute(sql)
sql_connection.commit_db_transaction
end
end
end