diff --git a/config/config.rb.example b/config/config.rb.example index 4428d31..0471ac6 100644 --- a/config/config.rb.example +++ b/config/config.rb.example @@ -3,5 +3,6 @@ CONFIG = { remote_path: 'example.com:/srv/httpd/some/path', download_url: 'https://example.com/some/path', - branch: 'user/nick/updates' + branch: 'user/nick/updates', + gh_token: '' }.freeze diff --git a/lib/up2date.rb b/lib/up2date.rb new file mode 100644 index 0000000..f51a89e --- /dev/null +++ b/lib/up2date.rb @@ -0,0 +1,39 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at https://mozilla.org/MPL/2.0/. + +# frozen_string_literal: true + +require 'net/http' +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 + } + } + } + } + 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'] + end +end