20210118095820_issue_3372_webhooks_admin_view.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Issue3372WebhooksAdminView < ActiveRecord::Migration[5.2]
  3. def up
  4. return if !Setting.exists?(name: 'system_init_done')
  5. create_webhooks_table
  6. record_upgrade
  7. Permission.create_if_not_exists(
  8. name: 'admin.webhook',
  9. note: 'Manage %s',
  10. preferences: {
  11. translations: ['Webhooks']
  12. },
  13. )
  14. end
  15. def create_webhooks_table
  16. create_table :webhooks do |t|
  17. t.column :name, :string, limit: 250, null: false
  18. t.column :endpoint, :string, limit: 300, null: false
  19. t.column :signature_token, :string, limit: 200, null: true
  20. t.column :ssl_verify, :boolean, null: false, default: true
  21. t.column :note, :string, limit: 500, null: true
  22. t.column :active, :boolean, null: false, default: true
  23. t.column :updated_by_id, :integer, null: false
  24. t.column :created_by_id, :integer, null: false
  25. t.timestamps limit: 3, null: false
  26. end
  27. end
  28. def record_upgrade
  29. Trigger.all.find_each do |trigger|
  30. next if trigger.perform.dig('notification.webhook', 'endpoint').blank?
  31. webhook = webhook_create(
  32. source: trigger.name,
  33. config: trigger.perform['notification.webhook'],
  34. )
  35. trigger.perform['notification.webhook'] = { webhook_id: webhook.id }
  36. trigger.save!
  37. end
  38. end
  39. def webhook_create(source:, config:)
  40. Webhook.create!(
  41. name: "Webhook '#{source}'",
  42. endpoint: config['endpoint'],
  43. signature_token: config['token'],
  44. ssl_verify: config['verify_ssl'] || false,
  45. active: true,
  46. created_by_id: 1,
  47. updated_by_id: 1,
  48. )
  49. end
  50. end