Skip to content

Commit

Permalink
Avo actions to enqueue compact index file upload jobs (#3970)
Browse files Browse the repository at this point in the history
  • Loading branch information
segiddins authored and arunagw committed Jul 31, 2023
1 parent ebb5570 commit 4344a14
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 79 deletions.
15 changes: 15 additions & 0 deletions app/avo/actions/upload_info_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class UploadInfoFile < BaseAction
self.name = "Upload Info File"
self.visible = lambda {
current_user.team_member?("rubygems-org") && view == :show
}
self.confirm_button_label = "Upload"

class ActionHandler < ActionHandler
def handle_model(rubygem)
UploadInfoFileJob.perform_later(rubygem_name: rubygem.name)

succeed("Upload job scheduled")
end
end
end
18 changes: 18 additions & 0 deletions app/avo/actions/upload_versions_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class UploadVersionsFile < BaseAction
self.name = "Upload Versions File"
self.visible = lambda {
current_user.team_member?("rubygems-org") && view == :index
}
self.standalone = true
self.confirm_button_label = "Upload"

class ActionHandler < ActionHandler
def handle_standalone
UploadVersionsFileJob.perform_later

succeed("Upload job scheduled")

Version.last
end
end
end
2 changes: 2 additions & 0 deletions app/avo/resources/rubygem_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class RubygemResource < Avo::BaseResource
action RestoreRubygemVersion
action YankRubygem
action ReserveNamespace
action UploadInfoFile
action UploadVersionsFile

class IndexedFilter < ScopeBooleanFilter; end
filter IndexedFilter, arguments: { default: { with_versions: true, without_versions: true } }
Expand Down
1 change: 1 addition & 0 deletions app/models/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class Audit < ApplicationRecord
serialize :audited_changes, JSON

validates :action, presence: true
validates :auditable, presence: false
end
105 changes: 26 additions & 79 deletions test/system/avo/rubygems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Avo::RubygemsSystemTest < ApplicationSystemTestCase
make_my_diffs_pretty!

include ActiveJob::TestHelper

def sign_in_as(user)
OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(
provider: "github",
Expand Down Expand Up @@ -357,100 +359,45 @@ def sign_in_as(user)
page.assert_text "Namespace reserved: Successfully registered gem: foo (0.0.0.reserved.2)"
end

test "restore a rubygem version" do
Minitest::Test.make_my_diffs_pretty!
test "upload versions file" do
admin_user = create(:admin_github_user, :is_admin)
sign_in_as admin_user

rubygem = create(:rubygem)
version = create(:version, rubygem: rubygem)
deletion = create(:deletion, version: version)
version_attributes = version.attributes.with_indifferent_access
rubygem_attributes = rubygem.attributes.with_indifferent_access
visit avo.resources_rubygems_path

visit avo.resources_rubygem_path(rubygem)
_ = create(:version)

click_button "Actions"
click_on "Restore version"
click_on "Upload Versions File"
fill_in "Comment", with: "A nice long comment"

assert_no_changes "Rubygem.find(#{rubygem.id}).attributes" do
click_button "Restore version"
end
page.assert_text "Must supply a sufficiently detailed comment"
assert_enqueued_jobs 1, only: UploadVersionsFileJob do
click_button "Upload"

fill_in "Comment", with: "A nice long comment"
select("version:#{deletion.number} - platform:#{deletion.platform}", from: "Version with platform")
page.assert_text "Upload job scheduled"
end

click_button "Restore version"
assert_not_nil Audit.last
end

page.assert_text "Action ran successfully!"
page.assert_text rubygem.to_global_id.uri.to_s
test "upload info file" do
admin_user = create(:admin_github_user, :is_admin)
sign_in_as admin_user

rubygem.reload
version.reload
version = create(:version)
visit avo.resources_rubygem_path(version.rubygem)

assert deletion.version.indexed
assert_nil version.yanked_at
assert_nil version.yanked_info_checksum
click_button "Actions"
click_on "Upload Info File"
fill_in "Comment", with: "A nice long comment"

audit = rubygem.audits.sole
assert_enqueued_jobs 1, only: UploadInfoFileJob do
click_button "Upload"

page.assert_text audit.id
assert_equal "Rubygem", audit.auditable_type
assert_equal "Restore version", audit.action
assert_equal admin_user, audit.admin_github_user
assert_equal "A nice long comment", audit.comment
rubygem_audit = audit.audited_changes["records"].select do |k, _|
k =~ %r{gid://gemcutter/Rubygem/#{rubygem.id}}
page.assert_text "Upload job scheduled"
end
rubygem_updated_at_changes = rubygem_audit["gid://gemcutter/Rubygem/#{rubygem.id}"]["changes"]["updated_at"]

assert_equal(
{
"records" => {
"gid://gemcutter/Version/#{version.id}" =>
{
"changes" =>
{
"indexed" => [false, true],
"yanked_at" => [version_attributes[:yanked_at].as_json, nil],
"yanked_info_checksum" => [version_attributes[:yanked_info_checksum], nil],
"updated_at" => [version_attributes[:updated_at].as_json, version.updated_at.as_json]
},
"unchanged" => version_attributes
.except("updated_at", "yanked_info_checksum", "yanked_at", "indexed")
.merge("position" => 0, "latest" => false)
.transform_values(&:as_json)
},
"gid://gemcutter/Rubygem/#{rubygem.id}" =>
{
"changes" =>
{ "updated_at" => rubygem_updated_at_changes,
"indexed" => [false, true] },
"unchanged" => rubygem_attributes
.except("updated_at", "indexed")
.transform_values(&:as_json)
},
"gid://gemcutter/Deletion/#{deletion.id}" =>
{
"changes" =>
{
"id" => [deletion.id, nil],
"user_id" => [admin_user.id, nil],
"rubygem" => [rubygem.name, nil],
"number" => [version_attributes[:number], nil],
"platform" => ["ruby", nil],
"created_at" => [deletion.created_at.as_json, nil],
"updated_at" => [deletion.updated_at.as_json, nil]
},
"unchanged" => {}
}
},
"fields" => { "version_with_platform" => deletion.id.to_s },
"arguments" => {},
"models" => ["gid://gemcutter/Rubygem/#{rubygem.id}"]
},
audit.audited_changes
)
assert_not_nil Audit.last
>>>>>>> 9e93a64ff ( Avo actions to enqueue compact index file upload jobs (#3970))
end
end

0 comments on commit 4344a14

Please sign in to comment.