# 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.dig('id') attrs[:login] = user_h.dig('login') attrs[:name] = user_h.dig('name') attrs[:avatar_url] = user_h.dig('avatarUrl') attrs[:bio] = user_h.dig('bio') attrs[:bio_html] = user_h.dig('bioHTML') attrs[:location] = user_h.dig('location') attrs[:repositories] = user_h.dig('repositories') attrs 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}" username_attrs = username_hashes.map { |user_h| user_h_to_attr(user_h) } GithubUser.upsert_all(username_attrs) end end end