Skip to content

Commit

Permalink
feat: allow label of can-i-deploy badge to be customised by setting t…
Browse files Browse the repository at this point in the history
…he label query parameter
  • Loading branch information
bethesque committed Sep 25, 2020
1 parent 887a9ca commit ed544f9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/pact_broker/api/resources/can_i_deploy_badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def moved_temporarily?
begin
if pacticipant
if version
badge_service.can_i_deploy_badge_url(selectors.first.pacticipant_name, options[:tag], results.deployable?)
badge_service.can_i_deploy_badge_url(pacticipant_name, identifier_from_path[:tag], identifier_from_path[:to], label, results.deployable?)
else
badge_service.error_badge_url("version", "not found")
end
Expand Down Expand Up @@ -74,6 +74,11 @@ def results
def version
@version ||= version_service.find_by_pacticipant_name_and_latest_tag(identifier_from_path[:pacticipant_name], identifier_from_path[:tag])
end

def label
lab = request.query['label']
lab && !lab.empty? ? lab : nil
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/badges/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def pact_verification_badge_url(pact, label, initials, pseudo_branch_verificatio
build_shield_io_uri(title, status, color)
end

def can_i_deploy_badge_url(pacticipant_name, environment_tag, deployable)
title = "Can I deploy #{pacticipant_name} to #{environment_tag}?"
def can_i_deploy_badge_url(pacticipant_name, tag, environment_tag, label, deployable)
title = label || "Can I deploy #{tag} #{pacticipant_name} to #{environment_tag}?"
status = deployable ? "yes" : "no"
color = deployable ? "brightgreen" : "red"
build_shield_io_uri(title, status, color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ 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.

To set a custom label for the badge, set the `label` query parameter. eg `?label=my+custom+label+here`.
11 changes: 10 additions & 1 deletion spec/lib/pact_broker/api/resources/can_i_deploy_badge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Resources

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(badge_service).to receive(:can_i_deploy_badge_url).with("Foo", "main", "prod", nil, true)
expect(subject.status).to eq 307
end
end
Expand All @@ -55,6 +55,15 @@ module Resources
end
end

context "with a custom label" do
subject { get(path, label: "some custom label") }

it "returns a redirect to a badge with a custom label" do
expect(badge_service).to receive(:can_i_deploy_badge_url).with("Foo", "main", "prod", "some custom label", true)
subject
end
end

context "when there is an error" do
before do
allow(matrix_service).to receive(:find).and_raise("foo error")
Expand Down

0 comments on commit ed544f9

Please sign in to comment.