From 463e9cfd69a2320eb6be11783184bb4654a0cc95 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 25 Sep 2020 12:40:23 +1000 Subject: [PATCH] feat: render matrix UI page for can-i-deploy endpoint --- lib/pact_broker/ui/app.rb | 6 +++ .../ui/controllers/can_i_deploy.rb | 42 +++++++++++++++++++ lib/pact_broker/ui/controllers/matrix.rb | 37 ++-------------- lib/pact_broker/ui/helpers/matrix_helper.rb | 40 ++++++++++++++++++ lib/pact_broker/ui/views/matrix/show.haml | 2 +- .../ui/controllers/can_i_deploy_spec.rb | 26 ++++++++++++ 6 files changed, 118 insertions(+), 35 deletions(-) create mode 100644 lib/pact_broker/ui/controllers/can_i_deploy.rb create mode 100644 lib/pact_broker/ui/helpers/matrix_helper.rb create mode 100644 spec/lib/pact_broker/ui/controllers/can_i_deploy_spec.rb diff --git a/lib/pact_broker/ui/app.rb b/lib/pact_broker/ui/app.rb index cbaf86cf7..a5ee2b62a 100644 --- a/lib/pact_broker/ui/app.rb +++ b/lib/pact_broker/ui/app.rb @@ -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' @@ -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 diff --git a/lib/pact_broker/ui/controllers/can_i_deploy.rb b/lib/pact_broker/ui/controllers/can_i_deploy.rb new file mode 100644 index 000000000..54140feba --- /dev/null +++ b/lib/pact_broker/ui/controllers/can_i_deploy.rb @@ -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 diff --git a/lib/pact_broker/ui/controllers/matrix.rb b/lib/pact_broker/ui/controllers/matrix.rb index 23d280d90..cd68e01e2 100644 --- a/lib/pact_broker/ui/controllers/matrix.rb +++ b/lib/pact_broker/ui/controllers/matrix.rb @@ -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 @@ -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 @@ -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 @@ -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), @@ -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 diff --git a/lib/pact_broker/ui/helpers/matrix_helper.rb b/lib/pact_broker/ui/helpers/matrix_helper.rb new file mode 100644 index 000000000..8390bb015 --- /dev/null +++ b/lib/pact_broker/ui/helpers/matrix_helper.rb @@ -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 diff --git a/lib/pact_broker/ui/views/matrix/show.haml b/lib/pact_broker/ui/views/matrix/show.haml index 7475c3de3..9becfe846 100644 --- a/lib/pact_broker/ui/views/matrix/show.haml +++ b/lib/pact_broker/ui/views/matrix/show.haml @@ -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' } diff --git a/spec/lib/pact_broker/ui/controllers/can_i_deploy_spec.rb b/spec/lib/pact_broker/ui/controllers/can_i_deploy_spec.rb new file mode 100644 index 000000000..753502037 --- /dev/null +++ b/spec/lib/pact_broker/ui/controllers/can_i_deploy_spec.rb @@ -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