Feature #29664 » 0001-Adds-webooks-for-time-entries-29664.patch
| app/models/time_entry.rb | ||
|---|---|---|
|
before_validation :set_author_if_nil
|
||
|
validate :validate_time_entry
|
||
|
after_create_commit ->{ Webhook.trigger('time_entry.created', self) }
|
||
|
after_update_commit ->{ Webhook.trigger('time_entry.updated', self) }
|
||
|
after_destroy_commit ->{ Webhook.trigger('time_entry.deleted', self) }
|
||
|
scope :visible, (lambda do |*args|
|
||
|
joins(:project).
|
||
|
where(TimeEntry.visible_condition(args.shift || User.current, *args))
|
||
| app/models/webhook_payload.rb | ||
|---|---|---|
|
EVENTS = {
|
||
|
issue: %w[created updated deleted],
|
||
|
wiki_page: %w[created updated deleted]
|
||
|
wiki_page: %w[created updated deleted],
|
||
|
time_entry: %w[created updated deleted],
|
||
|
}
|
||
|
def to_h
|
||
| ... | ... | |
|
}
|
||
|
end
|
||
|
def time_entry_payload(action)
|
||
|
time_entry = object
|
||
|
ts = case action
|
||
|
when 'created'
|
||
|
time_entry.created_on
|
||
|
when 'deleted'
|
||
|
Time.now
|
||
|
else
|
||
|
time_entry.updated_on
|
||
|
end
|
||
|
{
|
||
|
type: event,
|
||
|
timestamp: ts.iso8601,
|
||
|
data: {
|
||
|
time_entry: ApiRenderer.new("app/views/timelog/show.api.rsb", user).to_h(time_entry: time_entry)
|
||
|
}
|
||
|
}
|
||
|
end
|
||
|
# given a path to an API template (relative to RAILS_ROOT), renders it and returns the resulting hash
|
||
|
class ApiRenderer
|
||
|
include ApplicationHelper
|
||
| config/locales/en.yml | ||
|---|---|---|
|
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_events_time_entry: Time entries
|
||
|
webhook_events_time_entry_created: Time entry created
|
||
|
webhook_events_time_entry_updated: Time entry updated
|
||
|
webhook_events_time_entry_deleted: Time entry 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.
|
||
|
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.
|
||
|
button_login: Login
|
||
|
button_submit: Submit
|
||
| test/unit/webhook_payload_test.rb | ||
|---|---|---|
|
assert_equal 'wiki_page.deleted', h[:type]
|
||
|
assert_equal 'Test_Page', h.dig(:data, :wiki_page, :title)
|
||
|
end
|
||
|
test "time entry created payload should contain time entry details" do
|
||
|
time_entry = TimeEntry.generate!
|
||
|
p = WebhookPayload.new('time_entry.created', time_entry, @dlopper)
|
||
|
assert h = p.to_h
|
||
|
assert_equal 'time_entry.created', h[:type]
|
||
|
assert_equal time_entry.hours, h.dig(:data, :time_entry, :hours)
|
||
|
end
|
||
|
test "time entry updated payload should contain updated timestamp" do
|
||
|
time_entry = TimeEntry.first
|
||
|
time_entry.hours = 2.5
|
||
|
time_entry.save!
|
||
|
p = WebhookPayload.new('time_entry.updated', time_entry, @dlopper)
|
||
|
h = p.to_h
|
||
|
assert_equal 'time_entry.updated', h[:type]
|
||
|
assert_equal 2.5, h.dig(:data, :time_entry, :hours)
|
||
|
end
|
||
|
test "time entry deleted payload should contain basic info" do
|
||
|
time_entry = TimeEntry.first
|
||
|
time_entry.destroy
|
||
|
p = WebhookPayload.new('time_entry.deleted', time_entry, @dlopper)
|
||
|
h = p.to_h
|
||
|
assert_equal 'time_entry.deleted', h[:type]
|
||
|
assert_equal 4.25, h.dig(:data, :time_entry, :hours)
|
||
|
end
|
||
|
end
|
||