Skip to content

Commit

Permalink
feat: add environments to matrix response
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Nov 15, 2021
1 parent fac3fc8 commit fd50f22
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 6 deletions.
25 changes: 25 additions & 0 deletions lib/pact_broker/api/decorators/embedded_environment_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 17 additions & 2 deletions lib/pact_broker/api/decorators/matrix_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 |
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/api/decorators/version_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/pact_broker/matrix/quick_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
60 changes: 60 additions & 0 deletions spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 )
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/pact_broker/api/decorators/version_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fd50f22

Please sign in to comment.