summaryrefslogtreecommitdiff
path: root/lib/up2date.rb
blob: f51a89eff7dc7e427ab0632cbdaae6af4cadf0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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