Resolve GitHubRepo issue (#323)
* Change github repo's columen default * Update GithubRepo::update_to_latest * Fix failing spec
This commit is contained in:
parent
545f0acd8c
commit
75d1a89c7a
4 changed files with 12 additions and 27 deletions
|
|
@ -1,6 +1,7 @@
|
|||
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
|
||||
|
|
@ -16,21 +17,10 @@ class GithubRepo < ApplicationRecord
|
|||
end
|
||||
|
||||
def self.update_to_latest
|
||||
# TODO: this is a very intensive process. Definitely not a good approach on the long run.
|
||||
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)
|
||||
|
||||
fetched_repo = if repo[:github_id_code]
|
||||
client.repositories.select do |fresh_repo|
|
||||
fresh_repo.id == repo[:github_id_code]
|
||||
end.first
|
||||
else
|
||||
client.repositories.select do |fresh_repo|
|
||||
fresh_repo.html_url == repo[:url]
|
||||
end.first
|
||||
end
|
||||
|
||||
fetched_repo = client.repo(repo.info_hash[:full_name])
|
||||
repo.update!(
|
||||
github_id_code: fetched_repo.id,
|
||||
name: fetched_repo.name,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
class ChangeGithubRepoColumnDefault < ActiveRecord::Migration[5.1]
|
||||
def change
|
||||
change_column_default(:github_repos, :info_hash, {}.to_yaml)
|
||||
end
|
||||
end
|
||||
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20180508200948) do
|
||||
ActiveRecord::Schema.define(version: 20180516173047) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
||||
|
|
@ -294,7 +294,7 @@ ActiveRecord::Schema.define(version: 20180508200948) do
|
|||
t.boolean "featured", default: false
|
||||
t.boolean "fork", default: false
|
||||
t.integer "github_id_code"
|
||||
t.text "info_hash", default: "--- []\n"
|
||||
t.text "info_hash", default: "--- {}\n"
|
||||
t.string "language"
|
||||
t.string "name"
|
||||
t.integer "priority", default: 0
|
||||
|
|
|
|||
|
|
@ -37,15 +37,14 @@ RSpec.describe GithubRepo, type: :model do
|
|||
let(:repo_without_github_id) do
|
||||
create(:github_repo, user_id: user.id, url: url_of_repos_without_github_id)
|
||||
end
|
||||
let(:stubbed_github_repos) do
|
||||
[OpenStruct.new(repo.attributes.merge(id: repo.github_id_code, html_url: repo.url)),
|
||||
OpenStruct.new(repo.attributes.merge(id: rand(10000), html_url: url_of_repos_without_github_id))]
|
||||
let(:stubbed_github_repo) do
|
||||
OpenStruct.new(repo.attributes.merge(id: repo.github_id_code, html_url: repo.url))
|
||||
end
|
||||
|
||||
before do
|
||||
repo.save
|
||||
allow(Octokit::Client).to receive(:new).and_return(my_ocktokit_client)
|
||||
allow(my_ocktokit_client).to receive(:repositories) { stubbed_github_repos }
|
||||
allow(my_ocktokit_client).to receive(:repo) { stubbed_github_repo }
|
||||
end
|
||||
|
||||
it "updates all repo" do
|
||||
|
|
@ -55,14 +54,5 @@ RSpec.describe GithubRepo, type: :model do
|
|||
expect(old_updated_at).not_to eq(GithubRepo.find(repo.id).updated_at)
|
||||
end
|
||||
end
|
||||
|
||||
it "uses repo's url as reference if id isn't provided" do
|
||||
repo_without_github_id.update_columns(github_id_code: nil)
|
||||
old_updated_at = repo_without_github_id.updated_at
|
||||
Timecop.freeze(Date.today + 3) do
|
||||
described_class.update_to_latest
|
||||
expect(old_updated_at).not_to eq(GithubRepo.find(repo_without_github_id.id).updated_at)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue