Query packagist for the latest version
This commit is contained in:
parent
341eafcbf2
commit
6a81f0959d
@ -4,5 +4,6 @@ CONFIG = {
|
||||
remote_path: 'example.com:/srv/httpd/some/path',
|
||||
download_url: 'https://example.com/some/path',
|
||||
branch: 'user/nick/updates',
|
||||
gh_token: ''
|
||||
gh_token: '',
|
||||
repository: '../slackbuilds'
|
||||
}.freeze
|
||||
|
@ -9,6 +9,15 @@ require 'json'
|
||||
require_relative '../config/config'
|
||||
|
||||
module SlackBuilder
|
||||
# Remote repository for a single package.
|
||||
class Repository
|
||||
# Request the latest tag in the given repository.
|
||||
def latest
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
|
||||
class GitHub < Repository
|
||||
GITHUB_QUERY = <<~GQL
|
||||
query ($name: String!, $owner: String!) {
|
||||
repository(name: $name, owner: $owner) {
|
||||
@ -23,11 +32,17 @@ module SlackBuilder
|
||||
GQL
|
||||
private_constant :GITHUB_QUERY
|
||||
|
||||
# Request the latest tag in the given repository.
|
||||
def self.github_latest_tag(owner, name)
|
||||
def initialize(owner, name)
|
||||
super()
|
||||
|
||||
@owner = owner
|
||||
@name = name
|
||||
end
|
||||
|
||||
def latest
|
||||
post_data = {
|
||||
'query' => GITHUB_QUERY,
|
||||
'variables' => { 'name' => name, 'owner' => owner }
|
||||
'variables' => { 'name' => @name, 'owner' => @owner }
|
||||
}
|
||||
uri = URI('https://api.github.com/graphql')
|
||||
response = Net::HTTP.post uri, post_data.to_json, {
|
||||
@ -37,3 +52,36 @@ module SlackBuilder
|
||||
JSON.parse(response.body)['data']['repository']['refs']['nodes'].first['name']
|
||||
end
|
||||
end
|
||||
|
||||
class Packagist < Repository
|
||||
def initialize(vendor, name)
|
||||
super()
|
||||
|
||||
@vendor = vendor
|
||||
@name = name
|
||||
end
|
||||
|
||||
# Request the latest version from the packagist API.
|
||||
def latest
|
||||
full_name = [@vendor, @name].join '/'
|
||||
uri = URI "https://repo.packagist.org/p2/#{full_name}.json"
|
||||
response = Net::HTTP.get uri, {
|
||||
'content-type' => 'application/json'
|
||||
}
|
||||
JSON.parse(response)['packages'][full_name].first['version']
|
||||
end
|
||||
end
|
||||
|
||||
def self.check_for_latest(package_name, repository)
|
||||
package_path = Pathname.new Dir.glob("#{CONFIG[:repository]}/*/#{package_name}").first
|
||||
package_category = package_path.dirname.basename.to_s
|
||||
package = Package.parse("#{package_category}/#{package_name}", File.read("#{package_path}/#{package_name}.info"))
|
||||
latest_version = repository.latest
|
||||
|
||||
if package.version == latest_version
|
||||
puts "#{package_name} is up to date."
|
||||
else
|
||||
puts "#{package_name}: Current version is #{package.version}, #{latest_version} is available."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user