require 'net/https' namespace :repositories do def get_url_content(url, headers = {}) uri = URI.parse(url) req = Net::HTTP::Get.new(uri) headers.each do |header, value| req[header] = value end Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(req) end.body end desc 'Update version from github repositories' task github: :environment do GithubRepository.all.each do |repo| begin software = Software.find(repo.software_id) versions = software.versions.all.map(&:number) headers = { 'Accept' => 'application/vnd.github.v3+json' } tags = JSON.parse( get_url_content( "https://api.github.com/repos/#{repo.name}/git/refs/tags", headers ) ) tags.each do |tag| version = tag['ref'].split('/').last next if versions.include?(version) puts version date = JSON.parse( get_url_content(tag['object']['url'], headers) )['committer']['date'] Version.new( software_id: software.id, number: version, date: date ).save end rescue => e puts e next end end end end