trigger_webhook_job.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. class TriggerWebhookJob < ApplicationJob
  2. USER_ATTRIBUTE_BLACKLIST = %w[
  3. last_login
  4. login_failed
  5. password
  6. preferences
  7. group_ids
  8. groups
  9. authorization_ids
  10. authorizations
  11. ].freeze
  12. attr_reader :ticket, :trigger, :article
  13. retry_on TriggerWebhookJob::RequestError, attempts: 5, wait: lambda { |executions|
  14. executions * 10.seconds
  15. }
  16. def perform(trigger, ticket, article)
  17. @trigger = trigger
  18. @ticket = ticket
  19. @article = article
  20. return if request.success?
  21. raise TriggerWebhookJob::RequestError
  22. end
  23. private
  24. def request
  25. UserAgent.post(
  26. config['endpoint'],
  27. payload,
  28. {
  29. json: true,
  30. jsonParseDisable: true,
  31. open_timeout: 4,
  32. read_timeout: 30,
  33. total_timeout: 60,
  34. headers: headers,
  35. signature_token: config['token'],
  36. verify_ssl: verify_ssl?,
  37. log: {
  38. facility: 'webhook',
  39. },
  40. },
  41. )
  42. end
  43. def config
  44. @config ||= trigger.perform['notification.webhook']
  45. end
  46. def verify_ssl?
  47. config.fetch('verify_ssl', false).present?
  48. end
  49. def headers
  50. {
  51. 'X-Zammad-Trigger' => trigger.name,
  52. 'X-Zammad-Delivery' => job_id
  53. }
  54. end
  55. def payload
  56. {
  57. ticket: TriggerWebhookJob::RecordPayload.generate(ticket),
  58. article: TriggerWebhookJob::RecordPayload.generate(article),
  59. }
  60. end
  61. end