# actions 下载包
# node go java
namespace :actions_download do

  task go: :environment do
    # 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://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json"
    api_url = "https://testgitea2.trustie.net/actions/go-versions/raw/branch/main/versions-manifest.json"
    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}"
    lists.each do |data|
      version_arr = data['version'].to_s.split(".")
      if version_arr[0].to_i == 1 && version_arr[1].to_i >= 18
        action_node_select  = Action::NodeSelect.find_or_initialize_by(name: "go-version", val: data["version"])
        puts data["version"]
        data['files'].each do |file|
          if file['platform'] == "linux"
            puts "download_url==#{file['download_url']}"
            action_node_select.download_url = file['download_url']
          end
        end
        action_node_select.action_nodes_id=1
        action_node_select.save
      end
    end
  end

  task node: :environment do
    # 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://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json"
    api_url = "https://testgitea2.trustie.net/actions/node-versions/raw/branch/main/versions-manifest.json"
    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}"
    lists.each do |data|
      version_arr = data['version'].to_s.split(".")
      if version_arr[0].to_i >= 16
        puts data["version"]
        action_node_select  = Action::NodeSelect.find_or_initialize_by(name: "node-version", val: data["version"])
        data['files'].each do |file|
          if file['platform'] == "linux"
            puts "download_url==#{file['download_url']}"
            action_node_select.download_url = file['download_url']
          end
        end
        action_node_select.action_nodes_id=2
        action_node_select.save
      end
    end
  end

  task java: :environment do
    # 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://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json"
    [0, 1, 2].each do |page|
      api_url = "https://api.adoptium.net/v3/assets/version/%5B1.0,100.0%5D?project=jdk&vendor=adoptium&heap_size=normal&sort_method=DEFAULT&sort_order=DESC&os=linux&architecture=x64&image_type=jdk&release_type=ga&jvm_impl=hotspot&page_size=20&page=#{page}"
      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}"
      lists.each do |data|
        puts data["release_name"]
        puts "#{data['version_data']['major']}@#{data['version_data']['openjdk_version']}"
        action_node_select  = Action::NodeSelect.find_or_initialize_by(name: "java-version", val: "#{data['version_data']['major']}", val_ext: "#{data['version_data']['openjdk_version']}")
        data['binaries'].each do |file|
          puts "download_url==#{file['package']['link']}"
          action_node_select.download_url = file['package']['link']
        end
        action_node_select.action_nodes_id=5
        action_node_select.save
      end
    end

  end

end