Skip to content

Commit

Permalink
Merge branch 'provider_contract_link' into auto_detect_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 27, 2022
2 parents 2507941 + 8cf3c96 commit dcf6472
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 16 deletions.
43 changes: 43 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pactflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* [A request to create a provider contract](#a_request_to_create_a_provider_contract)

* [A request to create a provider contract](#a_request_to_create_a_provider_contract_given_there_is_a_pf:ui_href_in_the_response) given there is a pf:ui href in the response

* [A request to create a webhook for a team](#a_request_to_create_a_webhook_for_a_team_given_a_team_with_UUID_2abbc12a-427d-432a-a521-c870af1739d9_exists) given a team with UUID 2abbc12a-427d-432a-a521-c870af1739d9 exists

#### Interactions
Expand Down Expand Up @@ -77,6 +79,47 @@ Pactflow will respond with:
}
}
```
<a name="a_request_to_create_a_provider_contract_given_there_is_a_pf:ui_href_in_the_response"></a>
Given **there is a pf:ui href in the response**, upon receiving **a request to create a provider contract** from Pact Broker Client, with
```json
{
"method": "put",
"path": "/contracts/provider/Bar/version/1",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"content": "LS0tCjpzb21lOiBjb250cmFjdAo=",
"contractType": "oas",
"contentType": "application/yaml",
"verificationResults": {
"success": true,
"content": "c29tZSByZXN1bHRz",
"contentType": "text/plain",
"format": "text",
"verifier": "my custom tool",
"verifierVersion": "1.0"
}
}
}
```
Pactflow will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pf:ui": {
"href": "http://localhost:1235/contracts/bi-directional/provider/Bar/version/1/provider-contract"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_for_a_team_given_a_team_with_UUID_2abbc12a-427d-432a-a521-c870af1739d9_exists"></a>
Given **a team with UUID 2abbc12a-427d-432a-a521-c870af1739d9 exists**, upon receiving **a request to create a webhook for a team** from Pact Broker Client, with
```json
Expand Down
116 changes: 116 additions & 0 deletions example/scripts/oas.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
openapi: 3.0.1
info:
title: Product API
description: Pactflow Product API demo
version: 1.0.0
paths:
/products:
post:
summary: Create a product
description: Creates a new product
operationId: createProduct
requestBody:
description: Create a new Product
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
id: "1234"
type: "food"
price: 42
responses:
"200":
description: successful operation
content:
"application/json; charset=utf-8":
schema:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
id: "1234"
type: "food"
price: 42
get:
summary: List all products
description: Returns all products
operationId: getAllProducts
responses:
"200":
description: successful operation
content:
"application/json; charset=utf-8":
schema:
type: "array"
items:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
- id: "1234"
type: "food"
price: 42
# name: "pizza"
# version: "1.0.0"
# see https://github.com/apiaryio/dredd/issues/1430 for why
"400":
description: Invalid ID supplied
content: {}
/product/{id}:
get:
summary: Find product by ID
description: Returns a single product
operationId: getProductByID
parameters:
- name: id
in: path
description: ID of product to get
schema:
type: string
required: true
example: 10
responses:
"200":
description: successful operation
content:
"application/json; charset=utf-8":
schema:
$ref: '#/components/schemas/Product'
examples:
application/json:
value:
id: "1234"
type: "food"
price: 42
# name: "pizza"
# version: "1.0.0"
# see https://github.com/apiaryio/dredd/issues/1430 for why
"400":
description: Invalid ID supplied
content: {}
"404":
description: Product not found
content: {}
components:
schemas:
Product:
type: object
required:
- id
- name
- price
properties:
id:
type: string
type:
type: string
name:
type: string
version:
type: string
price:
type: number
12 changes: 12 additions & 0 deletions example/scripts/publish-provider-contract.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already

bundle exec bin/pactflow publish-provider-contract $(dirname "$0")/oas.yml \
--provider pactflow-cli-test-provider \
--provider-app-version 1.0.0 \
--branch master \
--tag master \
--content-type application/yaml \
--verification-exit-code=0 \
--verification-results $(dirname "$0")/oas.yml \
--verification-results-content-type application/yaml \
--verifier pactflow-cli-test-provider
4 changes: 4 additions & 0 deletions lib/pact_broker/client/base_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def green(text)
::Term::ANSIColor.green(text)
end

def blue(text)
::Term::ANSIColor.blue(text)
end

def red(text)
::Term::ANSIColor.red(text)
end
Expand Down
44 changes: 28 additions & 16 deletions lib/pactflow/client/provider_contracts/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,29 @@ def initialize(params, options, pact_broker_client_options)

def do_call
create_branch_version_and_tags
res = create_contract
render_response(create_contract)
end

def render_response(res)
if res.body && res.body['_links'] && res.body['_links']['pf:ui']['href']
ui_url = "View the uploaded contract at #{res.body['_links']['pf:ui']['href']}"
ui_url = "\nView the uploaded contract at #{blue(res.body['_links']['pf:ui']['href'])}"
PactBroker::Client::CommandResult.new(true,
green("Successfully published provider contract for Bar version 1\n#{ui_url}"))
green("Successfully published provider contract for #{provider_name} version #{provider_version_number} to Pactflow#{ui_url}#{next_steps}"))
else
PactBroker::Client::CommandResult.new(true,
green('Successfully published provider contract for Bar version 1'))
green("Successfully published provider contract for #{provider_name} version #{provider_version_number} to Pactflow#{next_steps}"))
end
end

def next_steps
[green("\nNext steps:\n"),
" #{green("Check your application is safe to deploy - #{blue('https://docs.pact.io/can_i_deploy')}:\n")}",
" #{"pact-broker can-i-deploy --pacticipant #{provider_name} --version #{provider_version_number} --to-environment <your environment name>\n"}",
" #{green("Record deployment or release to specified environment (choose one) - #{blue('https://docs.pact.io/go/record-deployment')}:\n")}",
" #{"pact-broker record-deployment --pacticipant #{provider_name} --version #{provider_version_number} --environment <your environment name>"}\n",
" #{"pact-broker record-release --pacticipant #{provider_name} --version #{provider_version_number} --environment <your environment name>"}"].join('')
end

def create_branch_version_and_tags
if branch_name || tags.any?
pacticipant_version_params = {
Expand All @@ -50,28 +62,28 @@ def create_branch_version_and_tags
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).response
entrypoint.expand(provider: provider_name, version: provider_version_number).put!(contract_params).response
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

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

body_params[:verificationResults] = verification_results_params if verification_results_params.any?

if verification_results_params.any?
body_params[:verificationResults] = verification_results_params
end
body_params
end

Expand Down
37 changes: 37 additions & 0 deletions spec/pacts/pact_broker_client-pactflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@
}
}
}
},
{
"description": "a request for the index resource",
"request": {
"method": "put",
"path": "/contracts/provider/Bar/version/1",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"content": "LS0tCjpzb21lOiBjb250cmFjdAo=",
"contractType": "oas",
"contentType": "application/yaml",
"verificationResults": {
"success": true,
"content": "c29tZSByZXN1bHRz",
"contentType": "text/plain",
"format": "text",
"verifier": "my custom tool",
"verifierVersion": "1.0"
}
}
},
"response": {
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pf:ui": {
"href": "http://localhost:1235/contracts/bi-directional/provider/Bar/version/1/provider-contract"
}
}
}
}
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
expect(subject).to be_a PactBroker::Client::CommandResult
expect(subject.success).to be true
expect(subject.message).to include "Successfully published provider contract for Bar version 1"
expect(subject.message).to include "Next steps:"
expect(subject.message).to include success_response_with_pf_ui_url[:body][:_links][:'pf:ui'][:href]
end
end
Expand Down

0 comments on commit dcf6472

Please sign in to comment.