From fd50f22c7db4c157aa432aff483c9b90f8865d51 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 15 Nov 2021 12:04:53 +1100 Subject: [PATCH] feat: add environments to matrix response --- .../embedded_environment_decorator.rb | 25 ++++++++ .../api/decorators/matrix_decorator.rb | 19 +++++- .../api/decorators/version_decorator.rb | 2 + lib/pact_broker/matrix/quick_row.rb | 16 +++++ .../api/decorators/matrix_decorator_spec.rb | 60 +++++++++++++++++++ .../api/decorators/version_decorator_spec.rb | 8 +-- 6 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 lib/pact_broker/api/decorators/embedded_environment_decorator.rb diff --git a/lib/pact_broker/api/decorators/embedded_environment_decorator.rb b/lib/pact_broker/api/decorators/embedded_environment_decorator.rb new file mode 100644 index 000000000..f78d5a9c7 --- /dev/null +++ b/lib/pact_broker/api/decorators/embedded_environment_decorator.rb @@ -0,0 +1,25 @@ +require_relative "base_decorator" +require_relative "timestamps" + +module PactBroker + module Api + module Decorators + class EmbeddedEnvironmentDecorator < BaseDecorator + property :uuid, writeable: false + property :name + property :display_name, camelize: true + property :production + + include Timestamps + + link :self do | user_options | + { + title: "Environment", + name: represented.name, + href: environment_url(represented, user_options.fetch(:base_url)) + } + end + end + end + end +end diff --git a/lib/pact_broker/api/decorators/matrix_decorator.rb b/lib/pact_broker/api/decorators/matrix_decorator.rb index c2b68b141..4d1c3f1f0 100644 --- a/lib/pact_broker/api/decorators/matrix_decorator.rb +++ b/lib/pact_broker/api/decorators/matrix_decorator.rb @@ -3,6 +3,7 @@ require "pact_broker/api/decorators/reason_decorator" require "pact_broker/api/decorators/format_date_time" require "pact_broker/api/decorators/embedded_branch_version_decorator" +require "pact_broker/api/decorators/embedded_environment_decorator" module PactBroker module Api @@ -85,7 +86,9 @@ def consumer_hash(line, consumer, consumer_version, base_url) version: { number: line.consumer_version_number, branch: line.consumer_version_branch_versions.last&.branch_name, - branches: branches(line.consumer_version_branch_versions, base_url), + branches: branches(line.consumer_version_branch_versions, base_url), # TODO delete this + branchVersions: branches(line.consumer_version_branch_versions, base_url), + environments: environments(line.consumer_version_deployed_versions, line.consumer_version_released_versions, base_url), _links: { self: { href: version_url(base_url, consumer_version) @@ -107,6 +110,16 @@ def branches(branch_versions, base_url) end end + def environments(deployed_versions, released_versions, base_url) + (deployed_versions + released_versions).sort_by(&:created_at).collect(&:environment).uniq.collect do | environment | + environment_decorator_class.new(environment).to_hash(user_options: { base_url: base_url }) + end + end + + def environment_decorator_class + PactBroker::Api::Decorators::EmbeddedEnvironmentDecorator + end + def tags(tags, base_url) tags.sort_by(&:created_at).collect do | tag | { @@ -136,7 +149,9 @@ def provider_hash(line, provider, provider_version, base_url) hash[:version] = { number: line.provider_version_number, branch: line.provider_version_branch_versions.last&.branch_name, - branches: branches(line.provider_version_branch_versions, base_url), + branches: branches(line.provider_version_branch_versions, base_url), # TODO delete this + branchVersions: branches(line.provider_version_branch_versions, base_url), + environments: environments(line.provider_version_deployed_versions, line.provider_version_released_versions, base_url), _links: { self: { href: version_url(base_url, provider_version) diff --git a/lib/pact_broker/api/decorators/version_decorator.rb b/lib/pact_broker/api/decorators/version_decorator.rb index b2400a87d..b033a606b 100644 --- a/lib/pact_broker/api/decorators/version_decorator.rb +++ b/lib/pact_broker/api/decorators/version_decorator.rb @@ -7,7 +7,9 @@ module Decorators class VersionDecorator < BaseDecorator property :number, writeable: false + # TODO delete branches in preference for branchVersions collection :branch_versions, as: :branches, embedded: true, writeable: false, extend: PactBroker::Api::Decorators::EmbeddedBranchVersionDecorator + collection :branch_versions, as: :branchVersions, embedded: true, writeable: false, extend: PactBroker::Api::Decorators::EmbeddedBranchVersionDecorator property :build_url, as: :buildUrl collection :tags, embedded: true, :extend => PactBroker::Api::Decorators::EmbeddedTagDecorator, class: OpenStruct diff --git a/lib/pact_broker/matrix/quick_row.rb b/lib/pact_broker/matrix/quick_row.rb index d0c9cfe52..027808bb6 100644 --- a/lib/pact_broker/matrix/quick_row.rb +++ b/lib/pact_broker/matrix/quick_row.rb @@ -361,6 +361,14 @@ def consumer_version_branch_versions consumer_version.branch_versions end + def consumer_version_deployed_versions + consumer_version.current_deployed_versions + end + + def consumer_version_released_versions + consumer_version.current_supported_released_versions + end + def consumer_version_order consumer_version.order end @@ -377,6 +385,14 @@ def provider_version_branch_versions provider_version&.branch_versions || [] end + def provider_version_deployed_versions + provider_version&.current_deployed_versions || [] + end + + def provider_version_released_versions + provider_version&.current_supported_released_versions || [] + end + def provider_version_order provider_version&.order end diff --git a/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb index b306d158a..084db882e 100644 --- a/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb @@ -22,12 +22,16 @@ module Decorators consumer_name: "Consumer", consumer_version_number: "1.0.0", consumer_version_branch_versions: consumer_version_branch_versions, + consumer_version_deployed_versions: deployed_versions, + consumer_version_released_versions: released_versions, consumer_version_tags: consumer_version_tags, provider_version_tags: provider_version_tags, pact_version_sha: "1234", pact_created_at: pact_created_at, provider_version_number: "4.5.6", provider_version_branch_versions: provider_version_branch_versions, + provider_version_deployed_versions: deployed_versions, + provider_version_released_versions: released_versions, provider_name: "Provider", success: row_1_success, verification_number: 1, @@ -42,11 +46,15 @@ module Decorators consumer_name: "Consumer", consumer_version_number: "1.0.0", consumer_version_branch_versions: [], + consumer_version_deployed_versions: [], + consumer_version_released_versions: [], consumer_version_tags: [], pact_version_sha: "1234", pact_created_at: pact_created_at, provider_version_number: nil, provider_version_branch_versions: [], + provider_version_deployed_versions: [], + provider_version_released_versions: [], provider_name: "Provider", success: row_2_success, verification_number: nil, @@ -72,6 +80,22 @@ module Decorators } ], + branchVersions: [ + name: "main", + _links: { + + } + ], + environments: [ + { + name: "test", + displayName: "Test" + }, + { + name: "production", + displayName: "Production" + } + ], _links: { self: { href: "http://example.org/pacticipants/Consumer/versions/1.0.0" @@ -103,6 +127,22 @@ module Decorators version: { number: "4.5.6", branch: "feat/x", + branchVersions: [ + { + name: "feat/x", + latest: true + } + ], + environments: [ + { + name: "test", + displayName: "Test" + }, + { + name: "production", + displayName: "Production" + } + ], _links: { self: { href: "http://example.org/pacticipants/Provider/versions/4.5.6" @@ -152,6 +192,26 @@ module Decorators [ instance_double("PactBroker::Versions::BranchVersion", branch_name: "main", latest?: true) ] end + let(:deployed_versions) do + [ + instance_double("PactBroker::Deployments::DeployedVersion", environment: test_environment, created_at: DateTime.new(2021, 1, 1)) + ] + end + + let(:released_versions) do + [ + instance_double("PactBroker::Deployments::ReleasedVersion", environment: prod_environment, created_at: DateTime.new(2021, 1, 2)) + ] + end + + let(:test_environment) do + instance_double("PactBroker::Deployments::Environment", uuid: "uuid", production: false, name: "test", display_name: "Test", created_at: DateTime.now, updated_at: DateTime.now ).as_null_object + end + + let(:prod_environment) do + instance_double("PactBroker::Deployments::Environment", uuid: "uuid", production: true, name: "production", display_name: "Production", created_at: DateTime.now, updated_at: DateTime.now ).as_null_object + end + let(:consumer_version_tags) do [ double("tag", name: "prod", latest?: true, version: consumer_version, created_at: DateTime.now ) diff --git a/spec/lib/pact_broker/api/decorators/version_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/version_decorator_spec.rb index 5e586f1ed..32cc52fcb 100644 --- a/spec/lib/pact_broker/api/decorators/version_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/version_decorator_spec.rb @@ -83,10 +83,10 @@ module Decorators expect(subject[:_embedded][:tags].first[:name]).to eq "prod" end - it "includes the branches" do - expect(subject[:_embedded][:branches]).to be_instance_of(Array) - expect(subject[:_embedded][:branches].first[:name]).to eq "main" - expect(subject[:_embedded][:branches].first[:latest]).to eq true + it "includes the branch versions" do + expect(subject[:_embedded][:branchVersions]).to be_instance_of(Array) + expect(subject[:_embedded][:branchVersions].first[:name]).to eq "main" + expect(subject[:_embedded][:branchVersions].first[:latest]).to eq true end it "includes the timestamps" do