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
|
|
|
|
|
2018-08-05 10:37:35 +00:00
|
|
|
def branch(software_id, branches, version)
|
|
|
|
name = version.match(/^v?(?<branch>\d+\.\d+)(\.\d+)?$/)[:branch]
|
|
|
|
|
|
|
|
return branches[name] if branches.include?(name)
|
|
|
|
|
|
|
|
branch = Branch.new(
|
|
|
|
name: name,
|
|
|
|
software_id: software_id
|
|
|
|
)
|
|
|
|
branch.save
|
|
|
|
branches[name] = branch.id
|
|
|
|
branch.id
|
|
|
|
end
|
|
|
|
|
2018-07-30 12:08:02 +00:00
|
|
|
desc 'Update version from github repositories'
|
|
|
|
task github: :environment do
|
2018-08-04 06:19:58 +00:00
|
|
|
headers = {
|
|
|
|
'Accept' => 'application/vnd.github.v3+json'
|
|
|
|
}
|
|
|
|
headers['Authorization'] = "token #{Figaro.env.github_token}" if Figaro.env.github_token
|
|
|
|
|
2018-07-30 12:08:02 +00:00
|
|
|
GithubRepository.all.each do |repo|
|
|
|
|
begin
|
|
|
|
software = Software.find(repo.software_id)
|
2018-08-05 10:37:35 +00:00
|
|
|
branches = software.branches.all.map { |b| [b.name, b.id] }.to_h
|
|
|
|
versions = software.branches.all.map { |b| b.versions.all.map(&:number) }
|
2018-07-30 12:08:02 +00:00
|
|
|
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
|
|
|
|
2018-08-05 10:37:35 +00:00
|
|
|
branch_id = branch(software.id, branches, 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-08-05 10:37:35 +00:00
|
|
|
)['commit']['committer']['date']
|
2018-07-30 12:08:02 +00:00
|
|
|
|
2018-08-05 10:37:35 +00:00
|
|
|
puts Version.new(
|
|
|
|
branch_id: branch_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
|
2018-08-05 10:37:35 +00:00
|
|
|
puts e.strace
|
2018-07-30 12:08:02 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|