bullet_train-outgoing_webhooks-core 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +29 -0
  4. data/Rakefile +8 -0
  5. data/app/assets/config/bullet_train_outgoing_webhooks_core_manifest.js +0 -0
  6. data/app/jobs/webhooks/outgoing/delivery_job.rb +7 -0
  7. data/app/models/concerns/webhooks/outgoing/has_uuid.rb +21 -0
  8. data/app/models/concerns/webhooks/outgoing/issuing_model.rb +49 -0
  9. data/app/models/concerns/webhooks/outgoing/team_support.rb +15 -0
  10. data/app/models/outgoing.rb +5 -0
  11. data/app/models/webhooks/outgoing/delivery.rb +79 -0
  12. data/app/models/webhooks/outgoing/delivery_attempt.rb +65 -0
  13. data/app/models/webhooks/outgoing/endpoint.rb +32 -0
  14. data/app/models/webhooks/outgoing/event.rb +41 -0
  15. data/app/models/webhooks/outgoing/event_type.rb +13 -0
  16. data/app/models/webhooks/outgoing.rb +5 -0
  17. data/config/models/webhooks/outgoing/event_types.yml +9 -0
  18. data/config/routes.rb +2 -0
  19. data/db/migrate/20180420013127_create_webhooks_outgoing_endpoints.rb +10 -0
  20. data/db/migrate/20180420013505_create_webhooks_outgoing_endpoint_event_types.rb +10 -0
  21. data/db/migrate/20180420013852_create_webhooks_outgoing_event_types.rb +9 -0
  22. data/db/migrate/20180420014623_create_webhooks_outgoing_events.rb +12 -0
  23. data/db/migrate/20180420021016_create_webhooks_outgoing_deliveries.rb +11 -0
  24. data/db/migrate/20180420022027_add_team_to_webhooks_outgoing_events.rb +5 -0
  25. data/db/migrate/20180420165345_create_webhooks_outgoing_delivery_attempts.rb +11 -0
  26. data/db/migrate/20180420172112_add_error_message_to_webhooks_outgoing_delivery_attempt.rb +6 -0
  27. data/db/migrate/20181012234232_add_name_to_webhooks_outgoing_endpoints.rb +5 -0
  28. data/db/migrate/20181013030208_add_delivered_at_to_webhooks_outgoing_deliveries.rb +5 -0
  29. data/db/migrate/20181013165056_add_uuid_to_webhooks_outgoing_events.rb +5 -0
  30. data/db/migrate/20181013174539_add_payload_to_webhooks_outgoing_events.rb +5 -0
  31. data/db/migrate/20181013192951_add_attempt_number_to_webhooks_outgoing_delivery_attempts.rb +5 -0
  32. data/db/migrate/20210805060451_change_body_to_data_on_webhooks_outgoing_events.rb +5 -0
  33. data/db/migrate/20211126230846_add_event_type_ids_to_webhooks_outgoing_endpoints.rb +5 -0
  34. data/db/migrate/20211126230847_migrate_event_type_ids_on_webhooks_outgoing_endpoints.rb +8 -0
  35. data/db/migrate/20211127013539_remove_event_type_from_webhooks_outgoing_events.rb +5 -0
  36. data/db/migrate/20211127013800_add_new_event_type_id_to_webhooks_outgoing_events.rb +5 -0
  37. data/db/migrate/20211127014001_migrate_event_types_on_webhooks_outgoing_events.rb +10 -0
  38. data/db/migrate/20211127015712_drop_webhooks_outgoing_endpoint_event_types.rb +9 -0
  39. data/db/migrate/20211127015713_drop_webhooks_outgoing_event_types.rb +9 -0
  40. data/lib/bullet_train/outgoing_webhooks/core/engine.rb +8 -0
  41. data/lib/bullet_train/outgoing_webhooks/core/version.rb +7 -0
  42. data/lib/bullet_train/outgoing_webhooks/core.rb +10 -0
  43. data/lib/tasks/bullet_train/outgoing_webhooks/core_tasks.rake +4 -0
  44. metadata +103 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be13c183fdbc243bb02ccc3053bee0e9daa369eae1ee7a2b35c602365d3115c7
4
+ data.tar.gz: 2def8c82a2551adef85f807ba76741a0b819f58e2ab618ebbdd49eff2cf2ca9e
5
+ SHA512:
6
+ metadata.gz: 668fbd53b8f4271b90ef62d7bc097ee97a3012ccda9d84c52588c7c6cb2193a0ee3a5d29abde1ca88c00d61323c204927e897f9bd5531c074c62a2cf3c9f23a1
7
+ data.tar.gz: 73a1cee86a21509e18887eafda93d7dae31393645dc2a31c9b823190ec51ee056e6947b23ec501442ae4a3e0b6cbdb3eb1193711caa1bd5bd99d0e3460113ca3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Andrew Culver
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Bullet Train Outgoing Webhooks Core
2
+
3
+ Bullet Train Outgoing Webhooks Core allows users of your Rails application to subscribe and receive webhooks when activity takes place in your application. It was developed by [Andrew Culver](https://twitter.com/andrewculver) and extracted from [Bullet Train](https://bullettrain.co).
4
+
5
+ ## Usage
6
+ How to use my plugin.
7
+
8
+ ## Installation
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "bullet_train-outgoing_webhooks-core"
13
+ ```
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+ ```bash
22
+ $ gem install bullet_train-outgoing_webhooks-core
23
+ ```
24
+
25
+ ## Contributing
26
+ Contribution directions go here.
27
+
28
+ ## License
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ class Webhooks::Outgoing::DeliveryJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(delivery)
5
+ delivery.deliver
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module Webhooks::Outgoing::HasUuid
2
+ extend ActiveSupport::Concern
3
+
4
+ # define relationships.
5
+ included do
6
+ after_initialize do
7
+ self.uuid ||= SecureRandom.hex
8
+ end
9
+ end
10
+
11
+ # define class methods.
12
+ module ClassMethods
13
+ end
14
+
15
+ # define object methods.
16
+ def short_uuid
17
+ self.uuid.first(7)
18
+ rescue
19
+ "nil"
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ module Webhooks::Outgoing::IssuingModel
2
+ extend ActiveSupport::Concern
3
+
4
+ # define relationships.
5
+ included do
6
+ after_commit :generate_created_webhook, on: [:create]
7
+ after_commit :generate_updated_webhook, on: [:update]
8
+ after_commit :generate_deleted_webhook, on: [:destroy]
9
+ has_many :webhooks_outgoing_events, as: :subject, class_name: "Webhooks::Outgoing::Event", dependent: :nullify
10
+ end
11
+
12
+ # define class methods.
13
+ module ClassMethods
14
+ end
15
+
16
+ def generate_webhook(action)
17
+ # we can only generate webhooks for objects that return their team.
18
+ return unless respond_to? :team
19
+
20
+ # Try to find an event type definition for this action.
21
+ event_type = Webhooks::Outgoing::EventType.find_by(id: "#{self.class.name.underscore}.#{action}")
22
+
23
+ # If the event type is defined as one that people can be subscribed to,
24
+ # and this object has a team where an associated outgoing webhooks endpoint could be registered.
25
+ if event_type && team
26
+
27
+ # Only generate an event record if an endpoint is actually listening for this event type.
28
+ if team.webhooks_outgoing_endpoints.listening_for_event_type_id(event_type.id).any?
29
+ data = "Api::V1::#{self.class.name}Serializer".constantize.new(self).serializable_hash[:data]
30
+ webhook = team.webhooks_outgoing_events.create(event_type_id: event_type.id, subject: self, data: data)
31
+ webhook.deliver
32
+ end
33
+ end
34
+ end
35
+
36
+ def generate_created_webhook
37
+ generate_webhook("created")
38
+ end
39
+
40
+ def generate_updated_webhook
41
+ generate_webhook("updated")
42
+ end
43
+
44
+ def generate_deleted_webhook
45
+ return false unless respond_to?(:team)
46
+ return false if team&.being_destroyed?
47
+ generate_webhook("deleted")
48
+ end
49
+ end
@@ -0,0 +1,15 @@
1
+ module Webhooks::Outgoing::TeamSupport
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ has_many :webhooks_outgoing_endpoints, class_name: "Webhooks::Outgoing::Endpoint", dependent: :destroy
6
+ has_many :webhooks_outgoing_events, class_name: "Webhooks::Outgoing::Event", dependent: :destroy
7
+
8
+ before_destroy :mark_for_destruction, prepend: true
9
+ end
10
+
11
+ def mark_for_destruction
12
+ # This allows downstream logic to check whether a team is being destroyed in order to bypass webhook issuance.
13
+ update_column(:being_destroyed, true)
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Webhooks
2
+ def self.table_name_prefix
3
+ "webhooks_"
4
+ end
5
+ end
@@ -0,0 +1,79 @@
1
+ class Webhooks::Outgoing::Delivery < ApplicationRecord
2
+ # 🚅 add concerns above.
3
+
4
+ belongs_to :endpoint, class_name: "Webhooks::Outgoing::Endpoint"
5
+ belongs_to :event, class_name: "Webhooks::Outgoing::Event"
6
+ has_one :team, through: :endpoint
7
+
8
+ ATTEMPT_SCHEDULE = {
9
+ 1 => 15.seconds,
10
+ 2 => 1.minute,
11
+ 3 => 5.minutes,
12
+ 4 => 15.minutes,
13
+ 5 => 1.hour,
14
+ }
15
+
16
+ # 🚅 add belongs_to associations above.
17
+
18
+ has_many :delivery_attempts, class_name: "Webhooks::Outgoing::DeliveryAttempt", dependent: :destroy, foreign_key: :delivery_id
19
+ # 🚅 add has_many associations above.
20
+
21
+ # 🚅 add has_one associations above.
22
+
23
+ # 🚅 add scopes above.
24
+
25
+ # 🚅 add validations above.
26
+
27
+ # 🚅 add callbacks above.
28
+
29
+ # 🚅 add delegations above.
30
+
31
+ def label_string
32
+ event.short_uuid
33
+ end
34
+
35
+ def next_reattempt_delay
36
+ ATTEMPT_SCHEDULE[attempt_count]
37
+ end
38
+
39
+ def deliver_async
40
+ if still_attempting?
41
+ Webhooks::Outgoing::DeliveryJob.set(wait: next_reattempt_delay).perform_later(self)
42
+ end
43
+ end
44
+
45
+ def deliver
46
+ if delivery_attempts.create.attempt
47
+ touch(:delivered_at)
48
+ else
49
+ deliver_async
50
+ end
51
+ end
52
+
53
+ def attempt_count
54
+ delivery_attempts.count
55
+ end
56
+
57
+ def delivered?
58
+ delivered_at.present?
59
+ end
60
+
61
+ def still_attempting?
62
+ return false if delivered?
63
+ attempt_count < max_attempts
64
+ end
65
+
66
+ def failed?
67
+ !(delivered? || still_attempting?)
68
+ end
69
+
70
+ def name
71
+ event.short_uuid
72
+ end
73
+
74
+ def max_attempts
75
+ ATTEMPT_SCHEDULE.keys.max
76
+ end
77
+
78
+ # 🚅 add methods above.
79
+ end
@@ -0,0 +1,65 @@
1
+ class Webhooks::Outgoing::DeliveryAttempt < ApplicationRecord
2
+ # 🚅 add concerns above.
3
+
4
+ belongs_to :delivery
5
+ has_one :team, through: :delivery
6
+ scope :successful, -> { where(response_code: 200) }
7
+
8
+ before_create do
9
+ self.attempt_number = delivery.attempt_count + 1
10
+ end
11
+
12
+ def still_attempting?
13
+ error_message.nil? && response_code.nil?
14
+ end
15
+
16
+ def successful?
17
+ [200, 201, 202, 203, 204, 205, 206, 207, 226].include?(response_code)
18
+ end
19
+
20
+ def failed?
21
+ !(successful? || still_attempting?)
22
+ end
23
+
24
+ def attempt
25
+ uri = URI.parse(delivery.endpoint_url)
26
+ http = Net::HTTP.new(uri.host, uri.port)
27
+ http.use_ssl = true if uri.scheme == "https"
28
+ request = Net::HTTP::Post.new(uri.path)
29
+ request.add_field("Content-Type", "application/json")
30
+ request.body = delivery.event.payload.to_json
31
+
32
+ begin
33
+ response = http.request(request)
34
+ self.response_message = response.message
35
+ self.response_code = response.code
36
+ self.response_body = response.body
37
+ rescue Exception => exception
38
+ self.response_code = 0
39
+ self.error_message = exception.message
40
+ end
41
+
42
+ save
43
+ successful?
44
+ end
45
+
46
+ def label_string
47
+ "#{attempt_number.ordinalize} Attempt"
48
+ end
49
+ # 🚅 add belongs_to associations above.
50
+
51
+ # 🚅 add has_many associations above.
52
+
53
+ # 🚅 add has_one associations above.
54
+
55
+ # 🚅 add scopes above.
56
+
57
+ validates :response_code, presence: true
58
+ # 🚅 add validations above.
59
+
60
+ # 🚅 add callbacks above.
61
+
62
+ # 🚅 add delegations above.
63
+
64
+ # 🚅 add methods above.
65
+ end
@@ -0,0 +1,32 @@
1
+ class Webhooks::Outgoing::Endpoint < ApplicationRecord
2
+ # 🚅 add concerns above.
3
+
4
+ belongs_to :team
5
+ # 🚅 add belongs_to associations above.
6
+
7
+ has_many :deliveries, class_name: "Webhooks::Outgoing::Delivery", dependent: :destroy, foreign_key: :endpoint_id
8
+ has_many :events, -> { distinct }, through: :deliveries
9
+ # 🚅 add has_many associations above.
10
+
11
+ # 🚅 add has_one associations above.
12
+
13
+ scope :listening_for_event_type_id, ->(event_type_id) { where("event_type_ids @> ? OR event_type_ids = '[]'::jsonb", "\"#{event_type_id}\"") }
14
+ # 🚅 add scopes above.
15
+
16
+ validates :name, presence: true
17
+ # 🚅 add validations above.
18
+
19
+ # 🚅 add callbacks above.
20
+
21
+ # 🚅 add delegations above.
22
+
23
+ def valid_event_types
24
+ Webhooks::Outgoing::EventType.all
25
+ end
26
+
27
+ def event_types
28
+ event_type_ids.map { |id| Webhooks::Outgoing::EventType.find(id) }
29
+ end
30
+
31
+ # 🚅 add methods above.
32
+ end
@@ -0,0 +1,41 @@
1
+ class Webhooks::Outgoing::Event < ApplicationRecord
2
+ include HasUuid
3
+ belongs_to :team
4
+ belongs_to :event_type, class_name: "Webhooks::Outgoing::EventType"
5
+ belongs_to :subject, polymorphic: true
6
+ has_many :deliveries, dependent: :destroy
7
+
8
+ before_create do
9
+ self.payload = generate_payload
10
+ end
11
+
12
+ def generate_payload
13
+ {
14
+ event_id: uuid,
15
+ event_type: event_type_id,
16
+ subject_id: subject_id,
17
+ subject_type: subject_type,
18
+ data: data
19
+ }
20
+ end
21
+
22
+ def event_type_name
23
+ payload.dig("event_type")
24
+ end
25
+
26
+ def endpoints
27
+ team.webhooks_outgoing_endpoints.listening_for_event_type_id(event_type_id)
28
+ end
29
+
30
+ def deliver
31
+ endpoints.each do |endpoint|
32
+ unless endpoint.deliveries.where(event: self).any?
33
+ endpoint.deliveries.create(event: self, endpoint_url: endpoint.url).deliver_async
34
+ end
35
+ end
36
+ end
37
+
38
+ def label_string
39
+ short_uuid
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ class Webhooks::Outgoing::EventType < ApplicationHash
2
+ self.data = YAML.load_file("config/models/webhooks/outgoing/event_types.yml").map do |topic, events|
3
+ events.map { |event| event == "crud" ? ["created", "updated", "deleted"] : event }.flatten.map { |event| {id: "#{topic}.#{event}"} }
4
+ end.flatten
5
+
6
+ def label_string
7
+ name
8
+ end
9
+
10
+ def name
11
+ id
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Webhooks::Outgoing
2
+ def self.table_name_prefix
3
+ "webhooks_outgoing_"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ scaffolding/absolutely_abstract/creative_concept:
2
+ # This automatically generates `created`, `updated`, and `deleted` webhook events.
3
+ - crud
4
+ scaffolding/completely_concrete/tangible_thing:
5
+ - crud
6
+ invitation:
7
+ - crud
8
+ membership:
9
+ - crud
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,10 @@
1
+ class CreateWebhooksOutgoingEndpoints < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :webhooks_outgoing_endpoints do |t|
4
+ t.references :team, foreign_key: true
5
+ t.text :url
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class CreateWebhooksOutgoingEndpointEventTypes < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :webhooks_outgoing_endpoint_event_types do |t|
4
+ t.integer :endpoint_id
5
+ t.integer :event_type_id
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class CreateWebhooksOutgoingEventTypes < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :webhooks_outgoing_event_types do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class CreateWebhooksOutgoingEvents < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :webhooks_outgoing_events do |t|
4
+ t.integer :event_type_id
5
+ t.integer :subject_id
6
+ t.string :subject_type
7
+ t.jsonb :body
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ class CreateWebhooksOutgoingDeliveries < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :webhooks_outgoing_deliveries do |t|
4
+ t.integer :endpoint_id
5
+ t.integer :event_id
6
+ t.text :endpoint_url
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class AddTeamToWebhooksOutgoingEvents < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_reference :webhooks_outgoing_events, :team, foreign_key: true
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ class CreateWebhooksOutgoingDeliveryAttempts < ActiveRecord::Migration[5.2]
2
+ def change
3
+ create_table :webhooks_outgoing_delivery_attempts do |t|
4
+ t.integer :delivery_id
5
+ t.integer :response_code
6
+ t.text :response_body
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ class AddErrorMessageToWebhooksOutgoingDeliveryAttempt < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :webhooks_outgoing_delivery_attempts, :response_message, :text
4
+ add_column :webhooks_outgoing_delivery_attempts, :error_message, :text
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class AddNameToWebhooksOutgoingEndpoints < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :webhooks_outgoing_endpoints, :name, :string
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddDeliveredAtToWebhooksOutgoingDeliveries < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :webhooks_outgoing_deliveries, :delivered_at, :datetime
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddUuidToWebhooksOutgoingEvents < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :webhooks_outgoing_events, :uuid, :string
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddPayloadToWebhooksOutgoingEvents < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :webhooks_outgoing_events, :payload, :jsonb
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddAttemptNumberToWebhooksOutgoingDeliveryAttempts < ActiveRecord::Migration[5.2]
2
+ def change
3
+ add_column :webhooks_outgoing_delivery_attempts, :attempt_number, :integer
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class ChangeBodyToDataOnWebhooksOutgoingEvents < ActiveRecord::Migration[6.1]
2
+ def change
3
+ rename_column :webhooks_outgoing_events, :body, :data
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddEventTypeIdsToWebhooksOutgoingEndpoints < ActiveRecord::Migration[6.1]
2
+ def change
3
+ add_column :webhooks_outgoing_endpoints, :event_type_ids, :jsonb, default: []
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class MigrateEventTypeIdsOnWebhooksOutgoingEndpoints < ActiveRecord::Migration[6.1]
2
+ def change
3
+ Webhooks::Outgoing::Endpoint.find_each do |endpoint|
4
+ event_type_ids = ActiveRecord::Base.connection.execute("SELECT * FROM webhooks_outgoing_endpoint_event_types JOIN webhooks_outgoing_event_types ON webhooks_outgoing_endpoint_event_types.event_type_id = webhooks_outgoing_event_types.id WHERE endpoint_id = #{endpoint.id}").to_a.map { |result| result.dig("name") }
5
+ endpoint.update(event_type_ids: event_type_ids)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class RemoveEventTypeFromWebhooksOutgoingEvents < ActiveRecord::Migration[6.1]
2
+ def change
3
+ remove_reference :webhooks_outgoing_events, :event_type, null: false, foreign_key: false
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class AddNewEventTypeIdToWebhooksOutgoingEvents < ActiveRecord::Migration[6.1]
2
+ def change
3
+ add_column :webhooks_outgoing_events, :event_type_id, :string
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ class MigrateEventTypesOnWebhooksOutgoingEvents < ActiveRecord::Migration[6.1]
2
+ def up
3
+ Webhooks::Outgoing::Event.find_each do |event|
4
+ event.update_column(:event_type_id, event.payload.dig("event_type"))
5
+ end
6
+ end
7
+
8
+ def down
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class DropWebhooksOutgoingEndpointEventTypes < ActiveRecord::Migration[6.1]
2
+ def up
3
+ drop_table :webhooks_outgoing_endpoint_event_types
4
+ end
5
+
6
+ def down
7
+ raise ActiveRecord::IrreversibleMigration
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class DropWebhooksOutgoingEventTypes < ActiveRecord::Migration[6.1]
2
+ def up
3
+ drop_table :webhooks_outgoing_event_types
4
+ end
5
+
6
+ def down
7
+ raise ActiveRecord::IrreversibleMigration
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module BulletTrain
2
+ module OutgoingWebhooks
3
+ module Core
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module BulletTrain
2
+ module OutgoingWebhooks
3
+ module Core
4
+ VERSION = "1.0.2"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require "bullet_train/outgoing_webhooks/core/version"
2
+ require "bullet_train/outgoing_webhooks/core/engine"
3
+
4
+ module BulletTrain
5
+ module OutgoingWebhooks
6
+ module Core
7
+ # Your code goes here...
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bullet_train_outgoing_webhooks_core do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bullet_train-outgoing_webhooks-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Culver
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-01-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ description: Allow users of your Rails application to subscribe and receive webhooks
28
+ when activity takes place in your application.
29
+ email:
30
+ - andrew.culver@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - MIT-LICENSE
36
+ - README.md
37
+ - Rakefile
38
+ - app/assets/config/bullet_train_outgoing_webhooks_core_manifest.js
39
+ - app/jobs/webhooks/outgoing/delivery_job.rb
40
+ - app/models/concerns/webhooks/outgoing/has_uuid.rb
41
+ - app/models/concerns/webhooks/outgoing/issuing_model.rb
42
+ - app/models/concerns/webhooks/outgoing/team_support.rb
43
+ - app/models/outgoing.rb
44
+ - app/models/webhooks/outgoing.rb
45
+ - app/models/webhooks/outgoing/delivery.rb
46
+ - app/models/webhooks/outgoing/delivery_attempt.rb
47
+ - app/models/webhooks/outgoing/endpoint.rb
48
+ - app/models/webhooks/outgoing/event.rb
49
+ - app/models/webhooks/outgoing/event_type.rb
50
+ - config/models/webhooks/outgoing/event_types.yml
51
+ - config/routes.rb
52
+ - db/migrate/20180420013127_create_webhooks_outgoing_endpoints.rb
53
+ - db/migrate/20180420013505_create_webhooks_outgoing_endpoint_event_types.rb
54
+ - db/migrate/20180420013852_create_webhooks_outgoing_event_types.rb
55
+ - db/migrate/20180420014623_create_webhooks_outgoing_events.rb
56
+ - db/migrate/20180420021016_create_webhooks_outgoing_deliveries.rb
57
+ - db/migrate/20180420022027_add_team_to_webhooks_outgoing_events.rb
58
+ - db/migrate/20180420165345_create_webhooks_outgoing_delivery_attempts.rb
59
+ - db/migrate/20180420172112_add_error_message_to_webhooks_outgoing_delivery_attempt.rb
60
+ - db/migrate/20181012234232_add_name_to_webhooks_outgoing_endpoints.rb
61
+ - db/migrate/20181013030208_add_delivered_at_to_webhooks_outgoing_deliveries.rb
62
+ - db/migrate/20181013165056_add_uuid_to_webhooks_outgoing_events.rb
63
+ - db/migrate/20181013174539_add_payload_to_webhooks_outgoing_events.rb
64
+ - db/migrate/20181013192951_add_attempt_number_to_webhooks_outgoing_delivery_attempts.rb
65
+ - db/migrate/20210805060451_change_body_to_data_on_webhooks_outgoing_events.rb
66
+ - db/migrate/20211126230846_add_event_type_ids_to_webhooks_outgoing_endpoints.rb
67
+ - db/migrate/20211126230847_migrate_event_type_ids_on_webhooks_outgoing_endpoints.rb
68
+ - db/migrate/20211127013539_remove_event_type_from_webhooks_outgoing_events.rb
69
+ - db/migrate/20211127013800_add_new_event_type_id_to_webhooks_outgoing_events.rb
70
+ - db/migrate/20211127014001_migrate_event_types_on_webhooks_outgoing_events.rb
71
+ - db/migrate/20211127015712_drop_webhooks_outgoing_endpoint_event_types.rb
72
+ - db/migrate/20211127015713_drop_webhooks_outgoing_event_types.rb
73
+ - lib/bullet_train/outgoing_webhooks/core.rb
74
+ - lib/bullet_train/outgoing_webhooks/core/engine.rb
75
+ - lib/bullet_train/outgoing_webhooks/core/version.rb
76
+ - lib/tasks/bullet_train/outgoing_webhooks/core_tasks.rake
77
+ homepage: https://github.com/bullet-train-co/bullet_train-outgoing_webhooks-core
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/bullet-train-co/bullet_train-outgoing_webhooks-core
82
+ source_code_uri: https://github.com/bullet-train-co/bullet_train-outgoing_webhooks-core
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.2.22
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Allow users of your Rails application to subscribe and receive webhooks when
102
+ activity takes place in your application.
103
+ test_files: []