From 6a81f0959d18f9b525558f697754636f9ffc4313 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Wed, 24 May 2023 12:56:55 +0200 Subject: [PATCH] Query packagist for the latest version --- config/config.rb.example | 3 +- lib/up2date.rb | 92 ++++++++++++++++++++++++++++++---------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/config/config.rb.example b/config/config.rb.example index 0471ac6..82a88ac 100644 --- a/config/config.rb.example +++ b/config/config.rb.example @@ -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 diff --git a/lib/up2date.rb b/lib/up2date.rb index f51a89e..f8f15b2 100644 --- a/lib/up2date.rb +++ b/lib/up2date.rb @@ -9,31 +9,79 @@ require 'json' require_relative '../config/config' module SlackBuilder - GITHUB_QUERY = <<~GQL - query ($name: String!, $owner: String!) { - repository(name: $name, owner: $owner) { - refs(last: 1, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: ASC }) { - nodes { - id, - name + # 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) { + refs(last: 1, refPrefix: "refs/tags/", orderBy: { field: TAG_COMMIT_DATE, direction: ASC }) { + nodes { + id, + name + } } } } - } - GQL - private_constant :GITHUB_QUERY + GQL + private_constant :GITHUB_QUERY - # Request the latest tag in the given repository. - def self.github_latest_tag(owner, name) - post_data = { - 'query' => GITHUB_QUERY, - 'variables' => { 'name' => name, 'owner' => owner } - } - uri = URI('https://api.github.com/graphql') - response = Net::HTTP.post uri, post_data.to_json, { - 'content-type' => 'application/json', - 'authorization' => "Bearer #{CONFIG[:gh_token]}" - } - JSON.parse(response.body)['data']['repository']['refs']['nodes'].first['name'] + def initialize(owner, name) + super() + + @owner = owner + @name = name + end + + def latest + post_data = { + 'query' => GITHUB_QUERY, + 'variables' => { 'name' => @name, 'owner' => @owner } + } + uri = URI('https://api.github.com/graphql') + response = Net::HTTP.post uri, post_data.to_json, { + 'content-type' => 'application/json', + 'authorization' => "Bearer #{CONFIG[:gh_token]}" + } + 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