kaiho/lib/tasks/repositories.rake
2018-08-04 08:00:12 +02:00

54 lines
1.2 KiB
Ruby

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}/tags",
headers
)
)
tags.each do |tag|
next if versions.include?(tag['name'])
date =
JSON.parse(
get_url_content(tag['commit']['url'], headers)
)['committer']['date']
Version.new(
software_id: software.id,
number: tag['name'],
date: date
).save
end
rescue => e
puts e
next
end
end
end
end