Skip to content

Commit

Permalink
Fix showing custom fields in a project webhook (#16912)
Browse files Browse the repository at this point in the history
* Add reproduction spec

* Ensure representer#to_hash is called as system user

The getter was only called outside the user system block,
which caused the custom fields to not be visible
  • Loading branch information
oliverguenther authored Oct 8, 2024
1 parent 2761764 commit ed73e01
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
7 changes: 2 additions & 5 deletions modules/webhooks/app/workers/attachment_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions modules/webhooks/app/workers/project_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 10 additions & 2 deletions modules/webhooks/app/workers/represented_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions modules/webhooks/app/workers/time_entry_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions modules/webhooks/app/workers/work_package_webhook_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 33 additions & 4 deletions modules/webhooks/spec/workers/project_webhook_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -50,14 +49,19 @@
{ content_type: "text/plain", x_spec: "foobar" }
end

let(:expected_payload) do
{}
end

let(:stub) do
stub_request(:post, stubbed_url.sub("http://", ""))
.with(
body: hash_including(
"action" => event,
"project" => hash_including(
"_type" => "Project",
"name" => "Foo Bar"
"name" => "Foo Bar",
**expected_payload
)
),
headers: request_headers
Expand All @@ -78,7 +82,6 @@

before do
allow(Webhooks::Webhook).to receive(:find).with(webhook.id).and_return(webhook)
login_as user
stub
end

Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit ed73e01

Please sign in to comment.