Skip to content

Commit

Permalink
feat: trim long versions on can-i-deploy formatting (pact-foundation#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
konalegi authored Aug 13, 2021
1 parent 56e7a9d commit 86d21e1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/pact_broker/client/matrix/abbreviate_version_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ module PactBroker
module Client
class Matrix
class AbbreviateVersionNumber
def self.call version_number
if version_number
version_number.gsub(/[A-Za-z0-9]{40}/) do | val |
val[0...7] + "..."
end
# Official regex from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SEMVER_REGEX = /(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/
SHA1_REGEX = /[A-Za-z0-9]{40}/

class << self
def call version_number
return unless version_number

return replace_all_git_sha(version_number) if [SEMVER_REGEX, SHA1_REGEX].all?{|r| r.match?(version_number) }

return replace_all_git_sha(version_number) if Regexp.new("\\A#{SHA1_REGEX.source}\\z").match?(version_number)

# Trim to some meaningful value in case we couldn't match anything, just not to mess with the output
return version_number[0...60] + '...' if version_number.length > 60

version_number
end

private

def replace_all_git_sha(version)
version.gsub(SHA1_REGEX) { |val| val[0...7] + '...' }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

require 'pact_broker/client/matrix/abbreviate_version_number'

module PactBroker
module Client
describe Matrix::AbbreviateVersionNumber do
describe '.call' do
subject(:result) { described_class.call(version) }

context 'when version is nil' do
let(:version) { nil }
it { is_expected.to be_nil }
end

context 'when version is git sha' do
let(:version) { '182f9c6e4d7a5779c4507cb8b3e505ac927d0394' }
it { is_expected.to eq('182f9c6...') }
end

context 'when version is too long' do
let(:version) { '182f9c6e4d7a5779c4507cb8b3e505ac927d0394' * 2 }
it { is_expected.to eq(version[0...60] + '...') }
end

context 'when the version is something unknown and fits max length' do
let(:version) { '123' }
it { is_expected.to eq('123') }
end

context 'when version is embedded into semantic version v1' do
let(:version) { 'v1.3.4+182f9c6e4d7a5779c4507cb8b3e505ac927d0394' }
it { is_expected.to eq('v1.3.4+182f9c6...') }
end

context 'when version is embedded into semantic version v2' do
let(:version) { '1.3.4(182f9c6e4d7a5779c4507cb8b3e505ac927d0394)' }
it { is_expected.to eq('1.3.4(182f9c6...)') }
end
end
end
end
end

0 comments on commit 86d21e1

Please sign in to comment.