Module: PactBroker::Pacts::Metadata

Extended by:
Metadata
Included in:
Api::Decorators::VerifiablePactDecorator, Api::PactBrokerUrls, Metadata
Defined in:
lib/pact_broker/pacts/metadata.rb

Constant Summary collapse

MAPPINGS =
[
  [:consumer_version_tags, "cvt"],
  [:consumer_version_number, "cvn"], # for old urls and build_metadata_for_consumer_version_number
  [:consumer_version_id, "cv"],
  [:wip, "w"],
  [:pending, "p"],
  [:consumer_version_selectors, "s"],
  [:tag, "t"],
  [:branch, "b"],
  [:environment, "e"],
  [:latest, "l"]
]

Instance Method Summary collapse

Instance Method Details

#build_metadata_for_consumer_version_number(consumer_version_number) ⇒ Object



37
38
39
40
41
# File 'lib/pact_broker/pacts/metadata.rb', line 37

def (consumer_version_number)
  {
    "cvn" => consumer_version_number
  }
end

#build_metadata_for_latest_pact(pact, selection_parameters) ⇒ Object

When verifying a pact at /…/latest/TAG, this stores the tag and the current consumer version number in the metadata parameter of the URL for publishing the verification results. This is part of ensuring that verification results webhooks go back to the correct consumer version number (eg for git statuses)



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pact_broker/pacts/metadata.rb', line 24

def (pact, selection_parameters)
  if selection_parameters[:tag]
    {
      "cvt" => [selection_parameters[:tag]],
      "cv" => pact.consumer_version.id
    }
  else
    {
      "cv" => pact.consumer_version.id
    }
  end
end

#build_metadata_for_pact_for_verification(verifiable_pact) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pact_broker/pacts/metadata.rb', line 58

def (verifiable_pact)
  # todo put in tags
  if verifiable_pact.wip
    {
      "w" => true
    }
  else
    {
      "s" => verifiable_pact.selectors.collect do | selector |
        {
          "b" => selector.branch,
          "t" => selector.tag,
          "l" => selector.latest,
          "e" => selector.environment_name,
          "cv" => selector.consumer_version.id
        }.compact
      end,
      "p" => verifiable_pact.pending?
    }.compact
  end
end

#build_metadata_for_webhook_triggered_by_pact_publication(pact) ⇒ Object

When a pact is published, and a webhook is triggered, this stores the current tags and consumer version number in the metadata parameter of the pact version URL that is made available in the webhook template parameters. This is part of ensuring that verification results webhooks go back to the correct consumer version number (eg for git statuses)



48
49
50
51
52
53
54
55
56
# File 'lib/pact_broker/pacts/metadata.rb', line 48

def (pact)
  # Should probably put the branch in here, but I don't think the tags are used for anything
   = {
    "cvn" => pact.consumer_version_number,
    "cvt" => pact.consumer_version_tag_names
  }
  ["w"] = "true"
  
end

#parse_hash(hash) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pact_broker/pacts/metadata.rb', line 92

def parse_hash(hash)
  require "pact_broker/domain/version" # can't load this before the db models
  hash.each_with_object({}) do | (key, value), new_hash |
    long_key = MAPPINGS.find{ |mapping| mapping.last == key }&.first
    if long_key == :consumer_version_id
      new_hash[:consumer_version_number] = PactBroker::Domain::Version.find(id: value.to_i)&.number
    else
      new_hash[long_key || key] = parse_object(value)
    end
  end
end

#parse_metadata(metadata) ⇒ Object



80
81
82
# File 'lib/pact_broker/pacts/metadata.rb', line 80

def ()
  parse_object()
end

#parse_object(object) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/pact_broker/pacts/metadata.rb', line 84

def parse_object(object)
  case object
  when Hash then parse_hash(object)
  when Array then object.collect{|i| parse_object(i) }
  else object
  end
end