-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to publish provider contracts to pactflow (feature …
…toggle required) (#107)
- Loading branch information
Showing
11 changed files
with
402 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env ruby | ||
require "pactflow/client/cli/pactflow" | ||
|
||
Pactflow::Client::CLI::Pactflow.start |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require "pactflow/client/cli/provider_contract_commands" | ||
require "pact_broker/client/cli/custom_thor" | ||
|
||
module Pactflow | ||
module Client | ||
module CLI | ||
class Pactflow < PactBroker::Client::CLI::CustomThor | ||
include ::Pactflow::Client::CLI::ProviderContractCommands | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require "pact_broker/client/hash_refinements" | ||
|
||
module Pactflow | ||
module Client | ||
module CLI | ||
module ProviderContractCommands | ||
using PactBroker::Client::HashRefinements | ||
|
||
def self.included(thor) | ||
thor.class_eval do | ||
|
||
if ENV.fetch("PACTFLOW_FEATURES", "").include?("publish-provider-contract") | ||
|
||
desc 'publish-provider-contract CONTRACT_FILE ...', "Publish provider contract to Pactflow" | ||
method_option :provider, required: true, desc: "The provider name" | ||
method_option :provider_app_version, required: true, aliases: "-a", desc: "The provider application version" | ||
method_option :branch, aliases: "-h", desc: "Repository branch of the provider version" | ||
#method_option :auto_detect_version_properties, hidden: true, type: :boolean, default: false, desc: "Automatically detect the repository branch from known CI environment variables or git CLI." | ||
method_option :tag, aliases: "-t", type: :array, banner: "TAG", desc: "Tag name for provider version. Can be specified multiple times." | ||
#method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false" | ||
method_option :specification, default: "oas", desc: "The contract specification" | ||
method_option :content_type, desc: "The content type. eg. application/yml" | ||
method_option :verification_success, type: :boolean | ||
method_option :verification_results, desc: "The path to the file containing the output from the verification process" | ||
method_option :verification_results_content_type, desc: "The content type of the verification output eg. text/plain, application/yaml" | ||
method_option :verification_results_format, desc: "The format of the verification output eg. junit, text" | ||
method_option :verifier, desc: "The tool used to verify the provider contract" | ||
method_option :verifier_version, desc: "The version of the tool used to verify the provider contract" | ||
#method_option :build_url, desc: "The build URL that created the pact" | ||
|
||
output_option_json_or_text | ||
shared_authentication_options | ||
|
||
def publish_provider_contract(provider_contract_path) | ||
require "pactflow/client/provider_contracts/publish" | ||
|
||
params = params = { | ||
provider_name: options.provider.strip, | ||
provider_version_number: options.provider_app_version.strip, | ||
branch_name: options.branch && options.branch.strip, | ||
tags: (options.tag && options.tag.collect(&:strip)) || [], | ||
contract: { | ||
content: File.read(provider_contract_path), | ||
content_type: options.content_type, | ||
specification: options.specification | ||
}, | ||
verification_results: { | ||
success: options.verification_success, | ||
content: options.verification_results ? File.read(options.verification_results) : nil, | ||
content_type: options.verification_results_content_type, | ||
format: options.verification_results_format, | ||
verifier: options.verifier, | ||
verifier_version: options.verifier_version | ||
} | ||
} | ||
|
||
command_options = { verbose: options.verbose, output: options.output } | ||
result = ::Pactflow::Client::ProviderContracts::Publish.call(params, command_options, pact_broker_client_options) | ||
$stdout.puts result.message | ||
exit(1) unless result.success | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require "pact_broker/client/base_command" | ||
require "pact_broker/client/versions/create" | ||
require "base64" | ||
|
||
module Pactflow | ||
module Client | ||
module ProviderContracts | ||
class Publish < PactBroker::Client::BaseCommand | ||
attr_reader :branch_name, :tags, :provider_name, :provider_version_number, :contract, :verification_results | ||
|
||
def initialize(params, options, pact_broker_client_options) | ||
super | ||
@provider_name = params[:provider_name] | ||
@provider_version_number = params[:provider_version_number] | ||
@branch_name = params[:branch_name] | ||
@tags = params[:tags] || [] | ||
@contract = params[:contract] | ||
@verification_results = params[:verification_results] | ||
end | ||
|
||
private | ||
|
||
def do_call | ||
create_branch_version_and_tags | ||
create_contract | ||
PactBroker::Client::CommandResult.new(true, green("Successfully published provider contract for #{provider_name} version #{provider_version_number}")) | ||
end | ||
|
||
def create_branch_version_and_tags | ||
if branch_name || tags.any? | ||
pacticipant_version_params = { | ||
pacticipant_name: provider_name, | ||
version_number: provider_version_number, | ||
branch_name: branch_name, | ||
tags: tags | ||
} | ||
result = PactBroker::Client::Versions::Create.call(pacticipant_version_params, options, pact_broker_client_options) | ||
if !result.success | ||
raise PactBroker::Client::Error.new(result.message) | ||
end | ||
end | ||
end | ||
|
||
def create_contract | ||
contract_path = "#{pact_broker_base_url}/contracts/provider/{provider}/version/{version}" | ||
entrypoint = create_entry_point(contract_path, pact_broker_client_options) | ||
entrypoint.expand(provider: provider_name, version: provider_version_number).put!(contract_params) | ||
end | ||
|
||
def contract_params | ||
verification_results_params = { | ||
success: verification_results[:success], | ||
content: verification_results[:content] ? encode_content(verification_results[:content]) : nil, | ||
contentType: verification_results[:content_type], | ||
format: verification_results[:format], | ||
verifier: verification_results[:verifier], | ||
verifierVersion: verification_results[:verifier_version] | ||
}.compact | ||
|
||
body_params = { | ||
content: encode_content(contract[:content]), | ||
contractType: contract[:specification], | ||
contentType: contract[:content_type], | ||
}.compact | ||
|
||
if verification_results_params.any? | ||
body_params[:verificationResults] = verification_results_params | ||
end | ||
|
||
body_params | ||
end | ||
|
||
def encode_content oas | ||
Base64.strict_encode64(oas) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: Sample API | ||
description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML. | ||
version: 0.1.9 | ||
servers: | ||
- url: https://test.pactflow.io | ||
description: Prod | ||
- url: https://test.test.pactflow.io | ||
description: Test | ||
paths: | ||
/admin/teams/{uuid}: | ||
get: | ||
summary: Returns a team | ||
parameters: | ||
- name: uuid | ||
in: path | ||
description: The UUID of the team to return | ||
required: true | ||
example: 85ad09f5-e014-4e0f-a146-4377fa64b5ef | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: A JSON team | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
additionalProperties: false | ||
properties: | ||
name: | ||
type: string | ||
example: Team Awesome | ||
uuid: | ||
type: string | ||
example: 85ad09f5-e014-4e0f-a146-4377fa64b5ef | ||
numberOfMembers: | ||
type: integer | ||
example: 4 | ||
_links: | ||
type: object | ||
properties: | ||
self: | ||
type: object | ||
properties: | ||
href: | ||
type: string | ||
example: http://test.pactflow.io/admin/teams/85ad09f5-e014-4e0f-a146-4377fa64b5ef |
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,27 @@ | ||
export PACT_BROKER_BASE_URL="http://localhost:9292" | ||
export PACTFLOW_FEATURES=publish-provider-contract | ||
# bundle exec bin/pactflow publish-provider-contract \ | ||
# script/oas.yml \ | ||
# --provider Foo \ | ||
# --provider-app-version 1013b5650d61214e19f10558f97fb5a3bb082d44 \ | ||
# --branch main \ | ||
# --tag dev \ | ||
# --specification oas \ | ||
# --content-type application/yml \ | ||
# --no-verification-success \ | ||
# --verification-results script/verification-results.txt \ | ||
# --verification-results-content-type text/plain \ | ||
# --verification-results-format text \ | ||
# --verifier my-custom-tool \ | ||
# --verifier-version "1.0" \ | ||
# --verbose | ||
|
||
|
||
bundle exec bin/pactflow publish-provider-contract \ | ||
script/oas.yml \ | ||
--provider Foo \ | ||
--provider-app-version 1013b5650d61214e19f10558f97fb5a3bb082d44 \ | ||
--branch main \ | ||
--tag dev \ | ||
--specification oas \ | ||
--content-type application/yml |
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 @@ | ||
These are some results. |
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.