# 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