Query packagist for the latest version

This commit is contained in:
Eugen Wissner 2023-05-24 12:56:55 +02:00
parent 341eafcbf2
commit 6a81f0959d
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 72 additions and 23 deletions

View File

@ -4,5 +4,6 @@ CONFIG = {
remote_path: 'example.com:/srv/httpd/some/path', remote_path: 'example.com:/srv/httpd/some/path',
download_url: 'https://example.com/some/path', download_url: 'https://example.com/some/path',
branch: 'user/nick/updates', branch: 'user/nick/updates',
gh_token: '' gh_token: '',
repository: '../slackbuilds'
}.freeze }.freeze

View File

@ -9,6 +9,15 @@ require 'json'
require_relative '../config/config' require_relative '../config/config'
module SlackBuilder 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 GITHUB_QUERY = <<~GQL
query ($name: String!, $owner: String!) { query ($name: String!, $owner: String!) {
repository(name: $name, owner: $owner) { repository(name: $name, owner: $owner) {
@ -23,11 +32,17 @@ module SlackBuilder
GQL GQL
private_constant :GITHUB_QUERY private_constant :GITHUB_QUERY
# Request the latest tag in the given repository. def initialize(owner, name)
def self.github_latest_tag(owner, name) super()
@owner = owner
@name = name
end
def latest
post_data = { post_data = {
'query' => GITHUB_QUERY, 'query' => GITHUB_QUERY,
'variables' => { 'name' => name, 'owner' => owner } 'variables' => { 'name' => @name, 'owner' => @owner }
} }
uri = URI('https://api.github.com/graphql') uri = URI('https://api.github.com/graphql')
response = Net::HTTP.post uri, post_data.to_json, { response = Net::HTTP.post uri, post_data.to_json, {
@ -36,4 +51,37 @@ module SlackBuilder
} }
JSON.parse(response.body)['data']['repository']['refs']['nodes'].first['name'] JSON.parse(response.body)['data']['repository']['refs']['nodes'].first['name']
end 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 end