Skip to content

Commit

Permalink
Merge branch 'master' into feat/bulk-delete-branches
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Dec 8, 2023
2 parents 8338f4d + 89271f1 commit a2e6e29
Show file tree
Hide file tree
Showing 54 changed files with 431 additions and 265 deletions.
1 change: 1 addition & 0 deletions lib/pact_broker/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def self.build_api(application_context = PactBroker::ApplicationContext.default_
add ["pacticipants", :pacticipant_name, "versions", :pacticipant_version_number, "tags", :tag_name], Api::Resources::Tag, {resource_name: "pacticipant_version_tag"}
add ["pacticipants", :pacticipant_name, "branches"], Api::Resources::PacticipantBranches, {resource_name: "pacticipant_branches"}
add ["pacticipants", :pacticipant_name, "branches", :branch_name], Api::Resources::Branch, { resource_name: "branch" }
add ["pacticipants", :pacticipant_name, "branches", :branch_name, "latest-version"], Api::Resources::LatestVersion, { resource_name: "latest_pacticipant_version_for_branch" }
add ["pacticipants", :pacticipant_name, "branches", :branch_name, "versions", :version_number], Api::Resources::BranchVersion, { resource_name: "branch_version" }
add ["pacticipants", :pacticipant_name, "branches", :branch_name, "latest-version", "can-i-deploy", "to-environment", :environment_name], Api::Resources::CanIDeployPacticipantVersionByBranchToEnvironment, { resource_name: "can_i_deploy_latest_branch_version_to_environment" }
add ["pacticipants", :pacticipant_name, "branches", :branch_name, "latest-version", "can-i-deploy", "to-environment", :environment_name, "badge"], Api::Resources::CanIDeployPacticipantVersionByBranchToEnvironmentBadge, { resource_name: "can_i_deploy_latest_branch_version_to_environment_badge" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ module Api
module Contracts
class PaginationQueryParamsSchema < BaseContract
params do
# legacy format
optional(:pageNumber).maybe(:integer).value(gteq?: 1)
optional(:pageSize).maybe(:integer).value(gteq?: 1)

# desired format
optional(:page).maybe(:integer).value(gteq?: 1)
optional(:size).maybe(:integer).value(gteq?: 1)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/api/decorators/base_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "roar/decorator"
require "roar/json/hal"
require "pact_broker/api/pact_broker_urls"
require "pact_broker/api/decorators/decorator_context"
require "pact_broker/api/decorators/format_date_time"
require "pact_broker/string_refinements"
require "pact_broker/hash_refinements"
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/decorators/branch_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BranchDecorator < BaseDecorator
link "pb:latest-version" do | user_options |
{
title: "Latest version for branch",
href: branch_versions_url(represented, user_options.fetch(:base_url)) + "?pageSize=1"
href: latest_version_for_branch_url(represented, user_options.fetch(:base_url))
}
end

Expand Down
23 changes: 0 additions & 23 deletions lib/pact_broker/api/decorators/decorator_context.rb

This file was deleted.

49 changes: 47 additions & 2 deletions lib/pact_broker/api/decorators/decorator_context_creator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
require "pact_broker/api/decorators/decorator_context"
# Builds the Hash that is passed into the Decorator as the `user_options`. It contains the request details, rack env, the (optional) title
# and anything else that is required by the decorator to render the resource (eg. the pacticipant that the versions belong to)

module PactBroker
module Api
module Decorators
class DecoratorContextCreator

# @param [PactBroker::BaseResource] the Pact Broker webmachine resource
# @param [Hash] options any extra options that need to be passed through to the decorator.
# @return [Hash] decorator_context

# decorator_context [String] :base_url
# The location where the Pact Broker is hosted.
# eg. http://some.host:9292/pact_broker
# Always present

# decorator_context [String] :resource_url
# The resource URL without any query string.
# eg. http://some.host:9292/pact_broker/pacticipants/Foo/versions
# Always present

# decorator_context [String] :query_string
# The query string.
# "page=1&size=50"
# May be empty

# decorator_context [String] :request_url
# The full request URL.
# eg. http://some.host:9292/pact_broker/pacticipants/Foo/versions?page=1&size=50
# Always present

# decorator_context [Hash] :env
# The rack env.
# Always present

# decorator_context [Hash] :resource_title
# eg. "Pacticipant versions for Foo"
# Optional
# Used when a single decorator is being used for multiple resources and the title needs to be
# set from the resource.

def self.call(resource, options)
Decorators::DecoratorContext.new(resource.base_url, resource.resource_url, resource.request.env, options)
env = resource.request.env
decorator_context = {}
decorator_context[:base_url] = resource.base_url
decorator_context[:resource_url] = resource.resource_url
decorator_context[:query_string] = query_string = (env["QUERY_STRING"] && !env["QUERY_STRING"].empty? ? env["QUERY_STRING"] : nil)
decorator_context[:request_url] = query_string ? resource.resource_url + "?" + query_string : resource.resource_url
decorator_context[:env] = env
decorator_context[:resource_title] = options[:resource_title]
decorator_context.merge!(options)
decorator_context
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/api/decorators/deployed_versions_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "pact_broker/api/decorators/base_decorator"
require "pact_broker/api/decorators/deployed_version_decorator"
require "pact_broker/api/decorators/embedded_deployed_version_decorator"

module PactBroker
module Api
module Decorators
class DeployedVersionsDecorator < BaseDecorator
collection :entries, as: :deployedVersions, embedded: true, :extend => PactBroker::Api::Decorators::DeployedVersionDecorator
collection :entries, as: :deployedVersions, embedded: true, :extend => PactBroker::Api::Decorators::EmbeddedDeployedVersionDecorator

link :self do | context |
href = append_query_if_present(context[:resource_url], context[:query_string])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "pact_broker/api/decorators/base_decorator"
require "pact_broker/api/decorators/embedded_pacticipant_decorator"
require "pact_broker/api/decorators/embedded_version_decorator"
require "pact_broker/api/decorators/environment_decorator"

module PactBroker
module Api
module Decorators
class EmbeddedDeployedVersionDecorator < BaseDecorator
property :uuid
property :currently_deployed, camelize: true
property :target, camelize: true # deprecated
property :applicationInstance, getter: lambda { |_| target }
property :undeployedAt, getter: lambda { |_| undeployed_at ? FormatDateTime.call(undeployed_at) : nil }, writeable: false

property :pacticipant, :extend => EmbeddedPacticipantDecorator, writeable: false, embedded: true, if: -> (user_options:, **_other) { user_options[:expand]&.include?(:pacticipant) }
property :version, :extend => EmbeddedVersionDecorator, writeable: false, embedded: true, if: -> (user_options:, **_other) { user_options[:expand]&.include?(:version) }
property :environment, :extend => EnvironmentDecorator, writeable: false, embedded: true, if: -> (user_options:, **_other) { user_options[:expand]&.include?(:environment) }

include Timestamps

link :self do | user_options |
{
href: deployed_version_url(represented, user_options.fetch(:base_url))
}
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "pact_broker/api/decorators/base_decorator"
require "pact_broker/api/decorators/embedded_pacticipant_decorator"
require "pact_broker/api/decorators/embedded_version_decorator"
require "pact_broker/api/decorators/environment_decorator"

module PactBroker
module Api
module Decorators
class EmbeddedReleasedVersionDecorator < BaseDecorator
property :uuid
property :currently_supported, camelize: true
include Timestamps
property :supportEndedAt, getter: lambda { |_| support_ended_at ? FormatDateTime.call(support_ended_at) : nil }, writeable: false

property :pacticipant, :extend => EmbeddedPacticipantDecorator, writeable: false, embedded: true, if: -> (user_options:, **_other) { user_options[:expand]&.include?(:pacticipant) }
property :version, :extend => EmbeddedVersionDecorator, writeable: false, embedded: true, if: -> (user_options:, **_other) { user_options[:expand]&.include?(:version) }
property :environment, :extend => EnvironmentDecorator, writeable: false, embedded: true, if: -> (user_options:, **_other) { user_options[:expand]&.include?(:environment) }

link :self do | user_options |
{
href: released_version_url(represented, user_options.fetch(:base_url))
}
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/pact_broker/api/decorators/extended_pact_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require "pact_broker/api/decorators/pact_decorator"

# Pactflow notes:
# This decorator was added for Pactflow, but we needed to change it so much that there
# is a separate class in pact_broker_fork/lib/pactflow/api/decorators/extended_pact_decorator.rb now
# and this one isn't used.

module PactBroker
module Api
module Decorators
Expand Down Expand Up @@ -47,4 +52,4 @@ def head_tags
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/pact_broker/api/decorators/pagination_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module PaginationLinks
represented.respond_to?(:page_count) &&
represented.current_page < represented.page_count
{
href: context[:resource_url] + "?pageSize=#{represented.page_size}&pageNumber=#{represented.current_page + 1}",
href: context[:resource_url] + "?size=#{represented.page_size}&page=#{represented.current_page + 1}",
title: "Next page"
}

Expand All @@ -34,7 +34,7 @@ module PaginationLinks
link :previous do | context |
if represented.respond_to?(:first_page?) && !represented.first_page?
{
href: context[:resource_url] + "?pageSize=#{represented.page_size}&pageNumber=#{represented.current_page - 1}",
href: context[:resource_url] + "?size=#{represented.page_size}&page=#{represented.current_page - 1}",
title: "Previous page"
}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/api/decorators/released_versions_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "pact_broker/api/decorators/base_decorator"
require "pact_broker/api/decorators/released_version_decorator"
require "pact_broker/api/decorators/embedded_released_version_decorator"

module PactBroker
module Api
module Decorators
class ReleasedVersionsDecorator < BaseDecorator
collection :entries, as: :releasedVersions, embedded: true, :extend => PactBroker::Api::Decorators::ReleasedVersionDecorator
collection :entries, as: :releasedVersions, embedded: true, :extend => PactBroker::Api::Decorators::EmbeddedReleasedVersionDecorator

link :self do | context |
href = append_query_if_present(context[:resource_url], context[:query_string])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module PactBroker
module Api
module Decorators
class RuntimeErrorProblemJSONDecorator
class RuntimeErrorProblemJsonDecorator

# @param message [String]
def initialize(message)
Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/api/decorators/version_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class VersionDecorator < BaseDecorator
{
title: "Version",
name: represented.number,
href: version_url(options.fetch(:base_url), represented)
# This decorator is used for multiple Version resources, so dynamically fetch the current resource URL
href: options.fetch(:resource_url)
}
end

Expand Down
16 changes: 13 additions & 3 deletions lib/pact_broker/api/decorators/versions_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ module PactBroker
module Api
module Decorators
class VersionsDecorator < BaseDecorator
class VersionInCollectionDecorator < PactBroker::Api::Decorators::VersionDecorator
# VersionDecorator has a dynamic self URL, depending which path the Version resource is mounted at.
# Hardcode the URL of the embedded Versions in this collection to use the canonical URL with the version number.
link :self do | user_options |
{
title: "Version",
name: represented.number,
href: version_url(user_options.fetch(:base_url), represented)
}
end
end

collection :entries, as: :versions, embedded: true, :extend => PactBroker::Api::Decorators::VersionDecorator
collection :entries, as: :versions, embedded: true, :extend => VersionInCollectionDecorator

link :self do | user_options |
href = append_query_if_present(user_options[:resource_url], user_options[:query_string])
{
href: href,
href: user_options.fetch(:request_url),
title: user_options[:resource_title] || "All application versions of #{user_options[:pacticipant_name]}"
}
end
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ def branch_version_url(branch_version, base_url = "")
"#{branch_versions_url(branch_version.branch, base_url)}/#{url_encode(branch_version.version_number)}"
end

def latest_version_for_branch_url(branch, base_url = "")
"#{branch_url(branch, base_url)}/latest-version"
end

def templated_tag_url_for_pacticipant pacticipant_name, base_url = ""
pacticipant_url_from_params({ pacticipant_name: pacticipant_name }, base_url) + "/versions/{version}/tags/{tag}"
end
Expand Down Expand Up @@ -283,7 +287,7 @@ def webhooks_url base_url
"#{base_url}/webhooks"
end

def webhook_url uuid, base_url
def webhook_url uuid, base_url = ""
"#{base_url}/webhooks/#{uuid}"
end

Expand Down
6 changes: 3 additions & 3 deletions lib/pact_broker/api/resources/all_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def to_json
end

def from_json
saved_webhook = webhook_service.create(next_uuid, webhook, consumer, provider)
saved_webhook = webhook_service.create(next_uuid, webhook, webhook_consumer, webhook_provider)
response.body = decorator_class(:webhook_decorator).new(saved_webhook).to_json(**decorator_options)
end

Expand All @@ -60,11 +60,11 @@ def schema
api_contract_class(:webhook_contract)
end

def consumer
def webhook_consumer
webhook.consumer&.name ? pacticipant_service.find_pacticipant_by_name(webhook.consumer.name) : nil
end

def provider
def webhook_provider
webhook.provider&.name ? pacticipant_service.find_pacticipant_by_name(webhook.provider.name) : nil
end

Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def invalid_json?
def find_pacticipant name, role
pacticipant_service.find_pacticipant_by_name(name).tap do | pacticipant |
if pacticipant.nil?
set_json_error_message("No #{role} with name '#{name}' found", title: "Not found", type: "not_found", status: 404)
set_json_error_message("No #{role} with name '#{name}' found", title: "Not found", type: "not-found", status: 404)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def resource_exists?
end

def to_json
decorator_class(decorator_name).new(deployed_versions).to_json(**decorator_options(title: title))
decorator_class(decorator_name).new(deployed_versions).to_json(**decorator_options(title: title, expand: [:pacticipant, :version]))
end

def policy_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def resource_exists?
end

def to_json
decorator_class(decorator_name).new(released_versions).to_json(**decorator_options(title: title))
decorator_class(decorator_name).new(released_versions).to_json(**decorator_options(title: title, expand: [:pacticipant, :version]))
end

def policy_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def application_instance
end

def title
"Deployed versions for #{pacticipant_name} version #{pacticipant_version_number}"
"Deployed versions for #{pacticipant_name} version #{pacticipant_version_number} in environment #{environment.display_name}"
end
end
end
Expand Down
Loading

0 comments on commit a2e6e29

Please sign in to comment.