Skip to content

Commit

Permalink
Merge branch 'master' into feat/dashboard-api
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 8, 2018
2 parents 7277ecc + 3f74e7e commit ca1c6bb
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 46 deletions.
8 changes: 7 additions & 1 deletion lib/pact_broker/api/resources/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def forbidden?

def to_svg
response.headers['Cache-Control'] = 'no-cache'
badge_service.pact_verification_badge pact, label, initials, verification_status
comment + badge_service.pact_verification_badge(pact, label, initials, verification_status)
end

def pact
Expand All @@ -55,6 +55,12 @@ def label
def initials
request.query['initials'] == 'true'
end

def comment
consumer_version_number = pact ? pact.consumer_version_number : "?"
provider_version_number = latest_verification ? latest_verification.provider_version_number : "?"
"<!-- #{identifier_from_path[:consumer_name]} version #{consumer_version_number} #{identifier_from_path[:provider_name]} version #{provider_version_number} -->\n"
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/pact_broker/api/resources/matrix_badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class MatrixBadge < Badge
private

def latest_verification
@latest_verification ||= begin
matrix_row = matrix_service.find_for_consumer_and_provider_with_tags(identifier_from_path)
if matrix_row && matrix_row[:verification_id]
verification_service.find_by_id(matrix_row[:verification_id])
end
end
@latest_verification ||= verification_service.find_latest_verification_for_tags(
identifier_from_path[:consumer_name],
identifier_from_path[:provider_name],
identifier_from_path[:tag],
identifier_from_path[:provider_tag]
)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/domain/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ def untagged
.left_outer_join(:tags, {version_id: :consumer_version_id})
.where(Sequel.qualify(:tags, :name) => nil)
end

def provider_version_tag tag_name
filter = name_like(Sequel.qualify(:ptags, :name), tag_name)
join_params = { Sequel[:ptags][:version_id] => Sequel[model.table_name][:provider_version_id] }
join(:tags, join_params, {table_alias: :ptags}).where(filter)
end
end

def pact_version_sha
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/ui/views/matrix/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
-# This code is an embarassment. Sorry. Just trying to get as much stuff done in the little time I have.
.container
.navbar-right
%a{href: '/'}
Home
%h1.page-header
= title

Expand Down
12 changes: 12 additions & 0 deletions lib/pact_broker/verifications/all_verifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'pact_broker/domain/verification'

module PactBroker
module Verifications
include PactBroker::Repositories::Helpers

class AllVerifications < PactBroker::Domain::Verification
set_dataset(:all_verifications)
end

end
end
20 changes: 20 additions & 0 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'sequel'
require 'pact_broker/domain/verification'
require 'pact_broker/verifications/latest_verifications_by_consumer_version'
require 'pact_broker/verifications/all_verifications'

module PactBroker
module Verifications
Expand Down Expand Up @@ -64,6 +65,25 @@ def find_latest_verification_for consumer_name, provider_name, consumer_version_
).limit(1).single_record
end

def find_latest_verification_for_tags consumer_name, provider_name, consumer_version_tag, provider_version_tag
view_name = PactBroker::Verifications::AllVerifications.table_name
query = PactBroker::Verifications::AllVerifications
.select_all_qualified
.join(:versions, {Sequel[:provider_versions][:id] => Sequel[view_name][:provider_version_id]}, {table_alias: :provider_versions})
.join(:all_pact_publications, { Sequel[view_name][:pact_version_id] => Sequel[:all_pact_publications][:pact_version_id] })
.consumer(consumer_name)
.provider(provider_name)
.tag(consumer_version_tag)
.provider_version_tag(provider_version_tag)

query.reverse_order(
Sequel[:all_pact_publications][:consumer_version_order],
Sequel[:all_pact_publications][:revision_number],
Sequel[:provider_versions][:order],
Sequel[view_name][:execution_date]
).limit(1).single_record
end

def pact_version_id_for pact
PactBroker::Pacts::PactPublication.select(:pact_version_id).where(id: pact.id)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def find_latest_verification_for consumer, provider, tag = nil
verification_repository.find_latest_verification_for consumer.name, provider.name, tag
end

def find_latest_verification_for_tags consumer_name, provider_name, consumer_version_tag_name, provider_version_tag_name
verification_repository.find_latest_verification_for_tags(consumer_name, provider_name, consumer_version_tag_name, provider_version_tag_name)
end

def verification_summary_for_consumer_version params
verifications = find_latest_verifications_for_consumer_version(params)
pacts = pact_service.find_by_consumer_version(params)
Expand Down
62 changes: 23 additions & 39 deletions spec/lib/pact_broker/api/resources/badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ module Resources
allow(PactBroker::Verifications::Status).to receive(:new).and_return(verification_status)
end

let(:pact) { instance_double("PactBroker::Domain::Pact", consumer: consumer, provider: provider) }
let(:pact) { instance_double("PactBroker::Domain::Pact", consumer: consumer, provider: provider, consumer_version_number: "2") }
let(:consumer) { double('consumer') }
let(:provider) { double('provider') }
let(:verification) { double("verification") }
let(:verification) { double("verification", provider_version_number: "3") }
let(:verification_status) { instance_double("PactBroker::Verifications::Status", to_sym: :verified) }


Expand Down Expand Up @@ -82,7 +82,11 @@ module Resources
end

it "returns the badge" do
expect(subject.body).to eq "badge"
expect(subject.body).to end_with "badge"
end

it "returns a comment with the consumer and provider numbers" do
expect(subject.body).to include "<!-- consumer version 2 provider version 3 -->"
end

context "when the label param is specified" do
Expand Down Expand Up @@ -127,55 +131,35 @@ module Resources
end

let(:path) { "/matrix/provider/provider/latest/master/consumer/consumer/latest/prod/badge" }
let(:row) { { verification_id: 1 } }
let(:row) { { consumer_name: 'consumer', provider_name: 'provider' } }


it "looks up the matrix row" do
expect(PactBroker::Matrix::Service).to receive(:find_for_consumer_and_provider_with_tags).with(
hash_including(consumer_name: 'consumer',
provider_name: 'provider',
tag: 'prod',
provider_tag: 'master'
))
it "looks up the verification" do
expect(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags) do | consumer, provider, tag|
expect(consumer.name).to eq 'consumer'
expect(provider.name).to eq 'provider'
expect(tag).to eq 'prod'
end
subject
end

context "when a matrix row is found" do
context "when there is a verification_id" do
it "looks up the verification" do
expect(PactBroker::Verifications::Service).to receive(:find_by_id).with(1)
subject
end

it "returns the badge" do
expect(subject.body).to eq "badge"
end
context "when a verification is found" do
before do
allow(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags).and_return(verification)
end

context "when there is not a verification_id" do
let(:row) { {} }

it "does not look up the verification" do
expect(PactBroker::Verifications::Service).to_not receive(:find_by_id)
subject
end

it "returns the badge" do
expect(subject.body).to eq "badge"
end
it "returns the badge" do
expect(subject.body).to end_with "badge"
end
end

context "when a matrix row is not found" do
let(:row) { nil }

it "does not look up the verification" do
expect(PactBroker::Verifications::Service).to_not receive(:find_by_id)
subject
context "when a verification is not found" do
before do
allow(PactBroker::Verifications::Service).to receive(:find_latest_verification_for_tags).and_return(nil)
end

it "returns the badge" do
expect(subject.body).to eq "badge"
expect(subject.body).to end_with "badge"
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/pact_broker/verifications/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,30 @@ module Verifications
end
end
end

describe "find_latest_verification_for_tags" do
before do
TestDataBuilder.new
.create_pact_with_hierarchy("Foo", "1", "Bar")
.create_consumer_version_tag("feat-x")
.create_verification(provider_version: "5")
.use_provider_version("5")
.create_provider_version_tag("feat-y")
.create_verification(provider_version: "6", number: 2)
.use_provider_version("6")
.create_provider_version_tag("feat-y")
.create_verification(provider_version: "7", number: 3)
.create_consumer_version("2")
.create_pact
.create_verification(provider_version: "8")
end

subject { Repository.new.find_latest_verification_for_tags("Foo", "Bar", "feat-x", "feat-y") }

it "returns the latest verification for a pact with the given consumer tag, by a provider version with the given provider tag" do
expect(subject.provider_version_number).to eq "6"
end
end
end
end
end

0 comments on commit ca1c6bb

Please sign in to comment.