Skip to content

Commit

Permalink
feat(matrix): support querying by branch
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 23, 2021
1 parent 5e34a7e commit 110578d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def where_tag(tag)
end
end

def where_branch(branch)
where(branch: branch)
end

def where_number(number)
where(name_like(:number, number))
end
Expand Down Expand Up @@ -130,6 +134,7 @@ def for_selector(selector)
query = query.where_pacticipant_name(selector.pacticipant_name) if selector.pacticipant_name
query = query.currently_deployed_to_environment(selector.environment_name, selector.pacticipant_name) if selector.environment_name
query = query.where_tag(selector.tag) if selector.tag
query = query.where_branch(selector.branch) if selector.branch
query = query.where_number(selector.pacticipant_version_number) if selector.pacticipant_version_number
query = query.where_age_less_than(selector.max_age) if selector.max_age

Expand Down
10 changes: 5 additions & 5 deletions lib/pact_broker/matrix/parse_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ def self.call query
params = Rack::Utils.parse_nested_query(query)
selectors = (params['q'] || []).collect do |i|
p = PactBroker::Matrix::UnresolvedSelector.new
p[:pacticipant_name] = i['pacticipant'] if i['pacticipant'] && i['pacticipant'] != ''
p[:pacticipant_version_number] = i['version'] if i['version'] && i['version'] != ''
p[:latest] = true if i['latest'] == 'true'
p[:branch] = i['branch'] if i['branch'] && i['branch'] != ''
p[:tag] = i['tag'] if i['tag'] && i['tag'] != ''
p.pacticipant_name = i['pacticipant'] if i['pacticipant'] && i['pacticipant'] != ''
p.pacticipant_version_number = i['version'] if i['version'] && i['version'] != ''
p.latest = true if i['latest'] == 'true'
p.branch = i['branch'] if i['branch'] && i['branch'] != ''
p.tag = i['tag'] if i['tag'] && i['tag'] != ''
p
end
options = {}
Expand Down
14 changes: 14 additions & 0 deletions lib/pact_broker/matrix/resolved_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def self.for_pacticipant_and_version(pacticipant, version, original_selector, ty
pacticipant_version_number: version.number,
latest: original_selector[:latest],
tag: original_selector[:tag],
branch: original_selector[:branch],
environment_name: original_selector[:environment_name],
type: type,
one_of_many: one_of_many
Expand All @@ -45,6 +46,7 @@ def self.for_pacticipant_and_non_existing_version(pacticipant, original_selector
pacticipant_version_number: original_selector[:pacticipant_version_number],
latest: original_selector[:latest],
tag: original_selector[:tag],
branch: original_selector[:branch],
environment_name: original_selector[:environment_name],
type: type
)
Expand Down Expand Up @@ -74,6 +76,10 @@ def tag
self[:tag]
end

def branch
self[:branch]
end

def environment_name
self[:environment_name]
end
Expand All @@ -94,6 +100,10 @@ def latest_tagged?
latest? && tag
end

def latest_from_branch?
latest? && branch
end

def version_does_not_exist?
!version_exists?
end
Expand Down Expand Up @@ -130,6 +140,10 @@ def description
"the latest version of #{pacticipant_name} with tag #{tag} (#{pacticipant_version_number})"
elsif latest_tagged?
"the latest version of #{pacticipant_name} with tag #{tag} (no such version exists)"
elsif latest_from_branch? && pacticipant_version_number
"the latest version of #{pacticipant_name} from branch #{branch} (#{pacticipant_version_number})"
elsif latest_from_branch?
"the latest version of #{pacticipant_name} from branch #{branch} (no such version exists)"
elsif latest? && pacticipant_version_number
"the latest version of #{pacticipant_name} (#{pacticipant_version_number})"
elsif latest?
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/matrix/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def validate_selectors selectors, options = {}
if s[:pacticipant_name].nil?
error_messages << "Please specify the pacticipant name"
else
# TODO a bunch more validation
if s.key?(:pacticipant_version_number) && s.key?(:latest)
error_messages << "A version number and latest flag cannot both be specified for #{s[:pacticipant_name]}"
end
Expand Down
10 changes: 9 additions & 1 deletion lib/pact_broker/matrix/unresolved_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(params = {})
end

def self.from_hash(hash)
new(hash.symbolize_keys.snakecase_keys.slice(:pacticipant_name, :pacticipant_version_number, :latest, :tag, :environment_name, :max_age))
new(hash.symbolize_keys.snakecase_keys.slice(:pacticipant_name, :pacticipant_version_number, :latest, :tag, :branch, :environment_name, :max_age))
end

def pacticipant_name
Expand All @@ -37,6 +37,10 @@ def tag
self[:tag]
end

def branch
self[:branch]
end

def environment_name
self[:environment_name]
end
Expand All @@ -49,6 +53,10 @@ def tag= tag
self[:tag] = tag
end

def branch= branch
self[:branch] = branch
end

def environment_name= environment_name
self[:environment_name] = environment_name
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/test/test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def create_consumer_version version_number = "1.0.#{model_counter}", params = {}
def create_provider_version version_number = "1.0.#{model_counter}", params = {}
params.delete(:comment)
tag_names = [params.delete(:tag_names), params.delete(:tag_name)].flatten.compact
@version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @provider)
@version = PactBroker::Domain::Version.create(:number => version_number, :pacticipant => @provider, branch: params[:branch])
@provider_version = @version
tag_names.each do | tag_name |
tag = PactBroker::Domain::Tag.create(name: tag_name, version: provider_version)
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/pact_broker/domain/version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,40 @@ def version_numbers
expect(version_numbers).to eq %w{2 10}
end
end

context "selecting versions for a branch" do
before do
td.create_consumer("Foo")
.create_consumer_version("1", branch: "main")
.create_consumer_version("2", branch: "feat/foo")
.create_consumer_version("3", branch: "main")
.create_provider("Bar")
.create_provider_version("10", branch: "main")
end

let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(branch: "main") }

it "returns the versions with the matching branch" do
expect(version_numbers).to eq %w{1 3 10}
end
end

context "selecting latest version for a branch" do
before do
td.create_consumer("Foo")
.create_consumer_version("1", branch: "main")
.create_consumer_version("2", branch: "feat/foo")
.create_consumer_version("3", branch: "main")
.create_provider("Bar")
.create_provider_version("10", branch: "main")
end

let(:selector) { PactBroker::Matrix::UnresolvedSelector.new(branch: "main", latest: true) }

it "returns the latest versions for each matching branch" do
expect(version_numbers).to eq %w{3 10}
end
end
end

describe "latest_for_pacticipant?" do
Expand Down

0 comments on commit 110578d

Please sign in to comment.