From ad81d2076520b16a7dc0b88a677aff2485e75c93 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 21 Aug 2017 20:06:47 +1000 Subject: [PATCH] feat(webhook status): add endpoint for triggered webhook execution logs --- .../38_create_triggered_webhooks_table.rb | 1 + lib/pact_broker/api.rb | 1 + .../api/resources/triggered_webhook_logs.rb | 36 +++++++++++++++++++ .../resources/triggered_webhook_logs_spec.rb | 28 +++++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 lib/pact_broker/api/resources/triggered_webhook_logs.rb create mode 100644 spec/lib/pact_broker/api/resources/triggered_webhook_logs_spec.rb diff --git a/db/migrations/38_create_triggered_webhooks_table.rb b/db/migrations/38_create_triggered_webhooks_table.rb index 3c14ddc4c..27b8d137b 100644 --- a/db/migrations/38_create_triggered_webhooks_table.rb +++ b/db/migrations/38_create_triggered_webhooks_table.rb @@ -12,6 +12,7 @@ String :status, null: false DateTime :created_at, null: false DateTime :updated_at, null: false + add_index [:webhook_id, :trigger_uuid], unique: true, name: 'uq_triggered_webhook_wi' add_index [:pact_publication_id, :webhook_id, :trigger_uuid], unique: true, name: 'uq_triggered_webhook_ppi_wi' end end diff --git a/lib/pact_broker/api.rb b/lib/pact_broker/api.rb index 2d9fc240a..163844a91 100644 --- a/lib/pact_broker/api.rb +++ b/lib/pact_broker/api.rb @@ -52,6 +52,7 @@ module PactBroker add ['webhooks', 'provider', :provider_name, 'consumer', :consumer_name, 'status' ], Api::Resources::PactWebhooksStatus, {resource_name: "pact_webhooks"} add ['webhooks', :uuid ], Api::Resources::Webhook, {resource_name: "webhook"} + add ['webhooks', :uuid, 'trigger', :trigger_uuid, 'logs' ], Api::Resources::TriggeredWebhookLogs, {resource_name: "triggered_webhook_logs"} add ['webhooks', :uuid, 'execute' ], Api::Resources::WebhookExecution, {resource_name: "execute_webhook"} add ['webhooks'], Api::Resources::Webhooks, {resource_name: "webhooks"} diff --git a/lib/pact_broker/api/resources/triggered_webhook_logs.rb b/lib/pact_broker/api/resources/triggered_webhook_logs.rb new file mode 100644 index 000000000..3fbf74154 --- /dev/null +++ b/lib/pact_broker/api/resources/triggered_webhook_logs.rb @@ -0,0 +1,36 @@ +require 'pact_broker/api/resources/base_resource' +require 'pact_broker/webhooks/triggered_webhook' + +module PactBroker + module Api + module Resources + + class TriggeredWebhookLogs < BaseResource + + def content_types_provided + [["text/plain", :to_text]] + end + + def allowed_methods + ["GET"] + end + + def resource_exists? + triggered_webhook + end + + def to_text + # Too simple to bother putting into a service + triggered_webhook.webhook_executions.collect(&:logs).join("\n") + end + + def triggered_webhook + @triggered_webhook ||= begin + criteria = {webhook_uuid: identifier_from_path[:uuid], trigger_uuid: identifier_from_path[:trigger_uuid]} + PactBroker::Webhooks::TriggeredWebhook.where(criteria).single_record + end + end + end + end + end +end diff --git a/spec/lib/pact_broker/api/resources/triggered_webhook_logs_spec.rb b/spec/lib/pact_broker/api/resources/triggered_webhook_logs_spec.rb new file mode 100644 index 000000000..ba9c50679 --- /dev/null +++ b/spec/lib/pact_broker/api/resources/triggered_webhook_logs_spec.rb @@ -0,0 +1,28 @@ +require 'pact_broker/api/resources/triggered_webhook_logs' + +module PactBroker + module Api + module Resources + describe TriggeredWebhookLogs do + + let(:td) { TestDataBuilder.new } + + before do + td.create_pact_with_hierarchy + .create_webhook(uuid: "5432") + .create_triggered_webhook(trigger_uuid: "1234") + .create_webhook_execution(logs: "foo") + .create_webhook_execution(logs: "bar") + end + + let(:path) { "/webhooks/5432/trigger/1234/logs" } + + subject { get path; last_response } + + it "returns the concatenated webhook execution logs" do + expect(subject.body).to eq "foo\nbar" + end + end + end + end +end