Skip to content

Commit

Permalink
feat: allow dashboard resource to be filtered by consumer/provider
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 13, 2019
1 parent 0166930 commit a240852
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ module PactBroker
add ['matrix'], Api::Resources::Matrix, {resource_name: "matrix"}

add ['dashboard'], Api::Resources::Dashboard, {resource_name: "dashboard"}
add ['dashboard', 'provider', :provider_name, 'consumer', :consumer_name ], Api::Resources::Dashboard, {resource_name: "integration_dashboard"}
add ['test','error'], Api::Resources::ErrorTest, {resource_name: "error_test"}

add ['integrations'], Api::Resources::Integrations, {resource_name: "integrations"}
Expand Down
10 changes: 10 additions & 0 deletions lib/pact_broker/api/decorators/integration_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
require_relative 'base_decorator'
require 'pact_broker/api/pact_broker_urls'

module PactBroker
module Api
module Decorators
class IntegrationDecorator < BaseDecorator
include PactBroker::Api::PactBrokerUrls

property :consumer do
property :name
end

property :provider do
property :name
end

link "pb:dashboard" do | options |
{
title: "BETA: Pacts to show on the dashboard",
href: dashboard_url_for_integration(represented.consumer.name, represented.provider.name, options.fetch(:base_url))
}
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def integration_url consumer_name, provider_name, base_url = ""
"#{base_url}/integrations/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}"
end

def dashboard_url_for_integration consumer_name, provider_name, base_url = ""
"#{base_url}/dashboard/provider/#{url_encode(provider_name)}/consumer/#{url_encode(consumer_name)}"
end

def previous_distinct_diff_url pact, base_url
pact_url(base_url, pact) + "/diff/previous-distinct"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def to_text
private

def index_items
index_service.find_index_items(tags: true)
index_service.find_index_items(identifier_from_path.merge(tags: true))
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def self.find_index_items options = {}
.eager(:latest_triggered_webhooks)
.eager(:webhooks)

rows = rows.consumer(options[:consumer_name]) if options[:consumer_name]
rows = rows.provider(options[:provider_name]) if options[:provider_name]

if !options[:tags]
rows = rows.where(consumer_version_tag_name: nil)
else
Expand Down
8 changes: 8 additions & 0 deletions lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class Row < Sequel::Model(:matrix)
include PactBroker::Repositories::Helpers
include PactBroker::Logging

def consumer consumer_name
where(name_like(:consumer_name, consumer_name))
end

def provider provider_name
where(name_like(:provider_name, provider_name))
end

def matching_selectors selectors
if selectors.size == 1
where_consumer_or_provider_is(selectors.first)
Expand Down
23 changes: 21 additions & 2 deletions spec/lib/pact_broker/api/decorators/integration_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module PactBroker
module Api
module Decorators
describe IntegrationDecorator do
before do
allow(integration_decorator).to receive(:dashboard_url_for_integration).and_return("/dashboard")
end

let(:integration) do
instance_double(PactBroker::Integrations::Integration,
consumer: consumer,
Expand All @@ -13,24 +17,39 @@ module Decorators
end
let(:consumer) { double("consumer", name: "the consumer") }
let(:provider) { double("provider", name: "the provider") }

let(:options) { { user_options: { base_url: 'http://example.org' } } }
let(:expected_hash) do
{
"consumer" => {
"name" => "the consumer"
},
"provider" => {
"name" => "the provider"
},
"_links" => {
"pb:dashboard" => {
"href" => "/dashboard"
}
}
}
end

let(:json) { IntegrationDecorator.new(integration).to_json }
let(:integration_decorator) { IntegrationDecorator.new(integration) }
let(:json) { integration_decorator.to_json(options) }
subject { JSON.parse(json) }

it "generates a hash" do
expect(subject).to match_pact expected_hash
end

it "generates the correct link for the dashboard" do
expect(integration_decorator).to receive(:dashboard_url_for_integration).with(
"the consumer",
"the provider",
"http://example.org"
)
subject
end
end
end
end
Expand Down

0 comments on commit a240852

Please sign in to comment.