57 lines
1.7 KiB
Ruby
57 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require '/data_importer/lib/github_api/user'
|
|
require '/data_importer/lib/github_api/owner_repos'
|
|
|
|
class GithubUserImporter
|
|
attr_accessor :filepath, :usernames
|
|
|
|
def initialize
|
|
@filepath = '/data_importer/data/github_usernames.txt'
|
|
@usernames = File.read(filepath).split("\n")
|
|
end
|
|
|
|
def username_hashes
|
|
usernames.map do |username|
|
|
Retryable.retryable(tries: 3, on: QueryExecutionError, sleep: ->(n) { 4**n }) do
|
|
username_response = GithubApi::User.find(username)
|
|
username_repos = GithubApi::OwnerRepos.find(username)
|
|
repos_hash = { 'repositories' => username_repos }
|
|
username_response.to_h.merge(repos_hash)
|
|
end
|
|
end
|
|
end
|
|
|
|
def user_h_to_attr(user_h)
|
|
attrs = {}
|
|
attrs[:github_id] = user_h['id']
|
|
attrs[:login] = user_h['login']
|
|
attrs[:name] = user_h['name']
|
|
attrs[:avatar_url] = user_h['avatarUrl']
|
|
attrs[:bio] = user_h['bio']
|
|
attrs[:bio_html] = user_h['bioHTML']
|
|
attrs[:location] = user_h['location']
|
|
attrs[:repositories] = user_h['repositories']
|
|
attrs
|
|
end
|
|
|
|
def bulk_insert(username_hashes)
|
|
GithubUser.bulk_insert do |worker|
|
|
username_hashes.each do |username_hash|
|
|
attrs = user_h_to_attr(username_hash)
|
|
worker.add(attrs)
|
|
end
|
|
end
|
|
end
|
|
|
|
def import
|
|
if filepath.nil?
|
|
puts 'Please provide a filepath in the projects data dir named github_usernames.txt with one username per line.'
|
|
else
|
|
puts 'Now importing GithubUsers'
|
|
usernames = username_hashes.map { |h| h['login'] }
|
|
puts "Now importing data from the following usernames: #{usernames}"
|
|
bulk_insert(username_hashes)
|
|
end
|
|
end
|
|
end
|