From fdbbcb591181717449ac80901e5641cc2c0f7e19 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Mon, 11 Dec 2023 12:21:12 -0800 Subject: [PATCH] Fix creating rubygem trusted publisher when gh action exists (#4282) Fix is removing repository_owner_id from search params --- .../oidc/trusted_publisher/github_action.rb | 3 ++- ...ygem_trusted_publishers_controller_test.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/oidc/trusted_publisher/github_action.rb b/app/models/oidc/trusted_publisher/github_action.rb index 29873689123..0a3280ee41c 100644 --- a/app/models/oidc/trusted_publisher/github_action.rb +++ b/app/models/oidc/trusted_publisher/github_action.rb @@ -42,8 +42,9 @@ def self.permitted_attributes end def self.build_trusted_publisher(params) - params.delete(:environment) if params[:environment].blank? params = params.reverse_merge(repository_owner_id: nil, repository_name: nil, workflow_filename: nil, environment: nil) + params.delete(:environment) if params[:environment].blank? + params.delete(:repository_owner_id) find_or_initialize_by(params) end diff --git a/test/integration/oidc/rubygem_trusted_publishers_controller_test.rb b/test/integration/oidc/rubygem_trusted_publishers_controller_test.rb index d695739859e..4f8ab977973 100644 --- a/test/integration/oidc/rubygem_trusted_publishers_controller_test.rb +++ b/test/integration/oidc/rubygem_trusted_publishers_controller_test.rb @@ -88,6 +88,25 @@ class OIDC::RubygemTrustedPublishersControllerTest < ActionDispatch::Integration assert_redirected_to rubygem_trusted_publishers_url(@rubygem.slug) end + should "create rubygem trusted publisher when trusted publisher already exists" do + stub_request(:get, "https://api.github.com/users/example") + .to_return(status: 200, body: { id: "123456" }.to_json, headers: { "Content-Type" => "application/json" }) + + github_action_trusted_publisher = create(:oidc_trusted_publisher_github_action) + + assert_difference("OIDC::RubygemTrustedPublisher.count") do + post rubygem_trusted_publishers_url(@rubygem.slug), params: { + oidc_rubygem_trusted_publisher: { + trusted_publisher_type: github_action_trusted_publisher.class.polymorphic_name, + trusted_publisher_attributes: github_action_trusted_publisher.as_json + .slice("workflow_filename", "repository_owner", "repository_name").merge("environment" => "") + } + } + end + + assert_redirected_to rubygem_trusted_publishers_url(@rubygem.slug) + end + should "error creating trusted publisher with type" do assert_no_difference("OIDC::RubygemTrustedPublisher.count") do post rubygem_trusted_publishers_url(@rubygem.slug), params: {