* Update rubocop-todo.yml with new violations * Fix Layout/EmptyLine* rules * Fix Layout/Indentation* rules * Fix remaining Layout/* rules * Fix Lint/DuplicateMethods by removing unused accessor * Fix Lint/IneffectiveAccessModifier * Fix Lint/MissingCopEnableDirective * Re-run rubocop auto gen config * Fix Layout/RescueEnsureAlignment * Fix Naming/* rules * Fix some RSpec/* rules * Fix typos * Fix RSpec/LetBeforeExamples * Series should only be an attr_writer, not an attr_accessor * Fix RSpec/InstanceVariable * Fix RSpec/InstanceVariable * Fix RSpec/RepeatedDescription and RSpec/RepeatedExample * Fix Style/ClassAndModuleChildren * Fix Style/ConditionalAssignment * Fix some Style/* rules * Trigger Travis CI build because failing tests are not failing locally * Revert "Fix Style/ClassAndModuleChildren" This reverts commit 1686801d8a1516ba1894f79e24401a20dea65f99.
54 lines
1.5 KiB
Ruby
54 lines
1.5 KiB
Ruby
class GithubRepo < ApplicationRecord
|
|
belongs_to :user
|
|
|
|
serialize :info_hash, Hash
|
|
validates :name, :url, :github_id_code, presence: true
|
|
validates :url, uniqueness: true
|
|
validates :github_id_code, uniqueness: true
|
|
|
|
after_save :clear_caches
|
|
before_destroy :clear_caches
|
|
|
|
def self.find_or_create(params)
|
|
repo = where(github_id_code: params[:github_id_code]).
|
|
or(where(url: params[:url])).
|
|
first_or_initialize
|
|
repo.update(params)
|
|
repo
|
|
end
|
|
|
|
def self.update_to_latest
|
|
where("updated_at < ?", 1.day.ago).find_each do |repo|
|
|
user_token = User.find_by_id(repo.user_id).identities.where(provider: "github").last.token
|
|
client = Octokit::Client.new(access_token: user_token)
|
|
begin
|
|
fetched_repo = client.repo(repo.info_hash[:full_name])
|
|
repo.update!(
|
|
github_id_code: fetched_repo.id,
|
|
name: fetched_repo.name,
|
|
description: fetched_repo.description,
|
|
language: fetched_repo.language,
|
|
fork: fetched_repo.fork,
|
|
bytes_size: fetched_repo.size,
|
|
watchers_count: fetched_repo.watchers,
|
|
stargazers_count: fetched_repo.stargazers_count,
|
|
info_hash: fetched_repo.to_hash,
|
|
)
|
|
rescue StandardError => e
|
|
repo.destroy if e.message.include?("404 - Not Found")
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def clear_caches
|
|
return if user.blank?
|
|
|
|
user.touch
|
|
cache_buster = CacheBuster.new
|
|
cache_buster.bust user.path
|
|
cache_buster.bust user.path + "?i=i"
|
|
cache_buster.bust user.path + "/?i=i"
|
|
end
|
|
end
|