Skip to content

Commit

Permalink
Updated the feature to update the repo's stars.
Browse files Browse the repository at this point in the history
  • Loading branch information
prasadsurase committed Dec 8, 2016
1 parent a416893 commit 6767960
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/jobs/user_repos_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ def add_repo(gh_repo)
if repo
if repo.info.stargazers_count < REPOSITORY_CONFIG['popular']['stars']
# soft delete the repository if the star rating has declined.
repo.set(stars: gh_repo.stargazers_count)
repo.destroy
else
# restore the repo if the repository was already soft deleted and the current star count is greater then the threshold
repo.restore if repo.destroyed?
repo.set(stars: repo.info.stargazers_count)
end

repo.users << user unless repo.users.include?(user)
return
end
Expand Down
20 changes: 13 additions & 7 deletions test/jobs/user_repos_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def stub_get(path, endpoint = Github.endpoint.to_s)
test 'destroy the repository if it is already persisted if the rating has dropped' do
repo = create :repository, name: 'code-curiosity', ssh_url: '[email protected]:prasadsurase/code-curiosity.git',
owner: 'prasadsurase', stars: 26, gh_id: 67219068
@user.repositories << repo
@user.save
User.any_instance.stubs(:fetch_all_github_repos).returns(
JSON.parse(File.read('test/fixtures/repos.json')).collect{|i| Hashie::Mash.new(i)}
)
Expand All @@ -123,19 +125,22 @@ def stub_get(path, endpoint = Github.endpoint.to_s)
)
assert_nil repo.deleted_at
assert_equal 1, Repository.count
assert_equal 26, repo.stars
UserReposJob.perform_now(@user)
repo.reload
refute_nil repo.deleted_at
assert repo.destroyed?
assert_equal 26, repo.stars
assert_equal 24, repo.info.stargazers_count
assert_equal 0, Repository.count
assert_equal 1, Repository.unscoped.count
assert_equal 0, Repository.count
assert_equal 24, repo.info.stargazers_count
assert_equal 24, repo.stars
end

test 'restore the repository if it is already persisted and destroyed if the rating has increased' do
repo = create :repository, name: 'code-curiosity', ssh_url: '[email protected]:prasadsurase/code-curiosity.git',
owner: 'prasadsurase', stars: 24, gh_id: 67219068, deleted_at: Time.now - 2.days
@user.repositories << repo
@user.save
User.any_instance.stubs(:fetch_all_github_repos).returns(
JSON.parse(File.read('test/fixtures/repos.json')).collect{|i| Hashie::Mash.new(i)}
)
Expand All @@ -144,16 +149,17 @@ def stub_get(path, endpoint = Github.endpoint.to_s)
)
refute_nil repo.deleted_at
assert repo.destroyed?
assert_equal 24, repo.stars
assert_equal 0, Repository.count
assert_equal 1, Repository.unscoped.count
UserReposJob.perform_now(@user)
repo.reload
assert_nil repo.deleted_at
refute repo.destroyed?
assert_equal 24, repo.stars
assert_equal 25, repo.info.stargazers_count
assert_equal 1, Repository.count
assert_nil repo.deleted_at
assert_equal 1, Repository.unscoped.count
assert_equal 1, Repository.count
assert_equal 25, repo.info.stargazers_count
assert_equal 25, repo.stars
end

end

0 comments on commit 6767960

Please sign in to comment.