#!/usr/bin/ruby
# sudo apt-get install ruby-octokit ruby-colorize

require 'octokit'
require 'colorize'

# my private login credentials
Octokit.configure do |c|
  c.login = 'defunct'
  c.password = 'somePw'
end

# see:
# - https://developer.github.com/v3/pulls/#list-pull-requests
# - https://octokit.github.io/octokit.rb/Octokit
# - https://github.com/fazibear/colorize

client = Octokit::Client.new

rock_core_repos = client.organization_repositories('rock-core')
rock_core_repos.each do |repo|
    # looping over each repo in the given organization. there may be repo with
    # no open pull-requests, these will fail with "Octokkit::NotFound" -- which
    # we ignore
    begin
        pull_requests = client.pull_requests(repo.full_name)
        # if there are a number of open pull-requests we'll tell:
        if pull_requests.size > 0
            puts "got #{pull_requests.size.to_s.blue} PRs in #{repo.full_name.to_s.blue}"
            # name each pull-request with link in the terminal
            pull_requests.each do |pull_request|
                puts " - title:      #{pull_request.title.to_s.green}"
                # the "html_url" is "frozen"... well...
                html_url = pull_request.html_url.dup
                puts "   link:       #{html_url.to_s.yellow}"
                # TODO: do also some fancy date-stuff,
                # "distance_of_time_in_words" for example...
                puts "   created_at: #{pull_request.created_at.strftime("%-d.%-m.%Y").red}"
                if pull_request.body.length > 0
                    puts "   body:       #{pull_request.body}"
                end
            end
            puts "\n"
        end
    rescue Octokit::NotFound
    end
end
