From 3a6b145ac100db150148d20c65a0b14d68556353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Thu, 30 Oct 2025 22:05:34 +0200 Subject: [PATCH] Adds webhooks for WikiPage. diff --git a/app/models/webhook_payload.rb b/app/models/webhook_payload.rb index fd0af9171..869f09843 100644 --- a/app/models/webhook_payload.rb +++ b/app/models/webhook_payload.rb @@ -11,7 +11,8 @@ class WebhookPayload end EVENTS = { - issue: %w[created updated deleted] + issue: %w[created updated deleted], + wiki_page: %w[created updated deleted] } def to_h @@ -71,6 +72,27 @@ class WebhookPayload } end + def wiki_page_payload(action) + wiki_page = object + + ts = case action + when 'created' + wiki_page.created_on + when 'deleted' + Time.now + else + wiki_page.updated_on + end + + { + type: event, + timestamp: ts.iso8601, + data: { + wiki_page: ApiRenderer.new("app/views/wiki/show.api.rsb", user).to_h(page: wiki_page, content: wiki_page.content) + } + } + end + # given a path to an API template (relative to RAILS_ROOT), renders it and returns the resulting hash class ApiRenderer include ApplicationHelper diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index d76e288d8..4b7b7087c 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -62,6 +62,10 @@ class WikiPage < ApplicationRecord before_destroy :delete_redirects after_save :handle_children_move, :delete_selected_attachments + after_create_commit ->{ Webhook.trigger('wiki_page.created', self) } + after_update_commit ->{ Webhook.trigger('wiki_page.updated', self) } + after_destroy_commit ->{ Webhook.trigger('wiki_page.deleted', self) } + # eager load information about last updates, without loading text scope :with_updated_on, lambda {preload(:content_without_text)} @@ -192,6 +196,10 @@ class WikiPage < ApplicationRecord wiki.try(:project) end + def project_id + wiki&.project_id + end + def text content.text if content end diff --git a/config/locales/en.yml b/config/locales/en.yml index 005487b4f..dd0dd284c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1188,6 +1188,10 @@ en: webhook_events_issue_created: Issue created webhook_events_issue_updated: Issue updated webhook_events_issue_deleted: Issue deleted + webhook_events_wiki_page: Wiki pages + webhook_events_wiki_page_created: Wiki page created + webhook_events_wiki_page_updated: Wiki page updated + webhook_events_wiki_page_deleted: Wiki page deleted webhook_url_info: Redmine will send a POST request to this URL whenever one of the selected events occurs in one of the selected projects. webhook_secret_info_html: If provided, Redmine will use this to create a hash signature that is sent with each delivery as the value of the X-Redmine-Signature-256 header. diff --git a/test/unit/webhook_payload_test.rb b/test/unit/webhook_payload_test.rb index e1e809871..da2b36d78 100644 --- a/test/unit/webhook_payload_test.rb +++ b/test/unit/webhook_payload_test.rb @@ -33,4 +33,49 @@ class WebhookPayloadTest < ActiveSupport::TestCase assert i = h.dig(:data, :issue) assert_equal @issue.subject, i[:subject], i.inspect end + + test "wiki page create payload should contain page details" do + wiki = @project.wiki + page = WikiPage.new(:title => 'Test Page', :wiki => wiki) + page.content = WikiContent.new(text: 'Test content', author: @dlopper) + page.save! + + p = WebhookPayload.new('wiki_page.created', page, @dlopper) + assert h = p.to_h + assert_equal 'wiki_page.created', h[:type] + assert_equal 'Test_Page', h.dig(:data, :wiki_page, :title) + assert_equal 'Test content', h.dig(:data, :wiki_page, :text) + assert_equal @dlopper.name, h.dig(:data, :wiki_page, :author, :name) + end + + test "wiki page update payload should contain updated timestamp" do + wiki = @project.wiki + page = WikiPage.new(wiki: wiki, title: 'Test Page') + page.content = WikiContent.new(text: 'Initial content', author: @dlopper) + page.save! + + page.content.text = 'Updated content' + page.content.save! + page.reload + + p = WebhookPayload.new('wiki_page.updated', page, @dlopper) + h = p.to_h + assert_equal 'wiki_page.updated', h[:type] + assert_equal 'Updated content', h.dig(:data, :wiki_page, :text) + end + + test "deleted wiki page payload should contain basic info" do + wiki = @project.wiki + page = WikiPage.new(wiki: wiki, title: 'Test Page') + page.content = WikiContent.new(text: 'Test content', author: @dlopper) + page.save! + + page.destroy + + p = WebhookPayload.new('wiki_page.deleted', page, @dlopper) + h = p.to_h + assert_equal 'wiki_page.deleted', h[:type] + assert_equal 'Test_Page', h.dig(:data, :wiki_page, :title) + assert_equal @project.id, h.dig(:data, :wiki_page, :project, :id) + end end -- 2.50.1 (Apple Git-155)