Skip to content

Commit

Permalink
Add script to restore a yanked version
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalkr132 committed Jan 15, 2017
1 parent ebff0c0 commit f2c94d7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
30 changes: 26 additions & 4 deletions app/models/deletion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class Deletion < ActiveRecord::Base

attr_accessor :version

def restore!
restore_to_index
restore_to_storage
expire_cache
update_search_index
destroy!
end

private

def version_is_indexed
Expand All @@ -30,6 +38,7 @@ def record_metadata
end

def expire_cache
purge_fastly
GemCachePurger.call(rubygem)
end

Expand All @@ -38,22 +47,35 @@ def remove_from_index
Delayed::Job.enqueue Indexer.new, priority: PRIORITIES[:push]
end

def restore_to_index
version.update!(indexed: true, yanked_at: nil, yanked_info_checksum: nil)
Delayed::Job.enqueue Indexer.new, priority: PRIORITIES[:push]
end

def remove_from_storage
RubygemFs.instance.remove("gems/#{@version.full_name}.gem")
RubygemFs.instance.remove("quick/Marshal.4.8/#{@version.full_name}.gemspec.rz")
Fastly.delay.purge("gems/#{@version.full_name}.gem")
Fastly.delay.purge("quick/Marshal.4.8/#{@version.full_name}.gemspec.rz")
end

def restore_to_storage
RubygemFs.instance.restore("gems/#{@version.full_name}.gem")
RubygemFs.instance.restore("quick/Marshal.4.8/#{@version.full_name}.gemspec.rz")
end

def purge_fastly
Fastly.purge("gems/#{@version.full_name}.gem")
Fastly.purge("quick/Marshal.4.8/#{@version.full_name}.gemspec.rz")
end
handle_asynchronously :purge_fastly, priority: PRIORITIES[:push]

def update_search_index
@version.rubygem.delay.update_document
end

def set_yanked_info_checksum
# expire info cache of last version
Rails.cache.delete("info/#{rubygem}")
gem_info = GemInfo.new(version.rubygem.name)
checksum = Digest::MD5.hexdigest(CompactIndex.info(gem_info.compact_index_info))
checksum = GemInfo.new(rubygem).info_checksum
version.update_attribute :yanked_info_checksum, checksum
end
end
5 changes: 5 additions & 0 deletions app/models/gem_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def compact_index_info
end
end

def info_checksum
compact_index_info = CompactIndex.info(compute_compact_index_info)
Digest::MD5.hexdigest(compact_index_info)
end

def self.ordered_names
names = Rails.cache.read('names')
if names
Expand Down
3 changes: 1 addition & 2 deletions app/models/pusher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ def enqueue_web_hook_jobs
end

def set_info_checksum
gem_info = GemInfo.new(rubygem.name).compute_compact_index_info
checksum = Digest::MD5.hexdigest(CompactIndex.info(gem_info))
checksum = GemInfo.new(rubygem.name).info_checksum
version.update_attribute :info_checksum, checksum
end
end
19 changes: 19 additions & 0 deletions script/restore_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby

gem_name, version_number, platform = *ARGV

if gem_name.nil? || version_number.nil?
abort "Usage: script/restore_version GEM_NAME VESRION_NUMBER [PLATFORM]"
end

ENV['RAILS_ENV'] ||= 'production'
require_relative '../config/environment'

rubygem = Rubygem.find_by_name!(gem_name)
slug = version_number
slug << "-#{platform}" unless platform.blank?
version = Version.find_from_slug!(rubygem, slug)

deletion = Deletion.find_by(rubygem: gem_name, version: version_number, platform: version.platform)
deletion.version = version
deletion.restore!

0 comments on commit f2c94d7

Please sign in to comment.