20210118095820_issue_3372_webhooks_admin_view.rb 1.8 KB

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