-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add can-i-deploy endpoint to expose a simplified interface to t…
…he /matrix resource that accepts the same parameters as the can-i-deploy CLI.
- Loading branch information
Showing
7 changed files
with
136 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'pact_broker/api/resources/matrix' | ||
require 'pact_broker/matrix/can_i_deploy_query_schema' | ||
require 'pact_broker/matrix/parse_can_i_deploy_query' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class CanIDeploy < Matrix | ||
def initialize | ||
@query_params = JSON.parse(Rack::Utils.parse_nested_query(request.uri.query).to_json, symbolize_names: true) | ||
@selectors, @options = PactBroker::Matrix::ParseCanIDeployQuery.call(query_params) | ||
end | ||
|
||
def malformed_request? | ||
if (errors = query_schema.call(query_params)).any? | ||
set_json_validation_error_messages(errors) | ||
true | ||
else | ||
false | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :query_params | ||
|
||
def query_schema | ||
PactBroker::Api::Contracts::CanIDeployQuerySchema | ||
end | ||
|
||
def selectors | ||
@selectors | ||
end | ||
|
||
def options | ||
@options | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'dry-validation' | ||
|
||
module PactBroker | ||
module Api | ||
module Contracts | ||
class CanIDeployQuerySchema | ||
SCHEMA = Dry::Validation.Schema do | ||
required(:pacticipant).filled(:str?) | ||
required(:version).filled(:str?) | ||
optional(:to).filled(:str?) | ||
end | ||
|
||
def self.call(params) | ||
select_first_message(SCHEMA.call(params).messages(full: true)) | ||
end | ||
|
||
def self.select_first_message(messages) | ||
messages.each_with_object({}) do | (key, value), new_messages | | ||
new_messages[key] = [value.first] | ||
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,29 @@ | ||
require 'rack/utils' | ||
|
||
module PactBroker | ||
module Matrix | ||
class ParseCanIDeployQuery | ||
def self.call params | ||
selector = {} | ||
options = { | ||
latestby: 'cvp', | ||
latest: true | ||
} | ||
|
||
if params[:pacticipant].is_a?(String) | ||
selector[:pacticipant_name] = params[:pacticipant] | ||
end | ||
|
||
if params[:version].is_a?(String) | ||
selector[:pacticipant_version_number] = params[:version] | ||
end | ||
|
||
if params[:to].is_a?(String) | ||
options[:tag] = params[:to] | ||
end | ||
|
||
return [selector], options | ||
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,31 @@ | ||
RSpec.describe "can i deploy" do | ||
before do | ||
td.create_pact_with_hierarchy("Foo", "1.2.3", "Bar") | ||
end | ||
|
||
let(:query) do | ||
{ | ||
pacticipant: "Foo", | ||
version: "1.2.3", | ||
to: "prod" | ||
} | ||
end | ||
|
||
let(:response_body) { JSON.parse(subject.body, symbolize_names: true) } | ||
|
||
subject { get("/can-i-deploy", query, { 'HTTP_ACCEPT' => 'application/hal+json'}) } | ||
|
||
it "returns the matrix response" do | ||
expect(subject).to be_a_hal_json_success_response | ||
expect(response_body[:matrix]).to be_instance_of(Array) | ||
end | ||
|
||
context "with a validation error" do | ||
let(:query) { {} } | ||
|
||
it "returns an error response" do | ||
expect(subject.status).to eq 400 | ||
expect(response_body[:errors]).to be_instance_of(Hash) | ||
end | ||
end | ||
end |