Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate transferred / moved repos #1827

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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