Skip to content

Commit

Permalink
feat: allow integrations to be exported in dot format (text/vnd.graph…
Browse files Browse the repository at this point in the history
…viz)
  • Loading branch information
bethesque committed Aug 27, 2018
1 parent 9ac2ba9 commit ac60908
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ module PactBroker

add ['dashboard'], Api::Resources::Dashboard, {resource_name: "dashboard"}
add ['test','error'], Api::Resources::ErrorTest, {resource_name: "error_test"}

add ['integrations'], Api::Resources::Integrations, {resource_name: "integrations"}
add [], Api::Resources::Index, {resource_name: "index"}
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/pact_broker/api/renderers/integrations_dot_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module PactBroker
module Api
module Renderers
class IntegrationsDotRenderer
def initialize(integrations)
@integrations = integrations
end

def self.call(integrations)
new(integrations).call
end

def call
"digraph { ranksep=3; ratio=auto; overlap=false; node [ shape = plaintext, fontname = Helvetica ];
#{integrations_graph}
}
"
end

private

attr_reader :integrations

def integrations_graph
integrations
.collect{ | integration| " #{escape_name(integration.consumer_name)} -> #{escape_name(integration.provider_name)}" }
.join("\n")
end

def escape_name(name)
name.gsub(" ", "_")
end
end
end
end
end
5 changes: 5 additions & 0 deletions lib/pact_broker/api/resources/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def links
title: 'Webhooks',
templated: false
},
'pb:integrations' => {
href: base_url + '/integrations',
title: 'Integrations',
templated: false
},
'beta:pending-provider-pacts' =>
{
href: base_url + '/pacts/provider/{provider}/pending',
Expand Down
26 changes: 26 additions & 0 deletions lib/pact_broker/api/resources/integrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'pact_broker/api/resources/base_resource'
require 'pact_broker/api/renderers/integrations_dot_renderer'

module PactBroker
module Api
module Resources
class Integrations < BaseResource
def content_types_provided
[["text/vnd.graphviz", :to_dot]]
end

def allowed_methods
["GET", "OPTIONS"]
end

def to_dot
PactBroker::Api::Renderers::IntegrationsDotRenderer.call(integrations)
end

def integrations
pact_service.find_latest_pacts
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/pact_broker/doc/views/integrations.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Integrations

Allowed methods: `GET`

Content types: `text/vnd.graphviz`

A list of all the integrations (consumer/provider pairs) stored in the Pact Broker.
23 changes: 23 additions & 0 deletions spec/features/get_integrations_dot_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
describe "Get integrations dot file" do
before do
TestDataBuilder.new
.create_pact_with_hierarchy("Foo", "1", "Bar")
end

let(:path) { "/integrations" }
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) }

subject { get path, nil, {'HTTP_ACCEPT' => 'text/vnd.graphviz' }; last_response }

it "returns a 200 OK" do
expect(subject.status).to eq 200
end

it "returns a dot file content type" do
expect(subject.headers['Content-Type']).to eq 'text/vnd.graphviz;charset=utf-8'
end

it "returns dot file content" do
expect(subject.body).to include "Foo -> Bar"
end
end
4 changes: 4 additions & 0 deletions spec/fixtures/expected.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
digraph { ranksep=3; ratio=auto; overlap=false; node [ shape = plaintext, fontname = Helvetica ];
Foo -> Bar
Wiffle -> Foo_Thing
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'pact_broker/api/renderers/integrations_dot_renderer'

module PactBroker
module Api
module Renderers
describe IntegrationsDotRenderer do

# TODO work out how to handle apostrophes etc

let(:integrations) do
[
double('integration', consumer_name: "Foo", provider_name: "Bar"),
double('integration', consumer_name: "Wiffle", provider_name: "Foo Thing")
]
end

let(:expected_content) { load_fixture('expected.gv') }

describe "#call" do
subject { IntegrationsDotRenderer.call(integrations) }

it "renders a dot file" do
expect(subject).to eq expected_content
end
end
end
end
end
end

0 comments on commit ac60908

Please sign in to comment.