Project

General

Profile

Feature #29664 » 0001-Adds-webhooks-for-WikiPage.patch

Marius BĂLTEANU, 2025-10-30 21:11

View differences:

app/models/webhook_payload.rb
11 11
  end
12 12

  
13 13
  EVENTS = {
14
    issue: %w[created updated deleted]
14
    issue: %w[created updated deleted],
15
    wiki_page: %w[created updated deleted]
15 16
  }
16 17

  
17 18
  def to_h
......
71 72
    }
72 73
  end
73 74

  
75
  def wiki_page_payload(action)
76
    wiki_page = object
77

  
78
    ts = case action
79
         when 'created'
80
           wiki_page.created_on
81
         when 'deleted'
82
           Time.now
83
         else
84
           wiki_page.updated_on
85
         end
86

  
87
    {
88
      type: event,
89
      timestamp: ts.iso8601,
90
      data: {
91
        wiki_page: ApiRenderer.new("app/views/wiki/show.api.rsb", user).to_h(page: wiki_page, content: wiki_page.content)
92
      }
93
    }
94
  end
95

  
74 96
  # given a path to an API template (relative to RAILS_ROOT), renders it and returns the resulting hash
75 97
  class ApiRenderer
76 98
    include ApplicationHelper
app/models/wiki_page.rb
62 62
  before_destroy :delete_redirects
63 63
  after_save :handle_children_move, :delete_selected_attachments
64 64

  
65
  after_create_commit  ->{ Webhook.trigger('wiki_page.created', self) }
66
  after_update_commit  ->{ Webhook.trigger('wiki_page.updated', self) }
67
  after_destroy_commit ->{ Webhook.trigger('wiki_page.deleted', self) }
68

  
65 69
  # eager load information about last updates, without loading text
66 70
  scope :with_updated_on, lambda {preload(:content_without_text)}
67 71

  
......
192 196
    wiki.try(:project)
193 197
  end
194 198

  
199
  def project_id
200
    wiki&.project_id
201
  end
202

  
195 203
  def text
196 204
    content.text if content
197 205
  end
config/locales/en.yml
1188 1188
  webhook_events_issue_created: Issue created
1189 1189
  webhook_events_issue_updated: Issue updated
1190 1190
  webhook_events_issue_deleted: Issue deleted
1191
  webhook_events_wiki_page: Wiki pages
1192
  webhook_events_wiki_page_created: Wiki page created
1193
  webhook_events_wiki_page_updated: Wiki page updated
1194
  webhook_events_wiki_page_deleted: Wiki page deleted
1191 1195
  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.
1192 1196
  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.  
1193 1197

  
test/unit/webhook_payload_test.rb
33 33
    assert i = h.dig(:data, :issue)
34 34
    assert_equal @issue.subject, i[:subject], i.inspect
35 35
  end
36

  
37
  test "wiki page create payload should contain page details" do
38
    wiki = @project.wiki
39
    page = WikiPage.new(:title => 'Test Page', :wiki => wiki)
40
    page.content = WikiContent.new(text: 'Test content', author: @dlopper)
41
    page.save!
42

  
43
    p = WebhookPayload.new('wiki_page.created', page, @dlopper)
44
    assert h = p.to_h
45
    assert_equal 'wiki_page.created', h[:type]
46
    assert_equal 'Test_Page', h.dig(:data, :wiki_page, :title)
47
    assert_equal 'Test content', h.dig(:data, :wiki_page, :text)
48
    assert_equal @dlopper.name, h.dig(:data, :wiki_page, :author, :name)
49
  end
50

  
51
  test "wiki page update payload should contain updated timestamp" do
52
    wiki = @project.wiki
53
    page = WikiPage.new(wiki: wiki, title: 'Test Page')
54
    page.content = WikiContent.new(text: 'Initial content', author: @dlopper)
55
    page.save!
56

  
57
    page.content.text = 'Updated content'
58
    page.content.save!
59
    page.reload
60

  
61
    p = WebhookPayload.new('wiki_page.updated', page, @dlopper)
62
    h = p.to_h
63
    assert_equal 'wiki_page.updated', h[:type]
64
    assert_equal 'Updated content', h.dig(:data, :wiki_page, :text)
65
  end
66

  
67
  test "deleted wiki page payload should contain basic info" do
68
    wiki = @project.wiki
69
    page = WikiPage.new(wiki: wiki, title: 'Test Page')
70
    page.content = WikiContent.new(text: 'Test content', author: @dlopper)
71
    page.save!
72

  
73
    page.destroy
74

  
75
    p = WebhookPayload.new('wiki_page.deleted', page, @dlopper)
76
    h = p.to_h
77
    assert_equal 'wiki_page.deleted', h[:type]
78
    assert_equal 'Test_Page', h.dig(:data, :wiki_page, :title)
79
    assert_equal @project.id, h.dig(:data, :wiki_page, :project, :id)
80
  end
36 81
end
(13-13/13)