Skip to content

Commit

Permalink
Merge pull request joshsoftware#216 from prasadsurase/soft-delete-repos
Browse files Browse the repository at this point in the history
Soft delete repositories.
  • Loading branch information
sethu authored Dec 12, 2016
2 parents 93208b4 + 6767960 commit cb30bd7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ gem 'whenever', :require => false
gem 'mongoid-slug', '~> 5.2'
gem 'redcarpet'
gem 'rollbar'
gem 'mongoid-paranoia'

group :development, :test do
gem 'byebug'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ GEM
mongoid-compatibility (0.4.0)
activesupport
mongoid (>= 2.0)
mongoid-paranoia (2.0.0)
activesupport (~> 4.0)
mongoid (>= 4.0.0, <= 6.0.0)
mongoid-slug (5.2.0)
mongoid (>= 3.0)
mongoid-compatibility
Expand Down Expand Up @@ -401,6 +404,7 @@ DEPENDENCIES
minitest-rails-capybara
mocha
mongoid (~> 5.0)
mongoid-paranoia
mongoid-slug (~> 5.2)
omniauth-github (~> 1.1.2)
poltergeist
Expand Down
12 changes: 11 additions & 1 deletion app/jobs/user_repos_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ def perform(user)
end

def add_repo(gh_repo)
repo = Repository.where(gh_id: gh_repo.id).first
#check if the repository is not soft deleted and
repo = Repository.unscoped.where(gh_id: gh_repo.id).first

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
1 change: 1 addition & 0 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Repository
include Mongoid::Timestamps
include GlobalID::Identification
include RepoLeaders
include Mongoid::Paranoia

field :name, type: String
field :description, type: String
Expand Down
50 changes: 50 additions & 0 deletions test/jobs/user_repos_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,54 @@ def stub_get(path, endpoint = Github.endpoint.to_s)
assert_nil parent_repo.source_gh_id
end

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)}
)
Repository.any_instance.stubs(:info).returns(
Hashie::Mash.new(JSON.parse(File.read('test/fixtures/user-fork-repo.json')))
)
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 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)}
)
Repository.any_instance.stubs(:info).returns(
Hashie::Mash.new(JSON.parse(File.read('test/fixtures/repo.json')))
)
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
refute repo.destroyed?
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 cb30bd7

Please sign in to comment.