module RiskSummary

Constants

VERSION

Public Class Methods

cli(argv) click to toggle source
# File lib/risk_summary.rb, line 39
def cli(argv)
  parse_cli_options! argv
  token = ENV["GITHUB_TOKEN"] || token_from_gitconfig
  puts risks(*argv, token)
  0
end

Private Class Methods

http_get(path, token) click to toggle source
# File lib/risk_summary.rb, line 98
def http_get(path, token)
  url = "https://api.github.com#{path}"
  uri = URI(url)
  req = Net::HTTP::Get.new(uri)
  req["Authorization"] = "token #{token}" if token
  req["Accept"] = "application/vnd.github.groot-preview+json" # for /pulls requests
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end
  raise "Bad response #{res.code} from #{url}:\n#{res.body}" unless res.code == "200"
  JSON.parse(res.body, symbolize_names: true)
end
parallel(items, threads:) { |item| ... } click to toggle source
# File lib/risk_summary.rb, line 111
def parallel(items, threads:)
  results = Array.new(items.size)
  items = items.each_with_index.to_a

  Array.new([threads, items.size].min) do
    Thread.new do
      loop do
        item, index = items.pop
        break unless index
        results[index] = yield item
      end
    end
  end.each(&:join)

  results
end
parse_cli_options!(argv) click to toggle source
# File lib/risk_summary.rb, line 65
    def parse_cli_options!(argv)
      parser = OptionParser.new do |p|
        p.banner = <<~BANNER
          Collects Risk section from all merged PRs over a given commit range.
          Your github token needs to be available as `GITHUB_TOKEN` env var or `git config github.token`.

          Usage:
              risk-summary zendesk/samson v3240...v3250

          Options:
        BANNER
        p.on("-h", "--help", "Show this.") do
          puts p
          exit 0
        end
        p.on("-v", "--version", "Show Version") do
          puts VERSION
          exit 0
        end
      end
      parser.parse!(argv)

      if argv.size != 2
        puts parser
        exit 1
      end
    end
risks(repo, diff, token) click to toggle source
# File lib/risk_summary.rb, line 48
def risks(repo, diff, token)
  compare = http_get "/repos/#{repo}/compare/#{diff}", token

  pulls = parallel(compare.fetch(:commits), threads: 10) do |commit|
    http_get "/repos/#{repo}/commits/#{commit.fetch(:sha)}/pulls", token
  end.flatten(1).uniq

  pulls.map do |pull|
    risks = RiskParser.parse(pull.fetch(:body))
    case risks
    when :missing then "- missing risks from [##{pull.fetch(:number)}](#{pull.fetch(:html_url)})"
    when :none then nil
    else risks
    end
  end.compact
end
token_from_gitconfig() click to toggle source
# File lib/risk_summary.rb, line 93
def token_from_gitconfig
  result = `git config github.token`.chomp
  result if $?.success?
end