diff --git a/modules/webhooks/app/workers/attachment_webhook_job.rb b/modules/webhooks/app/workers/attachment_webhook_job.rb index 97a301136d6c..f05d2e855850 100644 --- a/modules/webhooks/app/workers/attachment_webhook_job.rb +++ b/modules/webhooks/app/workers/attachment_webhook_job.rb @@ -38,10 +38,7 @@ def accepted_in_project? webhook.enabled_for_project?(project.id) end - def payload_representer - User.system.run_given do |user| - ::API::V3::Attachments::AttachmentRepresenter - .create(resource, current_user: user, embed_links: true) - end + def payload_representer_class + ::API::V3::Attachments::AttachmentRepresenter end end diff --git a/modules/webhooks/app/workers/project_webhook_job.rb b/modules/webhooks/app/workers/project_webhook_job.rb index 732563074593..b96fd7771840 100644 --- a/modules/webhooks/app/workers/project_webhook_job.rb +++ b/modules/webhooks/app/workers/project_webhook_job.rb @@ -39,10 +39,7 @@ def accepted_in_project? end end - def payload_representer - User.system.run_given do |user| - ::API::V3::Projects::ProjectRepresenter - .create(resource, current_user: user, embed_links: true) - end + def payload_representer_class + ::API::V3::Projects::ProjectRepresenter end end diff --git a/modules/webhooks/app/workers/represented_webhook_job.rb b/modules/webhooks/app/workers/represented_webhook_job.rb index 0d4ca6a21163..0903395d604a 100644 --- a/modules/webhooks/app/workers/represented_webhook_job.rb +++ b/modules/webhooks/app/workers/represented_webhook_job.rb @@ -68,14 +68,22 @@ def payload_key raise NotImplementedError end - def payload_representer + def represented_payload + User.system.run_given do |user| + payload_representer_class + .create(resource, current_user: user, embed_links: true) + .to_hash # to_hash needs to be called within the system user block + end + end + + def payload_representer_class raise NotImplementedError end def request_body { :action => event_name, - payload_key => payload_representer + payload_key => represented_payload }.to_json end end diff --git a/modules/webhooks/app/workers/time_entry_webhook_job.rb b/modules/webhooks/app/workers/time_entry_webhook_job.rb index c8c2013caddb..d9400d8da954 100644 --- a/modules/webhooks/app/workers/time_entry_webhook_job.rb +++ b/modules/webhooks/app/workers/time_entry_webhook_job.rb @@ -31,10 +31,7 @@ def payload_key :time_entry end - def payload_representer - User.system.run_given do |user| - ::API::V3::TimeEntries::TimeEntryRepresenter - .create(resource, current_user: user, embed_links: true) - end + def payload_representer_class + ::API::V3::TimeEntries::TimeEntryRepresenter end end diff --git a/modules/webhooks/app/workers/work_package_webhook_job.rb b/modules/webhooks/app/workers/work_package_webhook_job.rb index b25b01d043a1..4f9cb3928f1b 100644 --- a/modules/webhooks/app/workers/work_package_webhook_job.rb +++ b/modules/webhooks/app/workers/work_package_webhook_job.rb @@ -31,10 +31,7 @@ def payload_key :work_package end - def payload_representer - User.system.run_given do |user| - ::API::V3::WorkPackages::WorkPackageRepresenter - .create(resource, current_user: user, embed_links: true) - end + def payload_representer_class + ::API::V3::WorkPackages::WorkPackageRepresenter end end diff --git a/modules/webhooks/spec/workers/project_webhook_job_spec.rb b/modules/webhooks/spec/workers/project_webhook_job_spec.rb index 03f81354a124..3eac1560955a 100644 --- a/modules/webhooks/spec/workers/project_webhook_job_spec.rb +++ b/modules/webhooks/spec/workers/project_webhook_job_spec.rb @@ -29,7 +29,6 @@ require "spec_helper" RSpec.describe ProjectWebhookJob, :webmock, type: :job do - shared_let(:user) { create(:admin) } shared_let(:request_url) { "http://example.net/test/42" } shared_let(:project) { create(:project, name: "Foo Bar") } shared_let(:webhook) { create(:webhook, all_projects: true, url: request_url, secret: nil) } @@ -50,6 +49,10 @@ { content_type: "text/plain", x_spec: "foobar" } end + let(:expected_payload) do + {} + end + let(:stub) do stub_request(:post, stubbed_url.sub("http://", "")) .with( @@ -57,7 +60,8 @@ "action" => event, "project" => hash_including( "_type" => "Project", - "name" => "Foo Bar" + "name" => "Foo Bar", + **expected_payload ) ), headers: request_headers @@ -78,7 +82,6 @@ before do allow(Webhooks::Webhook).to receive(:find).with(webhook.id).and_return(webhook) - login_as user stub end @@ -133,7 +136,7 @@ end end - describe "triggering a projec creation" do + describe "triggering a project creation" do it_behaves_like "a project webhook call" do let(:event) { "project:created" } end @@ -146,4 +149,30 @@ let(:response_body) { "not found" } end end + + describe "triggering an update with a custom field set" do + shared_let(:custom_field) { create(:project_custom_field, :string, projects: [project]) } + shared_let(:custom_value) do + create(:custom_value, + custom_field:, + customized: project, + value: "wat") + end + + it_behaves_like "a project webhook call" do + let(:expected_payload) do + { custom_field.attribute_name(:camel_case) => "wat" } + end + + it "includes the custom field value" do + subject + + expect(stub).to have_been_requested + + log = Webhooks::Log.last + request = JSON.parse(log.request_body) + expect(request["project"][custom_field.attribute_name(:camel_case)]).to eq "wat" + end + end + end end