Skip to content

Instantly share code, notes, and snippets.

@tra38
Created March 11, 2017 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tra38/299e597ed94c05972a80e885966903a6 to your computer and use it in GitHub Desktop.
Save tra38/299e597ed94c05972a80e885966903a6 to your computer and use it in GitHub Desktop.
"GitHub API Client" Library (built as a part of DBC's Phase 4)
require 'net/http'
require 'json'
# Using this odd stuff to handle factories: http://stackoverflow.com/a/1515631/4765379
# Look like this code will be licensed as GPL for the time being
class GitHub_Request
def self.new(args)
request_uri = args[:request_uri]
personal_authentication_token = args[:personal_authentication_token]
additional_query_strings = args[:additional_query_strings] || ""
parsed_json = grab_json(request_uri, personal_authentication_token, additional_query_strings)
if parsed_json.is_a?(Hash)
GitHub_Element.new(parsed_json, personal_authentication_token)
else
parsed_json.map do |element|
GitHub_Element.new(element, personal_authentication_token)
end
end
end
def self.grab_json(request_uri, personal_authentication_token, additional_query_strings)
response = get_http_query(request_uri, personal_authentication_token, additional_query_strings)
JSON.parse(response)
end
def self.get_http_query(request_uri, personal_authentication_token, additional_query_strings)
uri = URI("#{request_uri}?access_token=#{personal_authentication_token}#{additional_query_strings}")
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Get.new uri
response = http.request request # Net::HTTPResponse object
response.body
end
end
end
class GitHub_Element
def initialize(parsed_json, personal_authentication_token)
parsed_json.each do |key, value|
if /_url/.match(key)
method_name = key.gsub(/_url/, "")
request_uri = value
define_singleton_method(method_name) { GitHub_Request.new(request_uri: request_uri, personal_authentication_token: personal_authentication_token) }
end
define_singleton_method(key) { value }
end
end
end
class User
def self.new(github_user, personal_authentication_token)
request_uri = "https://api.github.com/users/#{github_user}"
GitHub_Request.new(request_uri: request_uri, personal_authentication_token: personal_authentication_token)
end
end
class Repo
def self.new(github_user, repo_name, personal_authentication_token)
request_uri = "https://api.github.com/repos/#{github_user}/#{repo_name}"
GitHub_Request.new(request_uri: request_uri, personal_authentication_token: personal_authentication_token)
end
end
class PullRequests
def self.new(github_user, repo_name, personal_authentication_token)
request_uri = "https://api.github.com/repos/#{github_user}/#{repo_name}/pulls"
array_of_pull_requests = GitHub_Request.new(request_uri: request_uri, personal_authentication_token: personal_authentication_token, additional_query_strings: "&state=all")
array_of_pull_requests.each do |pull_request|
number = pull_request.number
pull_request.define_singleton_method(:files_url) do
"https://api.github.com/repos/#{github_user}/#{repo_name}/pulls/#{number}/files"
end
pull_request.define_singleton_method(:files) do
files_url = pull_request.files_url
GitHub_Request.new(request_uri: files_url, personal_authentication_token: personal_authentication_token)
end
end
array_of_pull_requests
end
end
# Command-Line Tool
require 'net/http'
require 'json'
require_relative 'github-api-classes'
require 'pry'
github_profile = ARGV[0]
if ARGV[1]
repo_name = ARGV[1]
end
if ARGV[2]
pull_request_check = true
end
personal_authentication_token = ENV["API_KEY"]
if github_profile && !repo_name
user = User.new(github_profile, personal_authentication_token)
username = user.name
location = user.location
public_repos = user.public_repos
puts "Name: #{username}"
puts "Location: #{location}"
puts "Public Repos: #{public_repos}"
array_of_repos = user.repos
array_of_repos.sort_by! { |repo| -repo.watchers_count }
array_of_repos.each do |repo|
repo_name = repo.name
repo_watchers = repo.watchers_count
if repo_watchers != 1
watcher_term = "watchers"
else
watcher_term = "watcher"
end
puts "* #{repo_name} (#{repo_watchers} #{watcher_term})"
end
elsif github_profile && repo_name && !pull_request_check
user = User.new(github_profile, personal_authentication_token)
username = user.name
location = user.location
repo = Repo.new(github_profile, repo_name, personal_authentication_token)
events = repo.events
puts "Name: #{user.name}"
puts "Location: #{location}"
puts "Repository: #{repo.name}"
puts "Events: #{events.length}"
events.each do |event|
event_name = event.type.gsub(/Event/, "ed")
user_name = event.actor["display_login"]
puts "* #{event_name} by #{user_name}"
end
elsif github_profile && repo_name && pull_request_check
pull_requests = PullRequests.new(github_profile, repo_name, personal_authentication_token)
pull_requests.each do |request|
id = request.id
title = request.title
body = request.body
files = request.files
state = request.state
puts "#{id} - #{title}"
puts "#{body}"
puts "State - #{state}"
files.each do |file|
file_name = file.filename
file_additions = file.additions
file_deletions = file.deletions
puts "#{file_name}, +#{file_additions}, -#{file_deletions}"
end
if state != "closed"
puts "Do you want to merge this pull request? Y/N"
input = STDIN.gets.chomp
if input == "Y"
number = request.number
uri = URI("https://api.github.com/repos/#{github_profile}/#{repo_name}/pulls/#{number}/merge?access_token=#{personal_authentication_token}")
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') do |http|
request = Net::HTTP::Put.new uri
binding.pry
response = http.request request # Net::HTTPResponse object
puts response.body
end
end
end
end
else
puts "Usage - github-api-client <GITHUB_PROFILE_NAME> <REPO_NAME> <PULL_REQUEST_CHECK_STATUS>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment