Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix showing custom fields in a project webhook #16912

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions modules/webhooks/app/workers/attachment_webhook_job.rb
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,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
@@ -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