Skip to content

Commit

Permalink
feat: show in the UI whether or not a pact was pending when verificat…
Browse files Browse the repository at this point in the history
…ion failed
  • Loading branch information
bethesque committed Sep 13, 2021
1 parent 2d5141d commit 326c068
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 19 deletions.
7 changes: 7 additions & 0 deletions db/migrations/20210913_add_pending_to_verifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Sequel.migration do
change do
alter_table(:verifications) do
add_column(:pending, TrueClass)
end
end
end
1 change: 1 addition & 0 deletions lib/pact_broker/api/decorators/verification_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TagDecorator < BaseDecorator
property :provider_name, as: :providerName, writeable: false
property :provider_version_number, as: :providerApplicationVersion, writeable: false
property :success
property :pending, writeable: false
property :execution_date, as: :verificationDate
property :build_url, as: :buildUrl
property :test_results, as: :testResults
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
module PactBroker
module Api
module Decorators

class VerificationSummaryDecorator < BaseDecorator

# TODO count pending failures?
property :success
property :provider_summary, as: :providerSummary do
property :successful
Expand All @@ -30,7 +30,6 @@ def provider_summary
successful: represented.select(&:success).collect(&:provider_name),
failed: represented.select{|verification| !verification.success }.collect(&:provider_name))
end

end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/api/resources/verifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ def wip?
metadata[:wip] == "true"
end

def pending?
metadata[:pending]
end

def event_context
metadata
end

def verification_params
params(symbolize_names: false).merge("wip" => wip?)
params(symbolize_names: false).merge("wip" => wip?, "pending" => pending?)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/badges/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def badge_status pseudo_branch_verification_status
case pseudo_branch_verification_status
when :success then "verified"
when :failed then "failed"
when :failed_pending then "failed (pending)"
when :stale then "changed"
else "unknown"
end
Expand All @@ -96,6 +97,7 @@ def badge_color pseudo_branch_verification_status
case pseudo_branch_verification_status
when :success then "brightgreen"
when :failed then "red"
when :failed_pending then "red"
when :stale then "orange"
else "lightgrey"
end
Expand Down Expand Up @@ -151,7 +153,7 @@ def with_cache uri
def static_svg pact, pseudo_branch_verification_status
file_name = case pseudo_branch_verification_status
when :success then "pact-verified-brightgreen.svg"
when :failed then "pact-failed-red.svg"
when :failed, :failed_pending then "pact-failed-red.svg"
when :stale then "pact-changed-orange.svg"
else "pact-unknown-lightgrey.svg"
end
Expand Down
6 changes: 4 additions & 2 deletions lib/pact_broker/pacts/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Metadata
[:consumer_version_number, "cvn"], # for old urls
[:consumer_version_id, "cv"],
[:wip, "w"],
[:pending, "p"],
[:consumer_version_selectors, "s"],
[:tag, "t"],
[:branch, "b"],
Expand Down Expand Up @@ -62,8 +63,9 @@ def build_metadata_for_pact_for_verification(verifiable_pact)
"l" => selector.latest,
"cv" => selector.consumer_version.id
}.compact
end
}
end,
"p" => verifiable_pact.pending?
}.compact
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/test/http_test_data_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def publish_pact(consumer: last_consumer_name, consumer_version:, provider: last
self
end

def get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: false, include_wip_pacts_since: nil)
def get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: nil, include_wip_pacts_since: nil)
@last_provider_name = provider
@last_provider_version_tag = provider_version_tag
@last_provder_version_branch = provider_version_branch
Expand Down
15 changes: 12 additions & 3 deletions lib/pact_broker/ui/view_models/index_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class IndexItem
:provider_version_branches,
:latest_for_branch?,
:consumer_version_environment_names,
:provider_version_environment_names
:provider_version_environment_names,
:latest_verification
] => :relationship


Expand Down Expand Up @@ -153,8 +154,13 @@ def webhook_url

def last_verified_date
if @relationship.ever_verified?
date = @relationship.latest_verification.execution_date
PactBroker::DateHelper.distance_of_time_in_words(date, DateTime.now) + " ago"
date = latest_verification.execution_date
ago = PactBroker::DateHelper.distance_of_time_in_words(date, DateTime.now) + " ago"
if latest_verification.pending
"#{ago} (pending)"
else
ago
end
else
""
end
Expand All @@ -174,6 +180,7 @@ def pseudo_branch_verification_status
when :success then "success"
when :stale then "warning"
when :failed then "danger"
when :failed_pending then "danger"
else ""
end
end
Expand All @@ -197,6 +204,8 @@ def verification_tooltip
""
end
"Pact #{desc}has changed since last successful verification by #{provider_name} (#{short_version_number(@relationship.latest_verification_provider_version_number)})"
when :failed_pending
"Verification by #{provider_name} (#{short_version_number(@relationship.latest_verification_provider_version_number)}) failed, but did not fail the build as the pact content was in pending state for that provider branch"
when :failed
"Verification by #{provider_name} (#{short_version_number(@relationship.latest_verification_provider_version_number)}) failed"
else
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/verifications/pseudo_branch_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def to_sym
:success
end
else
:failed
if latest_verification.pending
:failed_pending
else
:failed
end
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/verifications/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def create next_verification_number, params, verified_pacts, event_context
provider_version_number = params.fetch("providerApplicationVersion")
PactBroker::Api::Decorators::VerificationDecorator.new(verification).from_hash(params)
verification.wip = params.fetch("wip")
verification.pending = params.fetch("pending")
verification.number = next_verification_number
verification.consumer_version_selector_hashes = event_context[:consumer_version_selectors]
pact_version = pact_repository.find_pact_version(first_verified_pact.consumer, first_verified_pact.provider, first_verified_pact.pact_version_sha)
Expand Down
26 changes: 26 additions & 0 deletions script/data/pending.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
begin

$LOAD_PATH << "#{Dir.pwd}/lib"
require "pact_broker/test/http_test_data_builder"
base_url = ENV["PACT_BROKER_BASE_URL"] || "http://localhost:9292"

td = PactBroker::Test::HttpTestDataBuilder.new(base_url)
td.delete_pacticipant("some-demo-provider")
.delete_pacticipant("some-demo-consumer")
.publish_contract(consumer: "some-demo-consumer", provider: "some-demo-provider", consumer_version: "1", content_id: "1111", branch: "main")
.publish_contract(consumer: "some-demo-consumer", provider: "some-demo-provider", consumer_version: "1", content_id: "1111", branch: "feat/x")
.publish_contract(consumer: "some-demo-consumer", provider: "some-demo-provider", consumer_version: "2", content_id: "1111", branch: "feat/x")
.get_pacts_for_verification(provider: "some-demo-provider")
.verify_pact(
provider_version_branch: "main",
provider_version: "1",
success: false
)


rescue StandardError => e
puts "#{e.class} #{e.message}"
puts e.backtrace
exit 1
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Decorators
test_results: { "arbitrary" => "json" },
build_url: "http://build-url",
pact_version_sha: "1234",
pending: is_pending,
latest_pact_publication: pact_publication,
execution_date: DateTime.now,
provider_version_tags: provider_version_tags)
Expand All @@ -32,6 +33,8 @@ module Decorators
)
end

let(:is_pending) { true }

let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: "prod", latest?: true)] }

let(:options) { { user_options: { base_url: "http://example.org" } } }
Expand All @@ -44,6 +47,10 @@ module Decorators
expect(subject[:success]).to eq true
end

it "includes the pending status" do
expect(subject[:pending]).to eq true
end

it "includes the provider version" do
expect(subject[:providerApplicationVersion]).to eq "4.5.6"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ module Decorators
pact_version_sha: "1234",
latest_pact_publication: pact,
test_results: nil,
pending: is_pending,
execution_date: DateTime.now,
provider_version_tags: provider_version_tags)
end

let(:pact_version) do
instance_double("PactBroker::Pacts::PactVersion", name: "Name")
end

let(:provider_version_tags) { [instance_double(PactBroker::Tags::TagWithLatestFlag, name: "prod", latest?: true)] }
let(:pact) { instance_double("PactBroker::Domain::Pact", name: "Some pact", consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1.2.3") }
let(:is_pending) { true }
let(:options) { {base_url: "http://example.org", consumer_name: "Foo", consumer_version_number: "1.2.3", resource_url: "http://self"} }

subject { JSON.parse VerificationSummaryDecorator.new(summary).to_json(user_options: options), symbolize_names: true }
Expand Down
5 changes: 2 additions & 3 deletions spec/lib/pact_broker/api/resources/verifications_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "spec_helper"
require "pact_broker/api/resources/verifications"
require "pact_broker/pacts/service"
require "pact_broker/verifications/service"
Expand All @@ -17,7 +16,7 @@ module Resources
let(:database_connector) { double("database_connector" )}
let(:verification) { double(PactBroker::Domain::Verification) }
let(:errors_empty) { true }
let(:parsed_metadata) { { the: "metadata", consumer_version_number: "2"} }
let(:parsed_metadata) { { the: "metadata", consumer_version_number: "2", pending: true } }
let(:base_url) { "http://example.org" }
let(:webhook_execution_configuration) { instance_double(PactBroker::Webhooks::ExecutionConfiguration) }

Expand Down Expand Up @@ -86,7 +85,7 @@ module Resources
it "stores the verification in the database" do
expect(PactBroker::Verifications::Service).to receive(:create).with(
next_verification_number,
hash_including("some" => "params", "wip" => false),
hash_including("some" => "params", "wip" => false, "pending" => true),
verified_pacts,
parsed_metadata
)
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/pact_broker/badges/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ module Badges
end
end

context "when the pseudo_branch_verification_status is :failed_pending" do
let(:pseudo_branch_verification_status) { :failed_pending }
let(:expected_color) { "red" }
let(:expected_right_text) { "failed%20%28pending%29" }

it "create a red badge with left text 'failed'" do
subject
expect(http_request).to have_been_made
expect(pact_verification_badge_url).to eq URI(expected_url)
end
end

context "when the pseudo_branch_verification_status is :stale" do
let(:pseudo_branch_verification_status) { :stale }
let(:expected_color) { "orange" }
Expand Down Expand Up @@ -248,6 +260,16 @@ module Badges
end
end

# TODO save a static image for this
context "when the pseudo_branch_verification_status is failed_pending" do
let(:pseudo_branch_verification_status) { :failed_pending }

it "returns a static failed image" do
expect(subject).to include ">pact</"
expect(subject).to include ">failed</"
end
end

context "when the pseudo_branch_verification_status is stale" do
let(:pseudo_branch_verification_status) { :stale }

Expand Down
13 changes: 11 additions & 2 deletions spec/lib/pact_broker/pacts/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module Metadata
Selectors.new([ResolvedSelector.new({ latest: true, consumer: "consumer", tag: "tag" }, consumer_version)])
end
let(:consumer_version) { double("version", number: "2", id: 1) }
let(:verifiable_pact) { double("PactBroker::Pacts::VerifiablePact", wip: wip, selectors: selectors) }
let(:verifiable_pact) { double("PactBroker::Pacts::VerifiablePact", wip: wip, selectors: selectors, pending?: is_pending) }
let(:is_pending) { nil }
let(:wip) { false }

subject { Metadata.build_metadata_for_pact_for_verification(verifiable_pact) }
Expand All @@ -29,7 +30,12 @@ module Metadata
let(:wip) { true }

it { is_expected.to eq "w" => true }
end

context "when the pact is pending" do
let(:is_pending) { true }

it { is_expected.to include "p" => true }
end
end

Expand All @@ -52,7 +58,8 @@ module Metadata
"t" => "tag",
"cv" => 1
}
]
],
"p" => true
}
end

Expand All @@ -61,6 +68,7 @@ module Metadata
:consumer_version_number => "2",
:consumer_version_tags => ["tag"],
:wip => true,
:pending => true,
:consumer_version_selectors => [
{
:latest => true,
Expand All @@ -83,6 +91,7 @@ module Metadata
:consumer_version_number => nil,
:consumer_version_tags => ["tag"],
:wip => true,
:pending => true,
:consumer_version_selectors => [
{
:latest => true,
Expand Down
10 changes: 9 additions & 1 deletion spec/lib/pact_broker/verifications/pseudo_branch_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ module Verifications
describe PseudoBranchStatus do
describe "pseudo_branch_verification_status" do

let(:latest_verification) { instance_double("PactBroker::Domain::Verification", pact_version_sha: latest_verification_pact_version_sha, success: success) }
let(:latest_verification) { instance_double("PactBroker::Domain::Verification", pending: is_pending, pact_version_sha: latest_verification_pact_version_sha, success: success) }
let(:latest_pact) { instance_double("PactBroker::Domain::Pact", pact_version_sha: pact_pact_version_sha) }
let(:pact_pact_version_sha) { "1234" }
let(:latest_verification_pact_version_sha) { "1234" }
let(:success) { true }
let(:is_pending) { nil }

subject { PseudoBranchStatus.new(latest_pact, latest_verification) }

Expand All @@ -32,6 +33,13 @@ module Verifications
its(:to_sym) { is_expected.to eq :failed }
end

context "when the pact has not changed since the last failed verification and the pact was pending" do
let(:is_pending) { true }
let(:success) { false }

its(:to_sym) { is_expected.to eq :failed_pending }
end

context "when the pact has changed since the last successful verification" do
let(:pact_pact_version_sha) { "4566" }
its(:to_sym) { is_expected.to eq :stale }
Expand Down
Loading

0 comments on commit 326c068

Please sign in to comment.