Project

General

Profile

Feature #29664 » 0001-Adds-webooks-for-time-entries-29664.patch

Marius BĂLTEANU, 2025-12-04 23:41

View differences:

app/models/time_entry.rb
58 58
  before_validation :set_author_if_nil
59 59
  validate :validate_time_entry
60 60

  
61
  after_create_commit  ->{ Webhook.trigger('time_entry.created', self) }
62
  after_update_commit  ->{ Webhook.trigger('time_entry.updated', self) }
63
  after_destroy_commit ->{ Webhook.trigger('time_entry.deleted', self) }
64

  
61 65
  scope :visible, (lambda do |*args|
62 66
    joins(:project).
63 67
    where(TimeEntry.visible_condition(args.shift || User.current, *args))
app/models/webhook_payload.rb
29 29

  
30 30
  EVENTS = {
31 31
    issue: %w[created updated deleted],
32
    wiki_page: %w[created updated deleted]
32
    wiki_page: %w[created updated deleted],
33
    time_entry: %w[created updated deleted],
33 34
  }
34 35

  
35 36
  def to_h
......
110 111
    }
111 112
  end
112 113

  
114
  def time_entry_payload(action)
115
    time_entry = object
116
    ts = case action
117
         when 'created'
118
           time_entry.created_on
119
         when 'deleted'
120
           Time.now
121
         else
122
           time_entry.updated_on
123
         end
124
    {
125
      type: event,
126
      timestamp: ts.iso8601,
127
      data: {
128
        time_entry: ApiRenderer.new("app/views/timelog/show.api.rsb", user).to_h(time_entry: time_entry)
129
      }
130
    }
131
  end
132

  
113 133
  # given a path to an API template (relative to RAILS_ROOT), renders it and returns the resulting hash
114 134
  class ApiRenderer
115 135
    include ApplicationHelper
config/locales/en.yml
1193 1193
  webhook_events_wiki_page_created: Wiki page created
1194 1194
  webhook_events_wiki_page_updated: Wiki page updated
1195 1195
  webhook_events_wiki_page_deleted: Wiki page deleted
1196
  webhook_events_time_entry: Time entries
1197
  webhook_events_time_entry_created: Time entry created
1198
  webhook_events_time_entry_updated: Time entry updated
1199
  webhook_events_time_entry_deleted: Time entry deleted
1196 1200
  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.
1197
  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.  
1201
  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.
1198 1202

  
1199 1203
  button_login: Login
1200 1204
  button_submit: Submit
test/unit/webhook_payload_test.rb
94 94
    assert_equal 'wiki_page.deleted', h[:type]
95 95
    assert_equal 'Test_Page', h.dig(:data, :wiki_page, :title)
96 96
  end
97

  
98
  test "time entry created payload should contain time entry details" do
99
    time_entry = TimeEntry.generate!
100

  
101
    p = WebhookPayload.new('time_entry.created', time_entry, @dlopper)
102
    assert h = p.to_h
103
    assert_equal 'time_entry.created', h[:type]
104
    assert_equal time_entry.hours, h.dig(:data, :time_entry, :hours)
105
  end
106

  
107
  test "time entry updated payload should contain updated timestamp" do
108
    time_entry = TimeEntry.first
109

  
110
    time_entry.hours = 2.5
111
    time_entry.save!
112

  
113
    p = WebhookPayload.new('time_entry.updated', time_entry, @dlopper)
114
    h = p.to_h
115
    assert_equal 'time_entry.updated', h[:type]
116
    assert_equal 2.5, h.dig(:data, :time_entry, :hours)
117
  end
118

  
119
  test "time entry deleted payload should contain basic info" do
120
    time_entry = TimeEntry.first
121
    time_entry.destroy
122

  
123
    p = WebhookPayload.new('time_entry.deleted', time_entry, @dlopper)
124
    h = p.to_h
125
    assert_equal 'time_entry.deleted', h[:type]
126
    assert_equal 4.25, h.dig(:data, :time_entry, :hours)
127
  end
97 128
end
(15-15/15)