Class: PactBroker::Webhooks::Repository

Inherits:
Object
  • Object
show all
Includes:
Repositories
Defined in:
lib/pact_broker/webhooks/repository.rb

Instance Method Summary collapse

Methods included from Repositories

#label_repository, #matrix_repository, #pact_repository, #pacticipant_repository, #tag_repository, #verification_repository, #version_repository, #webhook_repository

Instance Method Details

#create(uuid, webhook, consumer, provider) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pact_broker/webhooks/repository.rb', line 18

def create uuid, webhook, consumer, provider
  db_webhook = Webhook.from_domain webhook, consumer, provider
  db_webhook.uuid = uuid
  db_webhook.save
  webhook.request.headers.each_pair do | name, value |
    db_webhook.add_header PactBroker::Webhooks::WebhookHeader.from_domain(name, value, db_webhook.id)
  end
  (webhook.events || []).each do | webhook_event |
    db_webhook.add_event(webhook_event)
  end
  find_by_uuid db_webhook.uuid
end

#create_execution(triggered_webhook, webhook_execution_result) ⇒ Object



99
100
101
102
103
104
# File 'lib/pact_broker/webhooks/repository.rb', line 99

def create_execution triggered_webhook, webhook_execution_result
  Execution.create(
    triggered_webhook: triggered_webhook,
    success: webhook_execution_result.success?,
    logs: webhook_execution_result.logs)
end

#create_triggered_webhook(trigger_uuid, webhook, pact, trigger_type) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pact_broker/webhooks/repository.rb', line 81

def create_triggered_webhook trigger_uuid, webhook, pact, trigger_type
  db_webhook = Webhook.where(uuid: webhook.uuid).single_record
  TriggeredWebhook.create(
    status: TriggeredWebhook::STATUS_NOT_RUN,
    pact_publication_id: pact.id,
    webhook: db_webhook,
    webhook_uuid: db_webhook.uuid,
    trigger_uuid: trigger_uuid,
    trigger_type: trigger_type,
    consumer: db_webhook.consumer,
    provider: db_webhook.provider
  )
end

#delete_by_pacticipant(pacticipant) ⇒ Object



53
54
55
56
# File 'lib/pact_broker/webhooks/repository.rb', line 53

def delete_by_pacticipant pacticipant
  Webhook.where(consumer_id: pacticipant.id).destroy
  Webhook.where(provider_id: pacticipant.id).destroy
end

#delete_by_uuid(uuid) ⇒ Object



49
50
51
# File 'lib/pact_broker/webhooks/repository.rb', line 49

def delete_by_uuid uuid
  Webhook.where(uuid: uuid).destroy
end

#delete_executions_by_pacticipant(pacticipants) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pact_broker/webhooks/repository.rb', line 111

def delete_executions_by_pacticipant pacticipants
  # TODO this relationship no longer exists, deprecate in next version
  DeprecatedExecution.where(consumer: pacticipants).delete
  DeprecatedExecution.where(provider: pacticipants).delete
  execution_ids = Execution
    .join(:triggered_webhooks, {id: :triggered_webhook_id})
    .where(Sequel.or(
      Sequel[:triggered_webhooks][:consumer_id] => [*pacticipants].collect(&:id),
      Sequel[:triggered_webhooks][:provider_id] => [*pacticipants].collect(&:id),
    )).all.collect(&:id)
  Execution.where(id: execution_ids).delete
end

#delete_triggered_webhooks_by_pact_publication_ids(pact_publication_ids) ⇒ Object



131
132
133
134
135
136
# File 'lib/pact_broker/webhooks/repository.rb', line 131

def delete_triggered_webhooks_by_pact_publication_ids pact_publication_ids
  triggered_webhook_ids = TriggeredWebhook.where(pact_publication_id: pact_publication_ids).select_for_subquery(:id)
  Execution.where(triggered_webhook_id: triggered_webhook_ids).delete
  TriggeredWebhook.where(id: triggered_webhook_ids).delete
  DeprecatedExecution.where(pact_publication_id: pact_publication_ids).delete
end

#delete_triggered_webhooks_by_pacticipant(pacticipant) ⇒ Object



106
107
108
109
# File 'lib/pact_broker/webhooks/repository.rb', line 106

def delete_triggered_webhooks_by_pacticipant pacticipant
  TriggeredWebhook.where(consumer: pacticipant).delete
  TriggeredWebhook.where(provider: pacticipant).delete
end

#delete_triggered_webhooks_by_webhook_uuid(uuid) ⇒ Object



124
125
126
127
128
129
# File 'lib/pact_broker/webhooks/repository.rb', line 124

def delete_triggered_webhooks_by_webhook_uuid uuid
  triggered_webhook_ids = TriggeredWebhook.where(webhook: Webhook.where(uuid: uuid)).select_for_subquery(:id)
  Execution.where(triggered_webhook_id: triggered_webhook_ids).delete
  DeprecatedExecution.where(webhook_id: Webhook.where(uuid: uuid).select_for_subquery(:id)).delete
  TriggeredWebhook.where(id: triggered_webhook_ids).delete
end

#fail_retrying_triggered_webhooksObject



145
146
147
# File 'lib/pact_broker/webhooks/repository.rb', line 145

def fail_retrying_triggered_webhooks
  TriggeredWebhook.retrying.update(status: TriggeredWebhook::STATUS_FAILURE)
end

#find_allObject



58
59
60
# File 'lib/pact_broker/webhooks/repository.rb', line 58

def find_all
  Webhook.all.collect(&:to_domain)
end

#find_by_consumer_and_provider(consumer, provider) ⇒ Object



62
63
64
# File 'lib/pact_broker/webhooks/repository.rb', line 62

def find_by_consumer_and_provider consumer, provider
  Webhook.where(consumer_id: consumer.id, provider_id: provider.id).collect(&:to_domain)
end

#find_by_consumer_and_provider_and_event_name(consumer, provider, event_name) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/pact_broker/webhooks/repository.rb', line 66

def find_by_consumer_and_provider_and_event_name consumer, provider, event_name
  Webhook
    .select_all_qualified
    .where(consumer_id: consumer.id, provider_id: provider.id)
    .join(:webhook_events, { webhook_id: :id })
    .where(Sequel[:webhook_events][:name] => event_name)
    .collect(&:to_domain)
end

#find_by_consumer_and_provider_existing_at(consumer, provider, date_time) ⇒ Object



75
76
77
78
79
# File 'lib/pact_broker/webhooks/repository.rb', line 75

def find_by_consumer_and_provider_existing_at consumer, provider, date_time
  Webhook.where(consumer_id: consumer.id, provider_id: provider.id)
  .where(Sequel.lit("created_at < ?", date_time))
  .collect(&:to_domain)
end

#find_by_uuid(uuid) ⇒ Object



31
32
33
# File 'lib/pact_broker/webhooks/repository.rb', line 31

def find_by_uuid uuid
  Webhook.where(uuid: uuid).limit(1).collect(&:to_domain)[0]
end

#find_latest_triggered_webhooks(consumer, provider) ⇒ Object



138
139
140
141
142
143
# File 'lib/pact_broker/webhooks/repository.rb', line 138

def find_latest_triggered_webhooks consumer, provider
  LatestTriggeredWebhook
    .where(consumer: consumer, provider: provider)
    .order(:id)
    .all
end

#update_by_uuid(uuid, webhook) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pact_broker/webhooks/repository.rb', line 35

def update_by_uuid uuid, webhook
  existing_webhook = Webhook.find(uuid: uuid)
  existing_webhook.update_from_domain(webhook).save
  existing_webhook.headers.collect(&:delete)
  existing_webhook.events.collect(&:delete)
  webhook.request.headers.each_pair do | name, value |
    existing_webhook.add_header PactBroker::Webhooks::WebhookHeader.from_domain(name, value, existing_webhook.id)
  end
  (webhook.events || []).each do | webhook_event |
    existing_webhook.add_event(webhook_event)
  end
  find_by_uuid uuid
end

#update_triggered_webhook_status(triggered_webhook, status) ⇒ Object



95
96
97
# File 'lib/pact_broker/webhooks/repository.rb', line 95

def update_triggered_webhook_status triggered_webhook, status
  triggered_webhook.update(status: status)
end