Skip to content

Commit

Permalink
Deduplicate transferred / moved repos
Browse files Browse the repository at this point in the history
  • Loading branch information
kiskoza committed Jan 11, 2024
1 parent 2f19341 commit 4790f65
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
23 changes: 20 additions & 3 deletions app/models/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Repo < ActiveRecord::Base
validate :github_url_exists, on: :create, unless: :skip_validation
validates :name, :user_name, presence: true

has_many :issues
has_many :repo_subscriptions
has_many :issues, dependent: :destroy
has_many :repo_subscriptions, dependent: :restrict_with_error
has_many :users, through: :repo_subscriptions
has_many :subscribers, through: :repo_subscriptions, source: :user
has_many :doc_classes, dependent: :destroy
Expand Down Expand Up @@ -194,7 +194,24 @@ def update_from_github
repo_full_name = fetcher_json.fetch("full_name", full_name)

if repo_full_name != full_name && self.class.exists_with_name?(repo_full_name)
# TODO: Add deduplication step
ActiveRecord::Base.transaction do
new_repo = self.class.find_by(full_name: repo_full_name)

repo_subscriptions.each do |existing_subscription|
new_repo.repo_subscriptions.find_or_create_by!(user_id: existing_subscription.user_id) do |new_subscription|
new_subscription.last_sent_at = existing_subscription.last_sent_at
new_subscription.email_limit = existing_subscription.email_limit
new_subscription.write = existing_subscription.write
new_subscription.read = existing_subscription.read
new_subscription.write_limit = existing_subscription.write_limit
new_subscription.read_limit = existing_subscription.read_limit
end
end

repo_subscriptions.destroy_all
destroy!
end

return
end

Expand Down
18 changes: 6 additions & 12 deletions test/jobs/update_repo_info_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,13 @@ class UpdateRepoInfoJobTest < ActiveJob::TestCase
"full_name" => "sinatra/sinatra"
}
)
repo = repos(:node)
assert_no_changes -> {
[
repo.full_name,
repo.name,
repo.user_name,
repo.language,
repo.description,
repo.archived
]
} do
repo = repos(:rails_rails)
sinatra = repos(:sinatra_sinatra)

assert_difference "Repo.count", -1 do
UpdateRepoInfoJob.perform_now(repo)
repo.reload
end

assert_equal 2, sinatra.subscribers.count
end
end

0 comments on commit 4790f65

Please sign in to comment.