-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add endpoint to create pacts, pacticipant, version, tags in one…
… request (#420)
- Loading branch information
Showing
57 changed files
with
1,806 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
lib/pact_broker/api/contracts/publish_contracts_schema.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require 'dry-validation' | ||
require 'pact_broker/api/contracts/dry_validation_workarounds' | ||
require 'pact_broker/api/contracts/dry_validation_predicates' | ||
require 'pact_broker/messages' | ||
|
||
module PactBroker | ||
module Api | ||
module Contracts | ||
class PublishContractsSchema | ||
extend DryValidationWorkarounds | ||
using PactBroker::HashRefinements | ||
extend PactBroker::Messages | ||
|
||
SCHEMA = Dry::Validation.Schema do | ||
configure do | ||
predicates(DryValidationPredicates) | ||
config.messages_file = File.expand_path("../../../locale/en.yml", __FILE__) | ||
end | ||
|
||
required(:pacticipantName).filled(:str?, :not_blank?) | ||
required(:pacticipantVersionNumber).filled(:not_blank?, :single_line?) | ||
optional(:tags).each(:not_blank?, :single_line?) | ||
optional(:branch).maybe(:not_blank?, :single_line?) | ||
optional(:buildUrl).maybe(:single_line?) | ||
|
||
required(:contracts).each do | ||
required(:consumerName).filled(:str?, :not_blank?) | ||
required(:providerName).filled(:str?, :not_blank?) | ||
required(:content).filled(:str?) | ||
required(:contentType).filled(included_in?: ["application/json"]) | ||
required(:specification).filled(included_in?: ["pact"]) | ||
optional(:writeMode).filled(included_in?:["overwrite", "merge"]) | ||
end | ||
end | ||
|
||
def self.call(params) | ||
select_first_message( | ||
flatten_indexed_messages( | ||
add_cross_field_validation_errors( | ||
params&.symbolize_keys, | ||
SCHEMA.call(params&.symbolize_keys).messages(full: true) | ||
) | ||
) | ||
) | ||
end | ||
|
||
def self.add_cross_field_validation_errors(params, errors) | ||
if params[:contracts].is_a?(Array) | ||
params[:contracts].each_with_index do | contract, i | | ||
if contract.is_a?(Hash) | ||
validate_consumer_name(params, contract, i, errors) | ||
validate_consumer_name_in_content(params, contract, i, errors) | ||
validate_provider_name_in_content(contract, i, errors) | ||
validate_encoding(contract, i, errors) | ||
validate_content_matches_content_type(contract, i, errors) | ||
end | ||
end | ||
end | ||
errors | ||
end | ||
|
||
def self.validate_consumer_name(params, contract, i, errors) | ||
if params[:pacticipantName] && contract[:consumerName] && (contract[:consumerName] != params[:pacticipantName]) | ||
add_contract_error(validation_message('consumer_name_in_contract_mismatch_pacticipant_name', { consumer_name_in_contract: contract[:consumerName], pacticipant_name: params[:pacticipantName] } ), i, errors) | ||
end | ||
end | ||
|
||
def self.validate_consumer_name_in_content(params, contract, i, errors) | ||
consumer_name_in_content = contract.dig(:decodedParsedContent, :consumer, :name) | ||
if consumer_name_in_content && consumer_name_in_content != params[:pacticipantName] | ||
add_contract_error(validation_message('consumer_name_in_content_mismatch_pacticipant_name', { consumer_name_in_content: consumer_name_in_content, pacticipant_name: params[:pacticipantName] } ), i, errors) | ||
end | ||
end | ||
|
||
def self.validate_provider_name_in_content(contract, i, errors) | ||
provider_name_in_content = contract.dig(:decodedParsedContent, :provider, :name) | ||
if provider_name_in_content && provider_name_in_content != contract[:providerName] | ||
add_contract_error(validation_message('provider_name_in_content_mismatch', { provider_name_in_content: provider_name_in_content, provider_name: contract[:providerName] } ), i, errors) | ||
end | ||
end | ||
|
||
def self.validate_encoding(contract, i, errors) | ||
if contract[:decodedContent].nil? | ||
add_contract_error(message('errors.base64?', scope: nil), i, errors) | ||
end | ||
end | ||
|
||
def self.validate_content_matches_content_type(contract, i, errors) | ||
if contract[:decodedParsedContent].nil? && contract[:contentType] | ||
add_contract_error(validation_message('invalid_content_for_content_type', { content_type: contract[:contentType]}), i, errors) | ||
end | ||
end | ||
|
||
|
||
def self.add_contract_error(message, i, errors) | ||
errors[:contracts] ||= {} | ||
errors[:contracts][i] ||= [] | ||
errors[:contracts][i] << message | ||
errors | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/pact_broker/api/decorators/embedded_pacticipant_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require_relative 'base_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class EmbeddedPacticipantDecorator < BaseDecorator | ||
camelize_property_names | ||
|
||
property :name | ||
|
||
link :self do | options | | ||
pacticipant_url(options[:base_url], represented) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
lib/pact_broker/api/decorators/publish_contract_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'pact_broker/api/decorators/base_decorator' | ||
require 'pact_broker/api/decorators/timestamps' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class PublishContractDecorator < BaseDecorator | ||
camelize_property_names | ||
|
||
property :consumer_name | ||
property :provider_name | ||
property :specification | ||
property :content_type | ||
property :decoded_content | ||
property :write_mode, default: "overwrite" | ||
end | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
lib/pact_broker/api/decorators/publish_contracts_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'pact_broker/api/decorators/base_decorator' | ||
require 'pact_broker/api/decorators/publish_contract_decorator' | ||
require 'pact_broker/contracts/contract_to_publish' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class PublishContractsDecorator < BaseDecorator | ||
camelize_property_names | ||
|
||
property :pacticipant_name | ||
property :pacticipant_version_number | ||
property :tags | ||
property :branch | ||
property :build_url | ||
|
||
collection :contracts, :extend => PublishContractDecorator, class: PactBroker::Contracts::ContractToPublish | ||
end | ||
end | ||
end | ||
end |
54 changes: 54 additions & 0 deletions
54
lib/pact_broker/api/decorators/publish_contracts_results_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'pact_broker/api/decorators/base_decorator' | ||
require 'pact_broker/api/decorators/publish_contract_decorator' | ||
require 'pact_broker/api/decorators/embedded_version_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class PublishContractsResultsDecorator < BaseDecorator | ||
camelize_property_names | ||
|
||
property :logs, getter: ->(represented:, **) { represented.logs.collect(&:to_h) } | ||
|
||
property :pacticipant, embedded: true, extend: EmbeddedPacticipantDecorator | ||
property :version, embedded: true, extend: EmbeddedVersionDecorator | ||
|
||
link :'pb:pacticipant' do | options | | ||
{ | ||
title: "Pacticipant", | ||
name: represented.pacticipant.name, | ||
href: pacticipant_url(options.fetch(:base_url), represented.pacticipant) | ||
} | ||
end | ||
|
||
link :'pb:pacticipant-version' do | options | | ||
{ | ||
title: "Pacticipant version", | ||
name: represented.version.number, | ||
href: version_url(options.fetch(:base_url), represented.version) | ||
} | ||
end | ||
|
||
links :'pb:pacticipant-version-tags' do | options | | ||
represented.tags.collect do | tag | | ||
{ | ||
title: "Tag", | ||
name: tag.name, | ||
href: tag_url(options.fetch(:base_url), tag) | ||
} | ||
end | ||
end | ||
|
||
links :'pb:contracts' do | options | | ||
represented.contracts.collect do | contract | | ||
{ | ||
title: 'Pact', | ||
name: contract.name, | ||
href: pact_url(options.fetch(:base_url), contract) | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.