Skip to content

Commit ad81d20

Browse files
committed
feat(webhook status): add endpoint for triggered webhook execution logs
1 parent dc283ad commit ad81d20

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

db/migrations/38_create_triggered_webhooks_table.rb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
String :status, null: false
1313
DateTime :created_at, null: false
1414
DateTime :updated_at, null: false
15+
add_index [:webhook_id, :trigger_uuid], unique: true, name: 'uq_triggered_webhook_wi'
1516
add_index [:pact_publication_id, :webhook_id, :trigger_uuid], unique: true, name: 'uq_triggered_webhook_ppi_wi'
1617
end
1718
end

lib/pact_broker/api.rb

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module PactBroker
5252
add ['webhooks', 'provider', :provider_name, 'consumer', :consumer_name, 'status' ], Api::Resources::PactWebhooksStatus, {resource_name: "pact_webhooks"}
5353

5454
add ['webhooks', :uuid ], Api::Resources::Webhook, {resource_name: "webhook"}
55+
add ['webhooks', :uuid, 'trigger', :trigger_uuid, 'logs' ], Api::Resources::TriggeredWebhookLogs, {resource_name: "triggered_webhook_logs"}
5556
add ['webhooks', :uuid, 'execute' ], Api::Resources::WebhookExecution, {resource_name: "execute_webhook"}
5657
add ['webhooks'], Api::Resources::Webhooks, {resource_name: "webhooks"}
5758

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'pact_broker/api/resources/base_resource'
2+
require 'pact_broker/webhooks/triggered_webhook'
3+
4+
module PactBroker
5+
module Api
6+
module Resources
7+
8+
class TriggeredWebhookLogs < BaseResource
9+
10+
def content_types_provided
11+
[["text/plain", :to_text]]
12+
end
13+
14+
def allowed_methods
15+
["GET"]
16+
end
17+
18+
def resource_exists?
19+
triggered_webhook
20+
end
21+
22+
def to_text
23+
# Too simple to bother putting into a service
24+
triggered_webhook.webhook_executions.collect(&:logs).join("\n")
25+
end
26+
27+
def triggered_webhook
28+
@triggered_webhook ||= begin
29+
criteria = {webhook_uuid: identifier_from_path[:uuid], trigger_uuid: identifier_from_path[:trigger_uuid]}
30+
PactBroker::Webhooks::TriggeredWebhook.where(criteria).single_record
31+
end
32+
end
33+
end
34+
end
35+
end
36+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'pact_broker/api/resources/triggered_webhook_logs'
2+
3+
module PactBroker
4+
module Api
5+
module Resources
6+
describe TriggeredWebhookLogs do
7+
8+
let(:td) { TestDataBuilder.new }
9+
10+
before do
11+
td.create_pact_with_hierarchy
12+
.create_webhook(uuid: "5432")
13+
.create_triggered_webhook(trigger_uuid: "1234")
14+
.create_webhook_execution(logs: "foo")
15+
.create_webhook_execution(logs: "bar")
16+
end
17+
18+
let(:path) { "/webhooks/5432/trigger/1234/logs" }
19+
20+
subject { get path; last_response }
21+
22+
it "returns the concatenated webhook execution logs" do
23+
expect(subject.body).to eq "foo\nbar"
24+
end
25+
end
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)