Skip to content

Commit

Permalink
feat: render matrix UI page for can-i-deploy endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 25, 2020
1 parent f9e183e commit 463e9cf
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 35 deletions.
6 changes: 6 additions & 0 deletions lib/pact_broker/ui/app.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact_broker/ui/controllers/index'
require 'pact_broker/ui/controllers/groups'
require 'pact_broker/ui/controllers/matrix'
require 'pact_broker/ui/controllers/can_i_deploy'
require 'pact_broker/ui/controllers/error_test'
require 'pact_broker/doc/controllers/app'

Expand Down Expand Up @@ -41,6 +42,11 @@ def initialize
run PactBroker::UI::Controllers::Matrix
end

map "/pacticipants/" do
use PathInfoFixer
run PactBroker::UI::Controllers::CanIDeploy
end

map "/test/error" do
use PathInfoFixer
run PactBroker::UI::Controllers::ErrorTest
Expand Down
42 changes: 42 additions & 0 deletions lib/pact_broker/ui/controllers/can_i_deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'pact_broker/ui/controllers/base_controller'
require 'pact_broker/ui/view_models/matrix_lines'
require 'pact_broker/matrix/unresolved_selector'
require 'pact_broker/matrix/parse_query'
require 'pact_broker/logging'
require 'pact_broker/api/pact_broker_urls'
require 'pact_broker/ui/helpers/matrix_helper'
require 'haml'

module PactBroker
module UI
module Controllers
class CanIDeploy < Base

include PactBroker::Services
include PactBroker::UI::Helpers::MatrixHelper

get "/:pacticipant_name/latest-version/:tag/can-i-deploy/to/:environment_tag" do
# selector and options must be in sync with lib/pact_broker/api/resources/can_i_deploy_badge.rb
selectors = [ PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: params[:pacticipant_name], latest: true, tag: params[:tag]) ]
options = { latestby: 'cvp', limit: 100, tag: params[:to] }
result = matrix_service.find(selectors, options)
lines = PactBroker::UI::ViewDomain::MatrixLines.new(result, base_url: base_url)
locals = {
lines: lines,
selectors: create_selector_objects(selectors),
options: create_options_model(options),
badge_url: badge_url,
base_url: base_url
}
haml :'matrix/show', { locals: locals, layout: :'layouts/main', escape_html: true }
end

def badge_url
u = URI(request.url)
u.path = u.path + "/badge"
u.to_s
end
end
end
end
end
37 changes: 3 additions & 34 deletions lib/pact_broker/ui/controllers/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'pact_broker/matrix/parse_query'
require 'pact_broker/logging'
require 'pact_broker/api/pact_broker_urls'

require 'pact_broker/ui/helpers/matrix_helper'
require 'haml'

module PactBroker
Expand All @@ -13,13 +13,13 @@ module Controllers
class Matrix < Base

include PactBroker::Services
include PactBroker::UI::Helpers::MatrixHelper

get "/" do
selectors = [OpenStruct.new, OpenStruct.new]
options = { limit: 100, latestby: nil }
locals = {
lines: [],
title: "The Matrix",
selectors: create_selector_objects(selectors),
options: create_options_model(options),
base_url: base_url
Expand All @@ -33,7 +33,7 @@ class Matrix < Base
if errors.empty?
lines = matrix_service.find(selectors, options)
locals[:lines] = PactBroker::UI::ViewDomain::MatrixLines.new(lines)
locals[:badge_url] = matrix_badge_url(selectors, lines)
locals[:badge_url] = matrix_badge_url(selectors, lines, base_url)
else
locals[:errors] = errors
end
Expand All @@ -52,7 +52,6 @@ class Matrix < Base
lines = PactBroker::UI::ViewDomain::MatrixLines.new(lines, base_url: base_url)
locals = {
lines: lines,
title: "The Matrix",
consumer_name: params[:consumer_name],
provider_name: params[:provider_name],
selectors: create_selector_objects(selectors),
Expand All @@ -62,36 +61,6 @@ class Matrix < Base
}
haml :'matrix/show', { locals: locals, layout: :'layouts/main', escape_html: true }
end

def create_selector_objects(selector_hashes)
selector_hashes.collect do | selector_hash |
o = OpenStruct.new(selector_hash)
o.specify_latest_tag = o.tag && o.latest ? 'checked' : nil
o.specify_all_tagged = o.tag && !o.latest ? 'checked' : nil
o.specify_latest = o.latest ? 'checked' : nil
o.specify_version = o.pacticipant_version_number ? 'checked' : nil
o.specify_all_versions = !(o.tag || o.pacticipant_version_number) ? 'checked' : nil
o
end
end

def create_options_model(options)
o = OpenStruct.new(options)
o.cvpv_checked = o.latestby == 'cvpv' ? 'checked' : nil
o.cvp_checked = o.latestby == 'cvp' ? 'checked' : nil
o.all_rows_checked = o.latestby.nil? ? 'checked' : nil
o
end

def matrix_badge_url(selectors, lines)
if lines.any? && selectors.size == 2 && selectors.all?{ | selector| selector.latest_for_pacticipant_and_tag? }
consumer_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.consumer_name }
provider_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.provider_name }
if consumer_selector && provider_selector
PactBroker::Api::PactBrokerUrls.matrix_badge_url_for_selectors(consumer_selector, provider_selector, base_url)
end
end
end
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions lib/pact_broker/ui/helpers/matrix_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module PactBroker
module UI
module Helpers
module MatrixHelper

extend self

def create_selector_objects(selector_hashes)
selector_hashes.collect do | selector_hash |
o = OpenStruct.new(selector_hash)
o.specify_latest_tag = o.tag && o.latest ? 'checked' : nil
o.specify_all_tagged = o.tag && !o.latest ? 'checked' : nil
o.specify_latest = o.latest ? 'checked' : nil
o.specify_version = o.pacticipant_version_number ? 'checked' : nil
o.specify_all_versions = !(o.tag || o.pacticipant_version_number) ? 'checked' : nil
o
end
end

def create_options_model(options)
o = OpenStruct.new(options)
o.cvpv_checked = o.latestby == 'cvpv' ? 'checked' : nil
o.cvp_checked = o.latestby == 'cvp' ? 'checked' : nil
o.all_rows_checked = o.latestby.nil? ? 'checked' : nil
o
end

def matrix_badge_url(selectors, lines, base_url)
if lines.any? && selectors.size == 2 && selectors.all?{ | selector| selector.latest_for_pacticipant_and_tag? }
consumer_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.consumer_name }
provider_selector = selectors.find{ | selector| selector.pacticipant_name == lines.first.provider_name }
if consumer_selector && provider_selector
PactBroker::Api::PactBrokerUrls.matrix_badge_url_for_selectors(consumer_selector, provider_selector, base_url)
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/ui/views/matrix/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%a{href: "#{base_url}/"}
Home
%h1.page-header
= title
The Matrix
- if defined?(badge_url) && badge_url
%img{src: badge_url, class: 'pact_badge' }

Expand Down
26 changes: 26 additions & 0 deletions spec/lib/pact_broker/ui/controllers/can_i_deploy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'pact_broker/ui/controllers/can_i_deploy'

module PactBroker
module UI
module Controllers
describe CanIDeploy do

let(:app) { CanIDeploy }

describe "GET" do
before do
TestDataBuilder.new
.create_pact_with_hierarchy("Foo", "1", "Bar")
.create_consumer_version_tag("main")
end

subject { get("/Foo/latest-version/main/can-i-deploy/to/prod") }

it "renders the matrix page" do
expect(subject.body).to include "The Matrix"
end
end
end
end
end
end

0 comments on commit 463e9cf

Please sign in to comment.