oss-stats 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ # In general, we use octokit, but there are a few places were
6
+ # we cannot, and this handles those cases
7
+ module OssStats
8
+ class GitHubClient
9
+ attr_reader :token
10
+
11
+ def initialize(token)
12
+ @token = token
13
+ @endpoint = 'https://api.github.com'
14
+ end
15
+
16
+ def pr_statuses(pr)
17
+ url = pr['statuses_url']
18
+ return [] unless url
19
+
20
+ uri = URI(url)
21
+ get(uri.path)
22
+ end
23
+
24
+ def recent_prs(org, repo, n = 10)
25
+ prs_path = "/repos/#{org}/#{repo}/pulls"
26
+ pr_query_params = {
27
+ state: 'open', sort: 'updated', direction: 'desc', per_page: n
28
+ }
29
+ pr_uri = URI(prs_path)
30
+ pr_uri.query = URI.encode_www_form(pr_query_params)
31
+ get(pr_uri.path)
32
+ rescue StandardError => e
33
+ log.error("Error fetching PRs for #{repo_url}: #{e.message}")
34
+ []
35
+ end
36
+
37
+ def get(path)
38
+ log.trace("github_api_get: Attempting to parse URI with path: '#{path}'")
39
+ uri = URI("#{@endpoint}#{path}")
40
+ req = Net::HTTP::Get.new(uri)
41
+ req['Authorization'] = "Bearer #{@token}"
42
+ req['Accept'] = 'application/vnd.github+json'
43
+ req['User-Agent'] = 'private-pipeline-checker'
44
+
45
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
46
+ http.request(req)
47
+ end
48
+ unless res.is_a?(Net::HTTPSuccess)
49
+ raise "GitHub API error: #{res.code} #{res.body}"
50
+ end
51
+
52
+ JSON.parse(res.body)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,23 @@
1
+ # looks for :github_token in `options`, falling back to
2
+ # $GITHUB_TOKEN, and then gh's auth.
3
+ def get_github_token(options)
4
+ return options[:github_token] if options[:github_token]
5
+ return ENV['GITHUB_TOKEN'] if ENV['GITHUB_TOKEN']
6
+
7
+ config_path = File.expand_path('~/.config/gh/hosts.yml')
8
+ if File.exist?(config_path)
9
+ config = YAML.load_file(config_path)
10
+ return config.dig('github.com', 'oauth_token')
11
+ end
12
+ nil
13
+ end
14
+
15
+ def get_github_token!(options)
16
+ token = get_github_token(options)
17
+ unless token
18
+ raise ArgumentError,
19
+ 'GitHub token is missing. Please provide a token using ' +
20
+ '--github-token, or set $GITHUB_TOKEN, or run `gh auth login`'
21
+ end
22
+ token
23
+ end
@@ -0,0 +1,25 @@
1
+ require 'mixlib/log'
2
+
3
+ module Mixlib
4
+ module Log
5
+ class Formatter
6
+ def call(severity, _time, _progname, msg)
7
+ if severity == 'INFO'
8
+ "#{msg2str(msg)}\n"
9
+ else
10
+ "#{severity}: #{msg2str(msg)}\n"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module OssStats
18
+ class Log
19
+ extend Mixlib::Log
20
+ end
21
+ end
22
+
23
+ def log
24
+ OssStats::Log
25
+ end