Class: OssStats::GitHubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/oss_stats/github_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ GitHubClient

Returns a new instance of GitHubClient.



11
12
13
14
# File 'lib/oss_stats/github_client.rb', line 11

def initialize(token)
  @token = token
  @endpoint = 'https://api.github.com'
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/oss_stats/github_client.rb', line 9

def token
  @token
end

Instance Method Details

#get(path) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oss_stats/github_client.rb', line 37

def get(path)
  log.trace("github_api_get: Attempting to parse URI with path: '#{path}'")
  uri = URI("#{@endpoint}#{path}")
  req = Net::HTTP::Get.new(uri)
  req['Authorization'] = "Bearer #{@token}"
  req['Accept'] = 'application/vnd.github+json'
  req['User-Agent'] = 'private-pipeline-checker'

  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end
  unless res.is_a?(Net::HTTPSuccess)
    raise "GitHub API error: #{res.code} #{res.body}"
  end

  JSON.parse(res.body)
end

#pr_statuses(pr) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/oss_stats/github_client.rb', line 16

def pr_statuses(pr)
  url = pr['statuses_url']
  return [] unless url

  uri = URI(url)
  get(uri.path)
end

#recent_prs(org, repo, n = 10) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oss_stats/github_client.rb', line 24

def recent_prs(org, repo, n = 10)
  prs_path = "/repos/#{org}/#{repo}/pulls"
  pr_query_params = {
    state: 'open', sort: 'updated', direction: 'desc', per_page: n
  }
  pr_uri = URI(prs_path)
  pr_uri.query = URI.encode_www_form(pr_query_params)
  get(pr_uri.path)
rescue StandardError => e
  log.error("Error fetching PRs for #{repo_url}: #{e.message}")
  []
end