Sha256: 17fb1f62f08c2015ff3e83cf0b8f3ba786e5a7e1a59c7217d8a02bcdee157daa

Contents?: true

Size: 1.32 KB

Versions: 38

Compression:

Stored size: 1.32 KB

Contents

module PactBroker
  module Client
    class Matrix
      class AbbreviateVersionNumber
        # 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| regex_match?(r, version_number) }

            return replace_all_git_sha(version_number) if regex_match?(Regexp.new("\\A#{SHA1_REGEX.source}\\z"), 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

          # To support ruby2.2
          def regex_match?(regex, value)
            !regex.match(value).nil?
          end

          def replace_all_git_sha(version)
            version.gsub(SHA1_REGEX) { |val| val[0...7] + '...' }
          end
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
pact_broker-client-1.77.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.76.2 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.76.1 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.76.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.75.4 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.75.3 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.75.1 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.75.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.74.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.73.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.72.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.71.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.70.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.69.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.68.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.67.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.66.1 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.66.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.65.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb
pact_broker-client-1.64.0 lib/pact_broker/client/matrix/abbreviate_version_number.rb