<commit_before>class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :repository_subscriptions
  has_many :api_keys
  has_many :repository_permissions
  has_many :adminable_repository_permissions, -> { where admin: true }, class: RepositoryPermission
  has_many :adminable_github_repositories, through: :adminable_repository_permissions, source: :github_repository
  has_many :github_repositories, primary_key: :uid, foreign_key: :owner_id
  has_many :source_github_repositories, -> { where fork: false }, class: GithubRepository, primary_key: :github_id, foreign_key: :owner_id
  has_many :dependencies, through: :source_github_repositories
  has_many :favourite_projects, -> { group('projects.id').order("COUNT(projects.id) DESC") }, through: :dependencies, source: :project
  has_one :github_user, primary_key: :uid, foreign_key: :github_id

  after_commit :create_api_key, :ping_andrew, :download_orgs, :update_repo_permissions_async, on: :create

  def admin?
    ['andrew', 'barisbalic', 'malditogeek', 'olizilla', 'thattommyhall', 'zachinglis'].include?(nickname)
  end

  def ping_andrew
    PushoverNewUserWorker.perform_async(self.id)
  end

  def create_api_key
    api_keys.create
  end

  def api_key
    api_keys.first.try(:access_token)
  end

  def self.create_from_auth_hash(hash)
    create!(AuthHash.new(hash).user_info)
  end

  def assign_from_auth_hash(hash)
    update_attributes(AuthHash.new(hash).user_info)
  end

  def self.find_by_auth_hash(hash)
    conditions = AuthHash.new(hash).user_info.slice(:provider, :uid)
    where(conditions).first
  end

  def token
    private_repo_token.presence || public_repo_token.presence || read_attribute(:token)
  end

  def github_client
    AuthToken.new_client(token)
  end

  def avatar_url(size = 60)
    "https://avatars.githubusercontent.com/u/#{uid}?size=#{size}"
  end

  def github_url
    "https://github.com/#{nickname}"
  end

  def update_repo_permissions_async
    SyncPermissionsWorker.perform_async(self.id)
  end

  def update_repo_permissions
    r = github_client.repos

    current_repo_ids = []
    r.each do |repo|
      github_repo = GithubRepository.find_by_full_name(repo.full_name)
      begin
        github_repo = GithubRepository.create_from_github(repo.full_name, token) if github_repo.nil?
        current_repo_ids << github_repo.id
      rescue Octokit::Unauthorized, Octokit::RepositoryUnavailable, Octokit::NotFound, Octokit::Forbidden, Octokit::InternalServerError, Octokit::BadGateway => e
        nil
      end

      rp = repository_permissions.find_or_initialize_by(github_repository: github_repo)
      rp.admin = repo.permissions.admin
      rp.push = repo.permissions.push
      rp.pull = repo.permissions.pull<commit_msg>Better handling of repo download errors<commit_after>class User < ActiveRecord::Base
  has_many :subscriptions
  has_many :repository_subscriptions
  has_many :api_keys
  has_many :repository_permissions
  has_many :adminable_repository_permissions, -> { where admin: true }, class: RepositoryPermission
  has_many :adminable_github_repositories, through: :adminable_repository_permissions, source: :github_repository
  has_many :github_repositories, primary_key: :uid, foreign_key: :owner_id
  has_many :source_github_repositories, -> { where fork: false }, class: GithubRepository, primary_key: :github_id, foreign_key: :owner_id
  has_many :dependencies, through: :source_github_repositories
  has_many :favourite_projects, -> { group('projects.id').order("COUNT(projects.id) DESC") }, through: :dependencies, source: :project
  has_one :github_user, primary_key: :uid, foreign_key: :github_id

  after_commit :create_api_key, :ping_andrew, :download_orgs, :update_repo_permissions_async, on: :create

  def admin?
    ['andrew', 'barisbalic', 'malditogeek', 'olizilla', 'thattommyhall', 'zachinglis'].include?(nickname)
  end

  def ping_andrew
    PushoverNewUserWorker.perform_async(self.id)
  end

  def create_api_key
    api_keys.create
  end

  def api_key
    api_keys.first.try(:access_token)
  end

  def self.create_from_auth_hash(hash)
    create!(AuthHash.new(hash).user_info)
  end

  def assign_from_auth_hash(hash)
    update_attributes(AuthHash.new(hash).user_info)
  end

  def self.find_by_auth_hash(hash)
    conditions = AuthHash.new(hash).user_info.slice(:provider, :uid)
    where(conditions).first
  end

  def token
    private_repo_token.presence || public_repo_token.presence || read_attribute(:token)
  end

  def github_client
    AuthToken.new_client(token)
  end

  def avatar_url(size = 60)
    "https://avatars.githubusercontent.com/u/#{uid}?size=#{size}"
  end

  def github_url
    "https://github.com/#{nickname}"
  end

  def update_repo_permissions_async
    SyncPermissionsWorker.perform_async(self.id)
  end

  def update_repo_permissions
    r = github_client.repos

    current_repo_ids = []
    r.each do |repo|
      github_repo = GithubRepository.find_by_full_name(repo.full_name)

      github_repo = GithubRepository.create_from_github(repo.full_name, token) if github_repo.nil?
      current_repo_ids << github_repo.id unless github_repo.nil?

      rp = repository_permissions.find_or_initialize_by(github_repository: github_repo)
      rp.admin = repo.permissions.admin
      rp.push = repo.permissions.push
      rp.pull = repo.permissions.pull
{'fraction_non_alphanumeric': 0.09747427502338635, 'fraction_numerical': 0.0007483629560336763, 'mean_word_length': 4.926829268292683, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5586520', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1743, 'n_words': 319}