-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
210 additions
and
7 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
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,80 @@ | ||
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 CanIDeployBadge < BaseResource | ||
def initialize | ||
super | ||
selector = PactBroker::Matrix::UnresolvedSelector.new(pacticipant_name: pacticipant_name, latest: true, tag: identifier_from_path[:tag]) | ||
@options = { | ||
latestby: 'cvp', | ||
latest: true, | ||
tag: identifier_from_path[:to] | ||
} | ||
@selectors = [selector] | ||
end | ||
|
||
def allowed_methods | ||
["GET", "OPTIONS"] | ||
end | ||
|
||
def content_types_provided | ||
[['image/svg+xml', :to_svg]] | ||
end | ||
|
||
def resource_exists? | ||
false | ||
end | ||
|
||
# Only called if resource_exists? returns false | ||
def previously_existed? | ||
true | ||
end | ||
|
||
def is_authorized?(authorization_header) | ||
super || PactBroker.configuration.enable_public_badge_access | ||
end | ||
|
||
def forbidden? | ||
false | ||
end | ||
|
||
def moved_temporarily? | ||
response.headers['Cache-Control'] = 'no-cache' | ||
begin | ||
if pacticipant | ||
if version | ||
badge_service.can_i_deploy_badge_url(selectors.first.pacticipant_name, options[:tag], results.deployable?) | ||
else | ||
badge_service.error_badge_url("version", "not found") | ||
end | ||
else | ||
badge_service.error_badge_url(selectors.first.pacticipant_name, "not found") | ||
end | ||
rescue StandardError => e | ||
# Want to render a badge, even if there's an error | ||
badge_service.error_badge_url("error", ErrorHandler.display_message(e, "reference: #{ErrorHandler.generate_error_reference}")) | ||
end | ||
end | ||
|
||
def policy_name | ||
:'badges::badge' | ||
end | ||
|
||
private | ||
|
||
attr_reader :selectors, :options | ||
|
||
def results | ||
@results ||= matrix_service.find(selectors, options) | ||
end | ||
|
||
def version | ||
@version ||= version_service.find_by_pacticipant_name_and_latest_tag(identifier_from_path[:pacticipant_name], identifier_from_path[:tag]) | ||
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
7 changes: 7 additions & 0 deletions
7
lib/pact_broker/doc/views/pacticipant/can-i-deploy-badge.markdown
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,7 @@ | ||
# Can I Deploy Badge | ||
|
||
Allowed methods: `GET` | ||
|
||
Path: `/pacticipants/{pacticipant}/latest-version/{tag}/can-i-deploy/to/{environmentTag}/badge` | ||
|
||
Returns a status badge that can be displayed in a README file that indicates whether the specified version of a pacticipant can be deployed to the specified environment. |
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,13 @@ | ||
RSpec.describe "can i deploy badge" do | ||
before do | ||
td.create_pact_with_hierarchy("Foo", "1.2.3", "Bar") | ||
.create_consumer_version_tag("main") | ||
end | ||
|
||
subject { get("/pacticipants/Foo/latest-version/main/can-i-deploy/to/prod/badge", nil, { 'HTTP_ACCEPT' => 'image/svg+xml'}) } | ||
|
||
it "returns a redirect response" do | ||
expect(subject.status).to eq 307 | ||
expect(subject.headers['Location']).to start_with "https://img.shields.io/badge" | ||
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
71 changes: 71 additions & 0 deletions
71
spec/lib/pact_broker/api/resources/can_i_deploy_badge_spec.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,71 @@ | ||
require 'pact_broker/api/resources/can_i_deploy_badge' | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
describe CanIDeployBadge do | ||
before do | ||
allow_any_instance_of(described_class).to receive(:badge_service).and_return(badge_service) | ||
allow_any_instance_of(described_class).to receive(:matrix_service).and_return(matrix_service) | ||
allow_any_instance_of(described_class).to receive(:pacticipant_service).and_return(pacticipant_service) | ||
allow_any_instance_of(described_class).to receive(:version_service).and_return(version_service) | ||
allow(badge_service).to receive(:can_i_deploy_badge_url).and_return("http://badge") | ||
allow(badge_service).to receive(:error_badge_url).and_return("http://error") | ||
allow(matrix_service).to receive(:find).and_return(results) | ||
allow(pacticipant_service).to receive(:find_pacticipant_by_name).and_return(pacticipant) | ||
allow(version_service).to receive(:find_by_pacticipant_name_and_latest_tag).and_return(version) | ||
allow(PactBroker.configuration).to receive(:show_backtrace_in_error_response?).and_return(false) | ||
allow(ErrorHandler).to receive(:generate_error_reference).and_return("abcd") | ||
end | ||
|
||
let(:pacticipant_service) { class_double("PactBroker::Pacticipant::Service").as_stubbed_const } | ||
let(:badge_service) { class_double("PactBroker::Badges::Service").as_stubbed_const } | ||
let(:matrix_service) { class_double("PactBroker::Matrix::Service").as_stubbed_const } | ||
let(:version_service) { class_double("PactBroker::Version::Service").as_stubbed_const } | ||
let(:results) { double('results', deployable?: true) } | ||
let(:pacticipant) { double('pacticipant') } | ||
let(:version) { double('version') } | ||
|
||
let(:path) { "/pacticipants/Foo/latest-version/main/can-i-deploy/to/prod/badge"} | ||
|
||
subject { get(path) } | ||
|
||
context "when the pacticipant exists" do | ||
it "returns a redirect to the badge" do | ||
expect(badge_service).to receive(:can_i_deploy_badge_url).with("Foo", "prod", true) | ||
expect(subject.status).to eq 307 | ||
end | ||
end | ||
|
||
context "when the pacticipant does not exist" do | ||
let(:pacticipant) { nil } | ||
|
||
it "returns a redirect to a 'not found' badge" do | ||
expect(badge_service).to receive(:error_badge_url).with("Foo", "not found") | ||
expect(subject.status).to eq 307 | ||
end | ||
end | ||
|
||
context "when the version does not exist" do | ||
let(:version) { nil } | ||
|
||
it "returns a redirect to a 'not found' badge" do | ||
expect(badge_service).to receive(:error_badge_url).with("version", "not found") | ||
expect(subject.status).to eq 307 | ||
end | ||
end | ||
|
||
context "when there is an error" do | ||
before do | ||
allow(matrix_service).to receive(:find).and_raise("foo error") | ||
end | ||
|
||
it "returns a redirect to a badge with an error message" do | ||
expect(badge_service).to receive(:error_badge_url).with("error", "reference: abcd") | ||
expect(subject.status).to eq 307 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |