mirror of
https://github.com/meineerde/redmine.git
synced 2026-02-17 17:12:03 +00:00
Adds webooks for news (#29664).
git-svn-id: https://svn.redmine.org/redmine/trunk@24203 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
6bc29c40fd
commit
ea0a6dfbcb
@ -41,6 +41,10 @@ class News < ApplicationRecord
|
|||||||
after_create :add_author_as_watcher
|
after_create :add_author_as_watcher
|
||||||
after_create_commit :send_notification
|
after_create_commit :send_notification
|
||||||
|
|
||||||
|
after_create_commit ->{ Webhook.trigger('news.created', self) }
|
||||||
|
after_update_commit ->{ Webhook.trigger('news.updated', self) }
|
||||||
|
after_destroy_commit ->{ Webhook.trigger('news.deleted', self) }
|
||||||
|
|
||||||
scope :visible, (lambda do |*args|
|
scope :visible, (lambda do |*args|
|
||||||
joins(:project).
|
joins(:project).
|
||||||
where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
|
where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args))
|
||||||
|
|||||||
@ -31,6 +31,7 @@ class WebhookPayload
|
|||||||
issue: %w[created updated deleted],
|
issue: %w[created updated deleted],
|
||||||
wiki_page: %w[created updated deleted],
|
wiki_page: %w[created updated deleted],
|
||||||
time_entry: %w[created updated deleted],
|
time_entry: %w[created updated deleted],
|
||||||
|
news: %w[created updated deleted],
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
@ -130,6 +131,25 @@ class WebhookPayload
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def news_payload(action)
|
||||||
|
news = object
|
||||||
|
ts = case action
|
||||||
|
when 'created'
|
||||||
|
news.created_on
|
||||||
|
when 'deleted'
|
||||||
|
Time.now
|
||||||
|
else
|
||||||
|
news.updated_on
|
||||||
|
end
|
||||||
|
{
|
||||||
|
type: event,
|
||||||
|
timestamp: ts.iso8601,
|
||||||
|
data: {
|
||||||
|
news: ApiRenderer.new("app/views/news/show.api.rsb", user).to_h(news: news)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
# given a path to an API template (relative to RAILS_ROOT), renders it and returns the resulting hash
|
# given a path to an API template (relative to RAILS_ROOT), renders it and returns the resulting hash
|
||||||
class ApiRenderer
|
class ApiRenderer
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
|
|||||||
@ -1197,6 +1197,10 @@ en:
|
|||||||
webhook_events_time_entry_created: Time entry created
|
webhook_events_time_entry_created: Time entry created
|
||||||
webhook_events_time_entry_updated: Time entry updated
|
webhook_events_time_entry_updated: Time entry updated
|
||||||
webhook_events_time_entry_deleted: Time entry deleted
|
webhook_events_time_entry_deleted: Time entry deleted
|
||||||
|
webhook_events_news: News
|
||||||
|
webhook_events_news_created: News created
|
||||||
|
webhook_events_news_updated: News updated
|
||||||
|
webhook_events_news_deleted: News 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_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.
|
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.
|
||||||
|
|
||||||
|
|||||||
@ -125,4 +125,35 @@ class WebhookPayloadTest < ActiveSupport::TestCase
|
|||||||
assert_equal 'time_entry.deleted', h[:type]
|
assert_equal 'time_entry.deleted', h[:type]
|
||||||
assert_equal 4.25, h.dig(:data, :time_entry, :hours)
|
assert_equal 4.25, h.dig(:data, :time_entry, :hours)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "news created payload should contain news details" do
|
||||||
|
news = News.generate!
|
||||||
|
|
||||||
|
p = WebhookPayload.new('news.created', news, @dlopper)
|
||||||
|
assert h = p.to_h
|
||||||
|
assert_equal 'news.created', h[:type]
|
||||||
|
assert_equal news.title, h.dig(:data, :news, :title)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "news updated payload should contain updated timestamp" do
|
||||||
|
news = News.first
|
||||||
|
|
||||||
|
news.title = 'Updated title'
|
||||||
|
news.save!
|
||||||
|
|
||||||
|
p = WebhookPayload.new('news.updated', news, @dlopper)
|
||||||
|
h = p.to_h
|
||||||
|
assert_equal 'news.updated', h[:type]
|
||||||
|
assert_equal 'Updated title', h.dig(:data, :news, :title)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "news deleted payload should contain basic info" do
|
||||||
|
news = News.first
|
||||||
|
news.destroy
|
||||||
|
|
||||||
|
p = WebhookPayload.new('news.deleted', news, @dlopper)
|
||||||
|
h = p.to_h
|
||||||
|
assert_equal 'news.deleted', h[:type]
|
||||||
|
assert_equal 'Updated title', h.dig(:data, :news, :title)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user