namespace :batch_add_commits 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 = 65669
    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)
    puts "page==========#{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
