Skip to content

Commit

Permalink
Merge pull request #708 from pact-foundation/fix/change-can-i-merge-b…
Browse files Browse the repository at this point in the history
…adge-text

chore: changed can-i-merge build badge text
  • Loading branch information
pahnin authored Aug 7, 2024
2 parents f95f70b + b25a616 commit 106db02
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ Lint/EachWithObjectArgument:
Lint/ElseLayout:
Description: 'Check for odd code arrangement in an else block.'
Enabled: true
EnforcedStyleAlignWith: variable

Lint/EmptyEnsure:
Description: 'Checks for empty ensure block.'
Expand Down
7 changes: 2 additions & 5 deletions lib/pact_broker/api/resources/can_i_merge_badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ class CanIMergeBadge < BaseResource
include BadgeMethods # This module contains all necessary webmachine methods for badge implementation

def badge_url
if pacticipant.nil?
if pacticipant.nil? # pacticipant method is defined in BaseResource
# if the pacticipant is nil, we return an error badge url
badge_service.error_badge_url("pacticipant", "not found")
elsif version.nil?
# when there is no main branch version, we return an error badge url
badge_service.error_badge_url("main branch version", "not found")
else
# we call badge_service to build the badge url
badge_service.can_i_merge_badge_url(
version_number: version.number,
deployable: results
)
badge_service.can_i_merge_badge_url(deployable: results)
end
end

Expand Down
18 changes: 11 additions & 7 deletions lib/pact_broker/badges/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ def can_i_deploy_badge_url(tag, environment_tag, label, deployable)
build_shield_io_uri(title, status, color)
end

def can_i_merge_badge_url(version_number: nil, deployable: nil)
def can_i_merge_badge_url(deployable: nil)
title = "can-i-merge"
status = deployable ? "yes" : "no"
if deployable.nil?
color = "lightgrey"
status = "unknown"

# rubocop:disable Layout/EndAlignment
color, status = case deployable
when nil
[ "lightgrey", "unknown" ]
when true
[ "brightgreen", "success" ]
else
color = deployable ? "brightgreen" : "red"
status = version_number
[ "red", "failed" ]
end
# rubocop:enable Layout/EndAlignment

# left text is "can-i-merge", right text is the version number
build_shield_io_uri(title, status, color)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Resources
end

it "return the badge URL" do
expect(badge_service). to receive(:can_i_merge_badge_url).with(version_number: "1", deployable: true)
expect(badge_service). to receive(:can_i_merge_badge_url).with(deployable: true)
expect(subject.headers["Location"]).to eq "http://badge_url"
end
end
Expand Down
7 changes: 4 additions & 3 deletions spec/lib/pact_broker/badges/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ module Badges
let(:version_number) { "abcd1234" }
let(:deployable) { true }

subject { Service.can_i_merge_badge_url(version_number: version_number, deployable: deployable) }
subject { Service.can_i_merge_badge_url(deployable: deployable) }

context "when deployable is true" do
it { is_expected.to eq URI("https://img.shields.io/badge/can--i--merge-abcd1234-brightgreen.svg") }
it { is_expected.to eq URI("https://img.shields.io/badge/can--i--merge-success-brightgreen.svg") }
end

context "when deployable is false" do
let(:deployable) { false }
it { is_expected.to eq URI("https://img.shields.io/badge/can--i--merge-abcd1234-red.svg") }
it { is_expected.to eq URI("https://img.shields.io/badge/can--i--merge-failed-red.svg") }
end
end

Expand Down

0 comments on commit 106db02

Please sign in to comment.