Skip to content

Commit

Permalink
feat(matrix): allow the 'groupby' to determine the 'latest' row to be…
Browse files Browse the repository at this point in the history
… specified via the api
  • Loading branch information
bethesque committed Nov 6, 2017
1 parent 314e837 commit 45c9a4c
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 42 deletions.
8 changes: 4 additions & 4 deletions lib/pact_broker/api/decorators/matrix_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ def to_hash(options)
}
end

private

attr_reader :lines

def deployable
return nil if lines.any?{ |line| line[:success].nil? }
lines.any? && lines.all?{ |line| line[:success] }
Expand All @@ -43,6 +39,10 @@ def reason
end
end

private

attr_reader :lines

def matrix(lines, base_url)
provider = nil
consumer = nil
Expand Down
5 changes: 4 additions & 1 deletion lib/pact_broker/api/decorators/matrix_text_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'ostruct'
require 'pact_broker/api/pact_broker_urls'
require 'pact_broker/api/decorators/matrix_decorator'

require 'table_print'

module PactBroker
Expand All @@ -13,11 +15,12 @@ def initialize(lines)
end

def to_text(options)
json_decorator = PactBroker::Api::Decorators::MatrixDecorator.new(lines)
data = lines.collect do | line |
Line.new(line[:consumer_name], line[:consumer_version_number], line[:provider_name], line[:provider_version_number], line[:success])
end
printer = TablePrint::Printer.new(data)
printer.table_print + "\n"
printer.table_print + "\n\nDeployable: #{json_decorator.deployable.inspect}\nReason: #{json_decorator.reason}\n"
end

private
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/matrix/parse_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def self.call query
if params.key?('success') && params['success'].is_a?(String)
options[:success] = [params['success'] == '' ? nil : params['success'] == 'true']
end
if params.key?('scope')
options[:scope] = params['scope']
end
if params.key?('groupby')
options[:groupby] = params['groupby']
end
return selectors, options
end
end
Expand Down
40 changes: 34 additions & 6 deletions lib/pact_broker/matrix/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ class Repository
include PactBroker::Repositories::Helpers
include PactBroker::Repositories

# TODO SORT BY MOST RECENT FIRST
# TODO move latest verification logic in to database

GROUP_BY_PROVIDER_VERSION_NUMBER = [:consumer_name, :consumer_version_number, :provider_name, :provider_version_number]
GROUP_BY_PROVIDER = [:consumer_name, :consumer_version_number, :provider_name]
GROUP_BY_PACT = [:consumer_name, :provider_name]

# Return the latest matrix row (pact/verification) for each consumer_version_number/provider_version_number
def find selectors, options = {}
# The group with the nil provider_version_numbers will be the results of the left outer join
# that don't have verifications, so we need to include them all.
lines = find_all(selectors)
.group_by{|line| [line[:consumer_name], line[:consumer_version_number], line[:provider_name], line[:provider_version_number]]}
.values
.collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last }
.flatten
lines = apply_scope(options, selectors, lines)

if options.key?(:success)
lines = lines.select{ |l| options[:success].include?(l[:success]) }
Expand All @@ -23,14 +27,39 @@ def find selectors, options = {}
lines
end

def all_versions_specified? selectors
selectors.all?{ |s| s[:pacticipant_version_number] }
end

def apply_scope options, selectors, lines
return lines unless options[:latestby]

group_by_columns = case options[:latestby]
when 'cvp' then GROUP_BY_PROVIDER
when 'cp' then GROUP_BY_PACT
else
GROUP_BY_PROVIDER_VERSION_NUMBER
end

lines.group_by{|line| group_by_columns.collect{|key| line[key] }}
.values
.collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last }
.flatten
end

def find_for_consumer_and_provider pacticipant_1_name, pacticipant_2_name
selectors = [{ pacticipant_name: pacticipant_1_name }, { pacticipant_name: pacticipant_2_name }]
find_all(selectors)
.sort{|l1, l2| l2[:consumer_version_order] <=> l1[:consumer_version_order]}
end

def find_compatible_pacticipant_versions selectors
find(selectors).select{ |line | line[:success] }
find(selectors)
.group_by{|line| GROUP_BY_PROVIDER_VERSION_NUMBER.collect{|key| line[key] }}
.values
.collect{ | lines | lines.first[:provider_version_number].nil? ? lines : lines.last }
.flatten
.select{|line| line[:success] }
end

##
Expand Down Expand Up @@ -60,7 +89,6 @@ def find_all selectors
def look_up_versions_for_tags(selectors)
selectors.collect do | selector |
# resource validation currently stops tag being specified without latest=true

if selector[:tag] && selector[:latest]
version = version_repository.find_by_pacticpant_name_and_latest_tag(selector[:pacticipant_name], selector[:tag])
# validation in resource should ensure we always have a version
Expand Down
Loading

0 comments on commit 45c9a4c

Please sign in to comment.