kaiho/lib/tasks/repositories.rake

55 lines
1.2 KiB
Ruby
Raw Normal View History

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(
2018-08-04 06:00:12 +00:00
"https://api.github.com/repos/#{repo.name}/tags",
headers
)
)
tags.each do |tag|
2018-08-04 06:00:12 +00:00
next if versions.include?(tag['name'])
date =
JSON.parse(
2018-08-04 06:00:12 +00:00
get_url_content(tag['commit']['url'], headers)
)['committer']['date']
Version.new(
software_id: software.id,
2018-08-04 06:00:12 +00:00
number: tag['name'],
date: date
).save
end
rescue => e
puts e
next
end
end
end
end