class GithubGrep

Constants

VERSION

Public Class Methods

new(token) click to toggle source
# File lib/github_grep.rb, line 9
def initialize(token)
  @token = token
end

Public Instance Methods

Private Instance Methods

all_pages(url, per_page:, **kwargs) { |items| ... } click to toggle source
# File lib/github_grep.rb, line 49
def all_pages(url, per_page:, **kwargs)
  page = 1
  connector = (url.include?("?") ? "&" : "?")
  loop do
    response = request_json("#{url}#{connector}per_page=#{per_page}&page=#{page}", **kwargs)
    hash = response.is_a?(Hash)
    if page == 1 && hash && total = response["total_count"]
      $stderr.puts "Found #{total}"
    else
      $stderr.puts "Page #{page}"
    end

    items = (hash ? response.fetch('items') : response)
    yield items

    break if items.size < per_page
    page += 1
  end
end
code_items_to_lines(items) click to toggle source
# File lib/github_grep.rb, line 31
def code_items_to_lines(items)
  items.flat_map do |item|
    file = item.fetch('repository').fetch('name') + ":" + item.fetch('path')
    lines(item).map { |l| "#{file}: #{l}" }
  end
end
issue_items_to_lines(items) click to toggle source
# File lib/github_grep.rb, line 38
def issue_items_to_lines(items)
  items.flat_map do |item|
    number = item.fetch("number")
    lines(item).map { |l| "##{number}: #{l}" }
  end
end
lines(item) click to toggle source
# File lib/github_grep.rb, line 45
def lines(item)
  item.fetch("text_matches").flat_map { |match| match.fetch('fragment').split("\n") }
end
request_json(url, argv: []) click to toggle source
# File lib/github_grep.rb, line 69
def request_json(url, argv: [])
  # NOTE: github returns a 403 with a Retry-After: 60 on page 3+ ... talking with support atm but might have to handle it
  command = ["curl", "-sSfv", "-H", "Authorization: token #{@token}", *argv, url]

  out, err, status = Open3.capture3(*command)

  # 403 Abuse rate limit often has no Retry-After
  retry_after = err[/Retry-After: (\d+)/, 1]
  abuse_limit = err.include?("returned error: 403")
  if retry_after || abuse_limit
    retry_after ||= "20"
    warn "Sleeping #{retry_after} to avoid abuse rate-limit"
    sleep Integer(retry_after)
    out, err, status = Open3.capture3(*command)
  end

  raise "ERROR Request failed\n#{url}\n#{err}\n#{out}" unless status.success?

  JSON.parse(out)
end