Class: PactBroker::Webhooks::Repository

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

Constant Summary

Constants included from Repositories

Repositories::REPOSITORY_FACTORIES

Instance Method Summary collapse

Methods included from Repositories

#branch_repository, #branch_version_repository, #get_repository, #integration_repository, #label_repository, #matrix_repository, #pact_repository, #pacticipant_repository, #register_default_repositories, #register_repository, #tag_repository, #verification_repository, #version_repository, #webhook_repository

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Instance Method Details

#any_webhooks_configured_for_pact?(pact) ⇒ Boolean

needs to know if there are any at all, regardless of whether or not user can edit them

Returns:

  • (Boolean)


70
71
72
# File 'lib/pact_broker/webhooks/repository.rb', line 70

def any_webhooks_configured_for_pact?(pact)
  deliberately_unscoped(Webhook).find_by_consumer_and_or_provider(pact.consumer, pact.provider).any?
end

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



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

def create uuid, webhook, consumer, provider
  consumer = find_pacticipant_by_name(webhook.consumer) || consumer
  provider = find_pacticipant_by_name(webhook.provider) || provider
  db_webhook = Webhook.from_domain webhook, consumer, provider
  db_webhook.uuid = uuid
  db_webhook.save
  (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



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pact_broker/webhooks/repository.rb', line 125

def create_execution triggered_webhook, webhook_execution_result
  # TriggeredWebhook may have been deleted since the webhook execution started
  if deliberately_unscoped(TriggeredWebhook).where(id: triggered_webhook.id).any?
    Execution.create(
      triggered_webhook: triggered_webhook,
      success: webhook_execution_result.success?,
      logs: webhook_execution_result.logs)
  else
    logger.info("Could not save webhook execution for triggered webhook with id #{triggered_webhook.id} as it has been delete from the database")
  end
end

#create_triggered_webhook(uuid, webhook, pact, verification, trigger_type, event_name, event_context) ⇒ Object

rubocop: disable Metrics/ParameterLists



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pact_broker/webhooks/repository.rb', line 99

def create_triggered_webhook uuid, webhook, pact, verification, trigger_type, event_name, event_context
  db_webhook = deliberately_unscoped(Webhook).where(uuid: webhook.uuid).single_record
  # trigger_uuid was meant to be one per *event*, not one per triggered webhook, but its intent got lost over time.
  # Retiring it now in favour of uuid, but can't leave it empty because
  # it has a not-null and unique webhook_uuid/trigger_uuid constraint on it.
  TriggeredWebhook.create(
    uuid: uuid,
    status: TriggeredWebhook::STATUS_NOT_RUN,
    pact_publication_id: pact.id,
    verification: verification,
    webhook: db_webhook,
    webhook_uuid: db_webhook.uuid,
    trigger_uuid: uuid,
    trigger_type: trigger_type,
    consumer: pact.consumer,
    provider: pact.provider,
    event_name: event_name,
    event_context: event_context
  )
end

#delete_by_consumer_and_provider(consumer, provider) ⇒ Object

deleting a particular integration do delete triggered webhooks only delete stuff matching both, as other integrations may still be present



81
82
83
84
85
86
87
# File 'lib/pact_broker/webhooks/repository.rb', line 81

def delete_by_consumer_and_provider consumer, provider
  webhooks_to_delete = deliberately_unscoped(Webhook).where(consumer: consumer, provider: provider)
  TriggeredWebhook.where(webhook: webhooks_to_delete).delete
  # Delete the orphaned triggerred webhooks
  TriggeredWebhook.where(consumer: consumer, provider: provider).delete
  webhooks_to_delete.delete
end

#delete_by_pacticipant(pacticipant) ⇒ Object

policy applied at resource level for pacticipant If we’re deleting the pacticipant, then we actually do want to delete the triggered webhooks



55
56
57
58
59
60
61
62
# File 'lib/pact_broker/webhooks/repository.rb', line 55

def delete_by_pacticipant pacticipant
  deliberately_unscoped(TriggeredWebhook).where(consumer_id: pacticipant.id).delete
  deliberately_unscoped(TriggeredWebhook).where(provider_id: pacticipant.id).delete
  deliberately_unscoped(TriggeredWebhook).where(webhook: deliberately_unscoped(Webhook).where(consumer_id: pacticipant.id)).delete
  deliberately_unscoped(TriggeredWebhook).where(webhook: deliberately_unscoped(Webhook).where(provider_id: pacticipant.id)).delete
  deliberately_unscoped(Webhook).where(consumer_id: pacticipant.id).delete
  deliberately_unscoped(Webhook).where(provider_id: pacticipant.id).delete
end

#delete_by_uuid(uuid) ⇒ Object

policy applied at resource level



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

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

#delete_triggered_webhooks_by_pact_publication_ids(pact_publication_ids) ⇒ Object



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

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)
  delete_triggered_webhooks_and_executions(triggered_webhook_ids)
end

#delete_triggered_webhooks_by_verification_ids(verification_ids) ⇒ Object



142
143
144
# File 'lib/pact_broker/webhooks/repository.rb', line 142

def delete_triggered_webhooks_by_verification_ids verification_ids
  delete_triggered_webhooks_and_executions(TriggeredWebhook.where(verification_id: verification_ids).select_for_subquery(:id))
end

#delete_triggered_webhooks_by_version_id(version_id) ⇒ Object



137
138
139
140
# File 'lib/pact_broker/webhooks/repository.rb', line 137

def delete_triggered_webhooks_by_version_id version_id
  delete_triggered_webhooks_by_pact_publication_ids(PactBroker::Pacts::PactPublication.where(consumer_version_id: version_id).select_for_subquery(:id))
  delete_triggered_webhooks_by_verification_ids(PactBroker::Domain::Verification.where(provider_version_id: version_id).select_for_subquery(:id))
end

#fail_retrying_triggered_webhooksObject



183
184
185
# File 'lib/pact_broker/webhooks/repository.rb', line 183

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

#find_allObject

this needs the scope!



65
66
67
# File 'lib/pact_broker/webhooks/repository.rb', line 65

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

#find_by_consumer_and_provider(consumer, provider) ⇒ Object



74
75
76
# File 'lib/pact_broker/webhooks/repository.rb', line 74

def find_by_consumer_and_provider consumer, provider
  scope_for(Webhook).find_by_consumer_and_provider(consumer, provider).collect(&:to_domain)
end

#find_by_uuid(uuid) ⇒ Object

policy applied at resource level



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

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

#find_latest_triggered_webhooks(consumer, provider) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/pact_broker/webhooks/repository.rb', line 159

def find_latest_triggered_webhooks consumer, provider
  # policy already applied to pact
  scope_for(TriggeredWebhook)
    .where(consumer: consumer, provider: provider)
    .latest_triggered_webhooks
    .all
end

#find_latest_triggered_webhooks_for_pact(pact) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/pact_broker/webhooks/repository.rb', line 151

def find_latest_triggered_webhooks_for_pact pact
  if pact
    find_latest_triggered_webhooks(pact.consumer, pact.provider)
  else
    []
  end
end

#find_triggered_webhooks_for_pact(pact) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/pact_broker/webhooks/repository.rb', line 167

def find_triggered_webhooks_for_pact pact
  scope_for(PactBroker::Webhooks::TriggeredWebhook)
    .where(pact_publication_id: pact.pact_publication_id)
    .eager(:webhook)
    .eager(:webhook_executions)
    .reverse(:created_at, :id)
end

#find_triggered_webhooks_for_verification(verification) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/pact_broker/webhooks/repository.rb', line 175

def find_triggered_webhooks_for_verification verification
  scope_for(PactBroker::Webhooks::TriggeredWebhook)
    .where(verification_id: verification.id)
    .eager(:webhook)
    .eager(:webhook_executions)
    .reverse(:created_at, :id)
end

#find_webhooks_to_trigger(consumer:, provider:, event_name:) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/pact_broker/webhooks/repository.rb', line 89

def find_webhooks_to_trigger consumer: , provider: , event_name:
  deliberately_unscoped(Webhook)
    .select_all_qualified
    .enabled
    .for_event_name(event_name)
    .find_by_consumer_and_or_provider(consumer, provider)
    .collect(&:to_domain)
end

#update_by_uuid(uuid, webhook) ⇒ Object

policy applied at resource level



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

def update_by_uuid uuid, webhook
  existing_webhook = deliberately_unscoped(Webhook).find(uuid: uuid)
  existing_webhook.consumer_id = find_pacticipant_by_name(webhook.consumer)&.id
  existing_webhook.provider_id = find_pacticipant_by_name(webhook.provider)&.id
  existing_webhook.update_from_domain(webhook).save
  existing_webhook.events.collect(&:delete)
  (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

rubocop: enable Metrics/ParameterLists



121
122
123
# File 'lib/pact_broker/webhooks/repository.rb', line 121

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