Class: PactBroker::Webhooks::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/webhooks/render.rb

Constant Summary collapse

TEMPLATE_PARAMETER_REGEXP =
/\$\{pactbroker\.[^\}]+\}/

Class Method Summary collapse

Class Method Details

.call(template, pact, trigger_verification = nil, &escaper) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pact_broker/webhooks/render.rb', line 7

def self.call(template, pact, trigger_verification = nil, &escaper)
  base_url = PactBroker.configuration.base_url
  verification = trigger_verification || (pact && pact.latest_verification)
  params = {
    '${pactbroker.pactUrl}' => pact ? PactBroker::Api::PactBrokerUrls.pact_url(base_url, pact) : "",
    '${pactbroker.verificationResultUrl}' => verification_url(verification),
    '${pactbroker.consumerVersionNumber}' => pact ? pact.consumer_version_number : "",
    '${pactbroker.providerVersionNumber}' => verification ? verification.provider_version_number : "",
    '${pactbroker.providerVersionTags}' => provider_version_tags(verification),
    '${pactbroker.consumerVersionTags}' => consumer_version_tags(pact),
    '${pactbroker.consumerName}' => pact ? pact.consumer_name : "",
    '${pactbroker.providerName}' => pact ? pact.provider_name : "",
    '${pactbroker.githubVerificationStatus}' => github_verification_status(verification),
    '${pactbroker.consumerLabels}' => pacticipant_labels(pact && pact.consumer),
    '${pactbroker.providerLabels}' => pacticipant_labels(pact && pact.provider)
  }

  if escaper
    params.keys.each do | key |
      params[key] = escaper.call(params[key])
    end
  end

  params.inject(template) do | template, (key, value) |
    template.gsub(key, value)
  end
end

.consumer_version_tags(pact) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/pact_broker/webhooks/render.rb', line 51

def self.consumer_version_tags pact
  if pact
    pact.consumer_version.tags.collect(&:name).join(", ")
  else
    ""
  end
end

.github_verification_status(verification) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/pact_broker/webhooks/render.rb', line 35

def self.github_verification_status verification
  if verification
    verification.success ? "success" : "failure"
  else
    "pending"
  end
end

.pacticipant_labels(pacticipant) ⇒ Object



67
68
69
# File 'lib/pact_broker/webhooks/render.rb', line 67

def self.pacticipant_labels pacticipant
  pacticipant && pacticipant.labels ? pacticipant.labels.collect(&:name).join(", ") : ""
end

.provider_version_tags(verification) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/pact_broker/webhooks/render.rb', line 59

def self.provider_version_tags verification
  if verification
    verification.provider_version.tags.collect(&:name).join(", ")
  else
    ""
  end
end

.verification_url(verification) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pact_broker/webhooks/render.rb', line 43

def self.verification_url verification
  if verification
    PactBroker::Api::PactBrokerUrls.verification_url(verification, PactBroker.configuration.base_url)
  else
    ""
  end
end