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