pact_broker 0.0.10 → 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +0 -3
  3. data/README.md +28 -1
  4. data/Rakefile +4 -2
  5. data/assets/d3.v3.js +9263 -0
  6. data/assets/force.csv +29 -0
  7. data/assets/index.html +186 -0
  8. data/assets/index2.html +224 -0
  9. data/assets/relationships +4 -0
  10. data/assets/stylesheets/github.css +387 -0
  11. data/bethtest.rb +67 -0
  12. data/config.ru +10 -4
  13. data/config/boot.rb +4 -0
  14. data/example/Gemfile +5 -0
  15. data/example/config.ru +18 -0
  16. data/lib/db.rb +52 -0
  17. data/lib/pact_broker.rb +2 -0
  18. data/lib/pact_broker/api.rb +2 -0
  19. data/lib/pact_broker/api/decorators/pact_collection_decorator.rb +1 -1
  20. data/lib/pact_broker/api/decorators/pacticipant_collection_decorator.rb +1 -1
  21. data/lib/pact_broker/api/decorators/relationships_csv_decorator.rb +32 -0
  22. data/lib/pact_broker/api/renderers/html_pact_renderer.rb +43 -0
  23. data/lib/pact_broker/api/resources/latest_pact.rb +7 -1
  24. data/lib/pact_broker/api/resources/relationships.rb +29 -0
  25. data/lib/pact_broker/app.rb +82 -0
  26. data/lib/pact_broker/configuration.rb +47 -0
  27. data/lib/pact_broker/db.rb +7 -39
  28. data/lib/pact_broker/models/relationship.rb +27 -0
  29. data/lib/pact_broker/project_root.rb +7 -0
  30. data/lib/pact_broker/services/pacticipant_service.rb +6 -0
  31. data/lib/pact_broker/ui/controllers/base_controller.rb +15 -0
  32. data/lib/pact_broker/ui/controllers/clusters.rb +28 -0
  33. data/lib/pact_broker/ui/controllers/relationships.rb +21 -0
  34. data/lib/pact_broker/ui/view_models/clusters.rb +0 -0
  35. data/lib/pact_broker/ui/view_models/relationship.rb +35 -0
  36. data/lib/pact_broker/ui/view_models/relationships.rb +21 -0
  37. data/lib/pact_broker/ui/views/clusters/show.haml +2 -0
  38. data/lib/pact_broker/ui/views/layouts/main.haml +2 -0
  39. data/lib/pact_broker/ui/views/relationships/show.haml +44 -0
  40. data/lib/pact_broker/version.rb +1 -1
  41. data/lib/rack/hal_browser.rb +1 -0
  42. data/lib/rack/hal_browser/redirect.rb +45 -0
  43. data/pact_broker.gemspec +10 -8
  44. data/public/bootstrap.zip +0 -0
  45. data/public/config.json +412 -0
  46. data/public/css/bootstrap-theme.css +346 -0
  47. data/public/css/bootstrap-theme.min.css +7 -0
  48. data/public/css/bootstrap.css +5780 -0
  49. data/public/css/bootstrap.min.css +7 -0
  50. data/public/fonts/glyphicons-halflings-regular.eot +0 -0
  51. data/public/fonts/glyphicons-halflings-regular.svg +229 -0
  52. data/public/fonts/glyphicons-halflings-regular.ttf +0 -0
  53. data/public/fonts/glyphicons-halflings-regular.woff +0 -0
  54. data/public/javascripts/jquery-2.1.1.min.js +4 -0
  55. data/public/javascripts/jquery.tablesorter.js +1765 -0
  56. data/public/javascripts/jquery.tablesorter.min.js +5 -0
  57. data/public/js/bootstrap.js +1943 -0
  58. data/public/js/bootstrap.min.js +7 -0
  59. data/public/stylesheets/github.css +387 -0
  60. data/public/stylesheets/relationships.css +18 -0
  61. data/script/update-hal-browser +6 -0
  62. data/spec/fixtures/renderer_pact.json +34 -0
  63. data/spec/lib/pact_broker/api/decorators/relationships_csv_decorator_spec.rb +30 -0
  64. data/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb +29 -0
  65. data/spec/lib/pact_broker/api/resources/latest_pact_spec.rb +57 -0
  66. data/spec/lib/pact_broker/configuration_spec.rb +21 -0
  67. data/spec/lib/pact_broker/services/pacticipant_service_spec.rb +30 -0
  68. data/spec/lib/pact_broker/ui/controllers/clusters_spec.rb +27 -0
  69. data/spec/lib/pact_broker/ui/controllers/relationships_spec.rb +37 -0
  70. data/spec/lib/pact_broker/ui/view_models/relationship_spec.rb +29 -0
  71. data/spec/lib/rack/hal_browser/redirect_spec.rb +63 -0
  72. data/spec/service_consumers/pact_helper.rb +1 -1
  73. data/spec/spec_helper.rb +10 -3
  74. data/tasks/database.rb +57 -0
  75. data/tasks/db.rake +57 -12
  76. data/tasks/pact.rake +1 -1
  77. data/tasks/rspec.rake +1 -9
  78. data/vendor/hal-browser/.gitignore +1 -0
  79. data/vendor/hal-browser/MIT-LICENSE.txt +20 -0
  80. data/vendor/hal-browser/README.md +35 -0
  81. data/vendor/hal-browser/browser.html +246 -0
  82. data/vendor/hal-browser/js/hal.js +57 -0
  83. data/vendor/hal-browser/js/hal/browser.js +60 -0
  84. data/vendor/hal-browser/js/hal/http/client.js +38 -0
  85. data/vendor/hal-browser/js/hal/resource.js +34 -0
  86. data/vendor/hal-browser/js/hal/views/browser.js +29 -0
  87. data/vendor/hal-browser/js/hal/views/documentation.js +7 -0
  88. data/vendor/hal-browser/js/hal/views/embedded_resource.js +56 -0
  89. data/vendor/hal-browser/js/hal/views/embedded_resources.js +41 -0
  90. data/vendor/hal-browser/js/hal/views/explorer.js +23 -0
  91. data/vendor/hal-browser/js/hal/views/inspector.js +39 -0
  92. data/vendor/hal-browser/js/hal/views/links.js +54 -0
  93. data/vendor/hal-browser/js/hal/views/location_bar.js +40 -0
  94. data/vendor/hal-browser/js/hal/views/navigation.js +19 -0
  95. data/vendor/hal-browser/js/hal/views/non_safe_request_dialog.js +53 -0
  96. data/vendor/hal-browser/js/hal/views/properties.js +14 -0
  97. data/vendor/hal-browser/js/hal/views/query_uri_dialog.js +69 -0
  98. data/vendor/hal-browser/js/hal/views/request_headers.js +30 -0
  99. data/vendor/hal-browser/js/hal/views/resource.js +38 -0
  100. data/vendor/hal-browser/js/hal/views/response.js +24 -0
  101. data/vendor/hal-browser/js/hal/views/response_body.js +40 -0
  102. data/vendor/hal-browser/js/hal/views/response_headers.js +19 -0
  103. data/vendor/hal-browser/styles.css +67 -0
  104. data/vendor/hal-browser/vendor/css/bootstrap-responsive.css +1109 -0
  105. data/vendor/hal-browser/vendor/css/bootstrap.css +6158 -0
  106. data/vendor/hal-browser/vendor/img/ajax-loader.gif +0 -0
  107. data/vendor/hal-browser/vendor/img/glyphicons-halflings-white.png +0 -0
  108. data/vendor/hal-browser/vendor/img/glyphicons-halflings.png +0 -0
  109. data/vendor/hal-browser/vendor/js/backbone.js +1487 -0
  110. data/vendor/hal-browser/vendor/js/bootstrap.js +2276 -0
  111. data/vendor/hal-browser/vendor/js/jquery-1.9.1.js +9597 -0
  112. data/vendor/hal-browser/vendor/js/underscore.js +1227 -0
  113. data/vendor/hal-browser/vendor/js/uritemplates.js +438 -0
  114. metadata +162 -47
  115. data/Gemfile.lock +0 -110
data/bethtest.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'set'
2
+
3
+ class Link
4
+ def initialize from, to
5
+ @from = from
6
+ @to = to
7
+ end
8
+
9
+ def include? endpoint
10
+ @from == endpoint || @to == endpoint
11
+ end
12
+
13
+ def to_s
14
+ "#{@from} - #{@to}"
15
+ end
16
+
17
+ def to_a
18
+ [@from, @to]
19
+ end
20
+
21
+ end
22
+
23
+ def unique_nodes links
24
+ links.collect(&:to_a).flatten.uniq.sort
25
+ end
26
+
27
+ def nodes_connected_to_node node, links
28
+ unique_nodes links.select{|l|l.include?(node)}
29
+ end
30
+
31
+ def nodes_connected_to_nodes_within_pool nodes, links, node_pool
32
+ nodes.collect{ | node | nodes_connected_to_node(node, links) }.flatten & node_pool
33
+ end
34
+
35
+ def split_into_clusters links
36
+ node_pool = unique_nodes links
37
+ groups = []
38
+
39
+ while node_pool.any?
40
+ group = []
41
+ groups << group
42
+ connected_nodes = [node_pool.first]
43
+
44
+ while connected_nodes.any?
45
+ group.concat(connected_nodes)
46
+ node_pool = node_pool - connected_nodes
47
+ connected_nodes = nodes_connected_to_nodes_within_pool connected_nodes, links, node_pool
48
+ end
49
+ end
50
+
51
+ groups
52
+ end
53
+
54
+ links = [Link.new('A', 'B'), Link.new('A', 'C'), Link.new('C', 'D'), Link.new('D', 'E'), Link.new('E','A'),
55
+ Link.new('Y', 'Z'), Link.new('X', 'Y'),
56
+ Link.new('M', 'N'), Link.new('N', 'O'), Link.new('O', 'P'), Link.new('P','Q')]
57
+
58
+
59
+ groups = split_into_clusters links
60
+
61
+ puts groups.collect{ | group| "group = #{group.join(" ")}"}
62
+
63
+
64
+
65
+
66
+
67
+
data/config.ru CHANGED
@@ -1,11 +1,17 @@
1
1
  require File.dirname(__FILE__) + '/config/boot'
2
- require 'pact_broker/db'
2
+ require 'db'
3
3
  require 'pact_broker/api'
4
- require 'pact_broker/api/resources/pact'
4
+ require 'rack/hal_browser'
5
+ require 'pact_broker/ui/controllers/relationships'
5
6
 
6
7
 
7
- use Rack::Static, root: 'public', urls: ['/favicon.ico']
8
+ use Rack::Static, :urls => ["/stylesheets", "/css", "/fonts", "/js"], :root => "public"
9
+ use Rack::HalBrowser::Redirect, :exclude => ['/diagnostic', '/trace','/index','/force.csv']
8
10
 
9
11
  run Rack::URLMap.new(
10
- '/' => PactBroker::API
12
+ '/ui/relationships' => PactBroker::UI::Controllers::Relationships,
13
+ '/index.html' => Rack::File.new("#{File.dirname(__FILE__)}/assets/index.html"),
14
+ '/index2.html' => Rack::File.new("#{File.dirname(__FILE__)}/assets/index2.html"),
15
+ '/force.csv' => Rack::File.new("#{File.dirname(__FILE__)}/assets/force.csv"),
16
+ '/' => PactBroker::API,
11
17
  )
data/config/boot.rb CHANGED
@@ -14,4 +14,8 @@ if File.exists?(ENV['BUNDLE_GEMFILE'])
14
14
  Bundler.require
15
15
  end
16
16
 
17
+ if defined?(I18n)
18
+ I18n.enforce_available_locales = false
19
+ end
20
+
17
21
  require 'pact_broker'
data/example/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "pact_broker"
4
+ gem "sqlite3" # Replace with your choice of database driver eg. mysql2
5
+ gem "thin" # Keep, or replace with your choice of web server
data/example/config.ru ADDED
@@ -0,0 +1,18 @@
1
+ require 'fileutils'
2
+ require 'logger'
3
+ require 'sequel'
4
+ require 'pact_broker'
5
+ require 'rack/hal_browser'
6
+
7
+ # Create a real database, and set the credentials for it here
8
+ DATABASE_CREDENTIALS = {database: "pact_broker_database.sqlite3", adapter: "sqlite"}
9
+
10
+ app = PactBroker::App.new do | config |
11
+ # change these from their default values if desired
12
+ # config.log_dir = "./log"
13
+ # config.auto_migrate_db = true
14
+ # config.use_hal_browser = true
15
+ config.database_connection = Sequel.connect(DATABASE_CREDENTIALS.merge(:logger => config.logger))
16
+ end
17
+
18
+ run app
data/lib/db.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'sequel'
2
+ require 'sequel/connection_pool/threaded'
3
+ require 'yaml'
4
+ require 'pact_broker/logging'
5
+ require 'erb'
6
+ require 'pact_broker/project_root'
7
+
8
+ module DB
9
+ include PactBroker::Logging
10
+ ##
11
+ # Sequel by default does not test connections in its connection pool before
12
+ # handing them to a client. To enable connection testing you need to load the
13
+ # "connection_validator" extension like below. The connection validator
14
+ # extension is configurable, by default it only checks connections once per
15
+ # hour:
16
+ #
17
+ # http://sequel.rubyforge.org/rdoc-plugins/files/lib/sequel/extensions/connection_validator_rb.html
18
+ #
19
+ # Because most of our applications so far are accessed infrequently, there is
20
+ # very little overhead in checking each connection when it is requested. This
21
+ # takes care of stale connections.
22
+ #
23
+ # A gotcha here is that it is not enough to enable the "connection_validator"
24
+ # extension, we also need to specify that we want to use the threaded connection
25
+ # pool, as noted in the documentation for the extension.
26
+ #
27
+ def self.connect db_credentials
28
+ con = Sequel.connect(db_credentials.merge(:logger => logger, :pool_class => Sequel::ThreadedConnectionPool))
29
+ con.extension(:connection_validator)
30
+ con.pool.connection_validation_timeout = -1 #Check the connection on every request
31
+ con
32
+ end
33
+
34
+ def self.connection_for_env env
35
+ logger.info "Connecting to #{env} database."
36
+ connect configuration_for_env(env)
37
+ end
38
+
39
+ def self.configuration_for_env env
40
+ database_yml = PactBroker.project_root.join('config','database.yml')
41
+ config = YAML.load(ERB.new(File.read(database_yml)).result)
42
+ config.fetch(env)
43
+ end
44
+
45
+ PACT_BROKER_DB ||= connection_for_env ENV.fetch('RACK_ENV')
46
+
47
+ def self.health_check
48
+ PACT_BROKER_DB.synchronize do |c| c
49
+ PACT_BROKER_DB.valid_connection? c
50
+ end
51
+ end
52
+ end
data/lib/pact_broker.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'pact_broker/version'
2
+ require 'pact_broker/logging'
3
+ require 'pact_broker/app'
2
4
 
3
5
  module PactBroker
4
6
 
@@ -5,6 +5,7 @@ require 'pact_broker/api/resources/pacticipant'
5
5
  require 'pact_broker/api/resources/pacticipants'
6
6
  require 'pact_broker/api/resources/tag'
7
7
  require 'pact_broker/api/resources/index'
8
+ require 'pact_broker/api/resources/relationships'
8
9
 
9
10
  require 'webmachine/adapters/rack'
10
11
 
@@ -20,6 +21,7 @@ module PactBroker
20
21
  add ['pacticipants'], Api::Resources::Pacticipants
21
22
  add ['pacticipants', :name], Api::Resources::Pacticipant
22
23
  add ['pacticipants', :pacticipant_name, 'versions', :pacticipant_version_number, 'tags', :tag_name], Api::Resources::Tag
24
+ add ['relationships'], Api::Resources::Relationships
23
25
  add [], Api::Resources::Index
24
26
  end
25
27
  end
@@ -13,7 +13,7 @@ module PactBroker
13
13
  include Roar::Representer::JSON::HAL
14
14
  include PactBroker::Api::PactBrokerUrls
15
15
 
16
- collection :pacts, decorator_scope: true, :class => PactBroker::Models::Pact, :extend => PactBroker::Api::Decorators::LatestPactDecorator
16
+ collection :pacts, exec_context: :decorator, :class => PactBroker::Models::Pact, :extend => PactBroker::Api::Decorators::LatestPactDecorator
17
17
 
18
18
  def pacts
19
19
  represented.collect{ | pact | create_representable_pact(pact) }
@@ -30,7 +30,7 @@ module PactBroker
30
30
 
31
31
  class PacticipantCollectionRepresenter < BaseDecorator
32
32
 
33
- collection :pacticipants, decorator_scope: true, :class => PactBroker::Models::Pacticipant, :extend => PactBroker::Api::Decorators::PacticipantRepresenter
33
+ collection :pacticipants, exec_context: :decorator, :class => PactBroker::Models::Pacticipant, :extend => PactBroker::Api::Decorators::PacticipantRepresenter
34
34
 
35
35
  def pacticipants
36
36
  represented
@@ -0,0 +1,32 @@
1
+ require 'csv'
2
+
3
+ module PactBroker
4
+
5
+ module Api
6
+
7
+ module Decorators
8
+
9
+ class RelationshipsCsvDecorator
10
+
11
+ def initialize pacts
12
+ @pacts = pacts
13
+ end
14
+
15
+ def to_csv
16
+
17
+ CSV.generate do |csv|
18
+ csv << ["source", "target", "weight"]
19
+ pacts.each do | pact |
20
+ csv << [pact.consumer.name, pact.provider.name, 1]
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ attr_accessor :pacts
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ require 'pact/consumer_contract'
2
+ require 'pact/reification'
3
+ require 'redcarpet'
4
+
5
+ module PactBroker
6
+ module Api
7
+ module Renderers
8
+ class HtmlPactRenderer
9
+
10
+ def self.call json_content
11
+ new(json_content).call
12
+ end
13
+
14
+ def initialize json_content
15
+ @json_content = json_content
16
+ end
17
+
18
+ def call
19
+ "<html><head>#{head}</head><body>#{html}</body></html>"
20
+ end
21
+
22
+ private
23
+
24
+ def head
25
+ '<link rel="stylesheet" type="text/css" href="/stylesheets/github.css">'
26
+ end
27
+
28
+ def markdown
29
+ Pact::Doc::Markdown::InteractionsRenderer.call consumer_contract
30
+ end
31
+
32
+ def html
33
+ Redcarpet::Markdown.new(Redcarpet::Render::HTML, :fenced_code_blocks => true, :lax_spacing => true).render(markdown)
34
+ end
35
+
36
+ def consumer_contract
37
+ Pact::ConsumerContract.from_json(@json_content)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,4 +1,5 @@
1
1
  require 'pact_broker/api/resources/base_resource'
2
+ require 'pact_broker/configuration'
2
3
 
3
4
  module PactBroker::Api
4
5
 
@@ -7,7 +8,8 @@ module PactBroker::Api
7
8
  class LatestPact < BaseResource
8
9
 
9
10
  def content_types_provided
10
- [["application/json", :to_json]]
11
+ [["application/json", :to_json],
12
+ ["text/html", :to_html]]
11
13
  end
12
14
 
13
15
  def allowed_methods
@@ -24,6 +26,10 @@ module PactBroker::Api
24
26
  @pact.json_content
25
27
  end
26
28
 
29
+ def to_html
30
+ PactBroker.configuration.html_pact_renderer.call(@pact.json_content)
31
+ end
32
+
27
33
  end
28
34
  end
29
35
 
@@ -0,0 +1,29 @@
1
+ require 'pact_broker/api/resources/base_resource'
2
+ require 'pact_broker/api/decorators/relationships_csv_decorator'
3
+
4
+ module PactBroker::Api
5
+
6
+ module Resources
7
+
8
+ class Relationships < BaseResource
9
+
10
+ def content_types_provided
11
+ [["text/csv", :to_csv]]
12
+ end
13
+
14
+ def allowed_methods
15
+ ["GET"]
16
+ end
17
+
18
+ def to_csv
19
+ generate_csv(pact_service.find_latest_pacts)
20
+ end
21
+
22
+ def generate_csv pacts
23
+ PactBroker::Api::Decorators::RelationshipsCsvDecorator.new(pacts).to_csv
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,82 @@
1
+ require 'pact_broker/configuration'
2
+ require 'pact_broker/db'
3
+ require 'pact_broker/project_root'
4
+ require 'rack/hal_browser'
5
+
6
+ module PactBroker
7
+
8
+ class App
9
+
10
+ attr_accessor :configuration
11
+
12
+ def initialize &block
13
+ @configuration = Configuration.default_configuration
14
+ yield configuration
15
+ post_configure
16
+ build_app
17
+ end
18
+
19
+ def call env
20
+ @app.call env
21
+ end
22
+
23
+ private
24
+
25
+ def logger
26
+ PactBroker.logger
27
+ end
28
+
29
+ def post_configure
30
+ PactBroker.logger = configuration.logger
31
+
32
+ if configuration.auto_migrate_db
33
+ logger.info "Migrating database"
34
+ PactBroker::DB.run_migrations configuration.database_connection
35
+ else
36
+ logger.info "Skipping database migrations"
37
+ end
38
+ end
39
+
40
+ def build_app
41
+ @app = Rack::Builder.new
42
+
43
+ @app.use Rack::Static, :urls => ["/stylesheets", "/images", "/css", "/fonts", "/js", "/javascripts"], :root => PactBroker.project_root.join("public")
44
+
45
+ if configuration.use_hal_browser
46
+ logger.info "Mounting HAL browser"
47
+ @app.use Rack::HalBrowser::Redirect, :exclude => ['/trace']
48
+ else
49
+ logger.info "Not mounting HAL browser"
50
+ end
51
+
52
+ logger.info "Mounting UI"
53
+ require 'pact_broker/ui/controllers/relationships'
54
+
55
+ ui = Rack::Builder.new {
56
+ map "/ui/relationships" do
57
+ run PactBroker::UI::Controllers::Relationships
58
+ end
59
+ map "/" do
60
+ run lambda { |env|
61
+ if (env['PATH_INFO'] == "/" || env['PATH_INFO'] == "") && !env['HTTP_ACCEPT'].include?("json")
62
+ [303, {'Location' => 'ui/relationships'},[]]
63
+ else
64
+ [404, {},[]]
65
+ end
66
+ }
67
+ end
68
+ }
69
+
70
+ logger.info "Mounting PactBroker::API"
71
+ require 'pact_broker/api'
72
+
73
+ apps = [ui, PactBroker::API]
74
+
75
+ @app.map "/" do
76
+ run Rack::Cascade.new(apps)
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ end