Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Github Issue Retriever for a particular user (exercism)
require 'json'
require 'open-uri'
OAUTH_TOKEN = ''
class Github
def auth_token
"access_token="+OAUTH_TOKEN unless OAUTH_TOKEN.empty?
end
end
class User < Github
def initialize(user_name)
@user_name = user_name
end
def repositories
repo_url = "https://api.github.com/users/#{@user_name}/repos?"+auth_token
repo_temp_file = open(repo_url, "Accept" => "application/vnd.github.v3+json")
JSON.parse(File.read(repo_temp_file))
end
end
class Repo < Github
def initialize(full_name)
@full_name = full_name
@page = 1
@issues = {}
end
def issue_url
"https://api.github.com/repos/#{@full_name}/issues?state=open&sort=created&direction=asc&per_page=100&#{auth_token}&page=#{@page}"
end
def load_issues
issue_temp_data = open(issue_url, "Accept" => "application/vnd.github.v3+json") { |f| f.read }
@issues = JSON.parse(issue_temp_data)
@page += 1 if more_pages?
more_pages?
end
def issues
@issues
end
def more_pages?
@issues.length > 0
end
end
class Display
def self.header
puts %w[repo id url title state labels username created_at updated_at].join("\t")
end
def self.issue(json,repo_name)
output = []
output << repo_name
output << json["number"]
output << json["html_url"]
output << json["title"]
output << json["state"]
output << labels(json["labels"])
output << json["user"]["login"]
output << json["created_at"]
output << json["updated_at"]
puts output.join("\t")
end
def self.labels(label_json)
labels = []
if label_json.length > 0
label_json.each { |label| labels << label["name"] }
end
labels.join(', ')
end
end
class IssueFetcher
def initialize(user_name,repo_names,label_names=[])
@user = User.new(user_name)
@repo_names = repo_names
@label_names = label_names
end
def retrieve
@user.repositories.each do |repo|
if keep?(repo) && has_open_issues?(repo)
stream_issues(repo)
end
end
end
def has_open_issues?(repo)
return repo["open_issues"] > 0
end
def keep?(repo)
@repo_names.include? repo['name']
end
def label_match?(label_json)
match = false
if label_json.length > 0
label_json.each do |label|
unless match
match = @label_names.include? label["name"]
end
end
end
match
end
def check_labels?
@label_names.length > 0
end
def display_issue?(label_json)
if check_labels?
label_match?(label_json)
else
true
end
end
def stream_issues(repo)
repository = Repo.new(repo['full_name'])
while repository.load_issues
repository.issues.each do |issue_json|
if display_issue?(issue_json["labels"])
Display.issue(issue_json,repo['name'])
end
end
end
end
end
Display.header
issue_fetcher = IssueFetcher.new('exercism',%w[exercism.io x-api cli],%w[audit/wish])
issue_fetcher.retrieve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.