Skip to content

Commit

Permalink
feat(pactflow): allow provider-contract file to be fetched from url n…
Browse files Browse the repository at this point in the history
…ote: without auth
  • Loading branch information
YOU54F committed Aug 7, 2024
1 parent 7d71aeb commit e7c7612
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
22 changes: 22 additions & 0 deletions example/scripts/publish-provider-contract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,25 @@ bundle exec bin/pactflow publish-provider-contract $(dirname "$0")/oas.yml \
--verification-results $(dirname "$0")/oas.yml \
--verification-results-content-type application/yaml \
--verifier pactflow-cli-test-provider

bundle exec bin/pactflow publish-provider-contract https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml \
--provider pactflow-cli-test-provider-via-uri-yaml \
--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-via-uri-yaml

bundle exec bin/pactflow publish-provider-contract https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json \
--provider pactflow-cli-test-provider-via-uri-json \
--provider-app-version 1.0.0 \
--branch master \
--tag master \
--content-type application/json \
--verification-exit-code=0 \
--verification-results $(dirname "$0")/oas.yml \
--verification-results-content-type application/yaml \
--verifier pactflow-cli-test-provider-via-uri-json
18 changes: 17 additions & 1 deletion lib/pactflow/client/cli/provider_contract_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def publish_provider_contract_command_params(provider_contract_path)
tags: (options.tag && options.tag.collect(&:strip)) || [],
build_url: options.build_url,
contract: {
content: File.read(provider_contract_path),
content: get_oas_contents(provider_contract_path),
content_type: options.content_type,
specification: options.specification
},
Expand All @@ -86,6 +86,22 @@ def publish_provider_contract_command_params(provider_contract_path)
}
}
end

def get_oas_contents(input)
if input.is_a?(String)
if URI.parse(input).is_a?(URI::HTTP) || URI.parse(input).is_a?(URI::HTTPS)
require 'net/http'
uri = URI.parse(input)
response = Net::HTTP.get_response(uri)
response.is_a?(Net::HTTPSuccess) ? nil : raise(StandardError, "Failed to retrieve CONTRACT_FILE from URL: #{uri}")
response.body
else
File.read(input)
end
else
raise ArgumentError, "CONTRACT_FILE #{input} must be a filepath or a URI"
end
end
end
end
end
Expand Down
Empty file.

0 comments on commit e7c7612

Please sign in to comment.