class MakersToolbelt::FetchPullRequests

Constants

CLOSED_SWITCHES
GITHUB_REMOTE_REGEX
OPEN_SWITCHES

Attributes

filter[R]
options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 12
def initialize(options = {})
  @options = options
  @filter = options[:filter] || '--open'
end

Public Instance Methods

include_closed?() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 47
def include_closed?
  CLOSED_SWITCHES.include? filter
end
include_open?() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 43
def include_open?
  OPEN_SWITCHES.include? filter
end
repo() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 32
def repo
  @repo ||= repo_from_remote_path(origin_path)
end
repo_from_remote_path(path) click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 36
def repo_from_remote_path(path)
  @regex ||= GITHUB_REMOTE_REGEX
  @regex.match(path).captures.first
rescue
  fail NotFoundError.new('Could not find remote origin')
end
run() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 17
def run()
  pulls = []
  pulls << open_pull_requests if include_open?
  pulls << closed_pull_requests if include_closed?

  pulls.flatten!.select! {|p| p.head.repo }

  pulls.map { |p| [p.head.repo.owner.login, p.head.repo.html_url] }.each do |name, url|
    puts "adding #{name}"
    `git remote add #{name} #{url}`
    puts "fetching #{url}"
    `git fetch #{name}`
  end
end

Private Instance Methods

client() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 86
def client
  @client ||= create_client
end
closed_pull_requests() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 58
def closed_pull_requests
  puts "fetching closed pull requests from #{repo}..."
  fetch_pull_requests('closed')
end
create_client() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 90
def create_client
  token = ENV['MAKERS_TOOLBELT_GITHUB_TOKEN']
  if token
    Octokit::Client.new access_token: token
  else
    create_client_from_config
  end
end
create_client_from_config() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 99
def create_client_from_config
  print 'Enter your GitHub username: '
  username = STDIN.gets.chomp
  print "Enter GitHub password for #{username}: "
  password = STDIN.noecho(&:gets).chomp
  Octokit::Client.new login: username, password: password
end
fetch_more_pages(into_array) click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 72
def fetch_more_pages(into_array)
  next_page = client.last_response.rels[:next]
  while next_page do
    response = next_page.get
    into_array << response.data
    next_page = response.rels[:next]
  end
  into_array
end
fetch_pull_requests(state) click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 63
def fetch_pull_requests(state)
  pulls = client.pull_requests repo, state: state, per_page: 100
  fetch_more_pages(pulls)
rescue StandardError => e
  puts "Error occurred while fetching pull requests: #{e.message}"
ensure
  pulls
end
open_pull_requests() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 53
def open_pull_requests
  puts "fetching open pull requests from #{repo}..."
  fetch_pull_requests('open')
end
origin_path() click to toggle source
# File lib/makers_toolbelt/fetch_pull_requests.rb, line 82
def origin_path
  @origin_path ||= `git remote show origin | grep 'Fetch URL:'`
end