From 1ab8a5d966ebc5a09ff811d1c1e0cfac78e368b1 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 9 Sep 2019 06:53:19 +0800 Subject: [PATCH] feat(dashboard): use 'refreshable' link for latest verification result so pact can be refreshed and display the latest result --- .../api/decorators/dashboard_decorator.rb | 4 +- lib/pact_broker/api/pact_broker_urls.rb | 44 ++++++++++++++++--- spec/fixtures/dashboard.json | 2 +- .../decorators/dashboard_decorator_spec.rb | 2 +- .../pact_broker/api/pact_broker_urls_spec.rb | 14 ++++++ 5 files changed, 57 insertions(+), 9 deletions(-) diff --git a/lib/pact_broker/api/decorators/dashboard_decorator.rb b/lib/pact_broker/api/decorators/dashboard_decorator.rb index 8dc5e8c93..ee9d3fa13 100644 --- a/lib/pact_broker/api/decorators/dashboard_decorator.rb +++ b/lib/pact_broker/api/decorators/dashboard_decorator.rb @@ -102,13 +102,15 @@ def pact_hash(index_item, base_url) end def verification_hash(index_item, base_url) + # Use the 'latest pact' URL instead of the permalink URL so that the page can be + # refreshed, and the latest verification result will be updated to the most recent if index_item.latest_verification { success: index_item.latest_verification.success, verifiedAt: format_date_time(index_item.latest_verification.created_at), _links: { self: { - href: verification_url(index_item.latest_verification, base_url) + href: latest_verification_for_pact_url(index_item.latest_pact, base_url, false) } } } diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index 143cfaed6..1824ac941 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -157,12 +157,20 @@ def latest_verifications_for_consumer_version_url version, base_url "#{base_url}/verification-results/consumer/#{url_encode(version.pacticipant.name)}/version/#{version.number}/latest" end - def latest_verification_for_pact_url pact, base_url - verification_url_from_params( - provider_name: pact.provider_name, - consumer_name: pact.consumer_name, - pact_version_sha: pact.pact_version_sha, - verification_number: 'latest') + def latest_verification_for_pact_url pact, base_url, permalink = true + if permalink + verification_url_from_params( + { + provider_name: provider_name(pact), + consumer_name: consumer_name(pact), + pact_version_sha: pact.pact_version_sha, + verification_number: 'latest' + }, + base_url + ) + else + pact_url(base_url, pact) + "/verification-results/latest" + end end def verification_triggered_webhooks_url verification, base_url = '' @@ -276,6 +284,30 @@ def pactigration_base_url_from_params base_url, params 'consumer', url_encode(params[:consumer_name]) ].join('/') end + + def consumer_name(thing) + if thing.respond_to?(:consumer_name) + thing.consumer_name + elsif thing.respond_to?(:consumer) + thing.consumer.name + elsif thing.respond_to?(:[]) + thing[:consumer_name] + else + nil + end + end + + def provider_name(thing) + if thing.respond_to?(:provider_name) + thing.provider_name + elsif thing.respond_to?(:provider) + thing.provider.name + elsif thing.respond_to?(:[]) + thing[:provider_name] + else + nil + end + end end end end diff --git a/spec/fixtures/dashboard.json b/spec/fixtures/dashboard.json index 0ce6f1d11..2e8a58278 100644 --- a/spec/fixtures/dashboard.json +++ b/spec/fixtures/dashboard.json @@ -26,7 +26,7 @@ "latestVerificationResult": { "_links": { "self": { - "href": "verification_url" + "href": "latest_verification_url" } }, "success": true, diff --git a/spec/lib/pact_broker/api/decorators/dashboard_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/dashboard_decorator_spec.rb index 80d74d61b..9631a89bb 100644 --- a/spec/lib/pact_broker/api/decorators/dashboard_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/dashboard_decorator_spec.rb @@ -49,7 +49,7 @@ def in_utc before do allow_any_instance_of(DashboardDecorator).to receive(:pact_url).with(base_url, pact).and_return('pact_url') - allow_any_instance_of(DashboardDecorator).to receive(:verification_url).with(verification, base_url).and_return('verification_url') + allow_any_instance_of(DashboardDecorator).to receive(:latest_verification_for_pact_url).with(pact, base_url, false).and_return('latest_verification_url') allow_any_instance_of(DashboardDecorator).to receive(:pacticipant_url).with(base_url, consumer).and_return('consumer_url') allow_any_instance_of(DashboardDecorator).to receive(:pacticipant_url).with(base_url, provider).and_return('provider_url') allow_any_instance_of(DashboardDecorator).to receive(:version_url).with(base_url, consumer_version).and_return('consumer_version_url') diff --git a/spec/lib/pact_broker/api/pact_broker_urls_spec.rb b/spec/lib/pact_broker/api/pact_broker_urls_spec.rb index 836ce4af9..fe9d42d3d 100644 --- a/spec/lib/pact_broker/api/pact_broker_urls_spec.rb +++ b/spec/lib/pact_broker/api/pact_broker_urls_spec.rb @@ -113,6 +113,20 @@ module Api end end end + + describe "latest_verification_for_pact_url" do + context "when permalink = true" do + subject { PactBrokerUrls.latest_verification_for_pact_url(pact, base_url, true) } + + it { is_expected.to eq "http://example.org/pacts/provider/Bar%2FBar/consumer/Foo%2FFoo/pact-version/5hbfu/verification-results/latest" } + end + + context "when permalink = false" do + subject { PactBrokerUrls.latest_verification_for_pact_url(pact, base_url, false) } + + it { is_expected.to eq "http://example.org/pacts/provider/Bar%2FBar/consumer/Foo%2FFoo/version/123%2F456/verification-results/latest" } + end + end end end end