Module: PactBroker::Pacts::Service

Extended by:
Events::Publisher, Messages, Service, SquashPactsForVerification, Repositories, Services
Includes:
Logging
Included in:
Service
Defined in:
lib/pact_broker/pacts/service.rb

Constant Summary

Constants included from Services

Services::FACTORIES

Instance Method Summary collapse

Methods included from Repositories

branch_version_repository, label_repository, matrix_repository, pact_repository, pacticipant_repository, tag_repository, verification_repository, version_repository, webhook_repository

Methods included from Services

badge_service, branch_service, certificate_service, contract_service, deployed_version_service, environment_service, get, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, register_default_services, register_service, released_version_service, tag_service, verification_service, version_service, webhook_service, webhook_trigger_service

Methods included from Messages

message, pluralize, potential_duplicate_pacticipant_message, validation_message

Methods included from SquashPactsForVerification

call, squash_pacts_for_verification

Methods included from Logging

included, #log_error, #log_with_tag

Instance Method Details

#contract_published_event_comment(pact) ⇒ Object



267
268
269
270
271
272
273
# File 'lib/pact_broker/pacts/service.rb', line 267

def contract_published_event_comment pact
  if pact.consumer_version_tag_names.count == 1
    message("messages.events.pact_published_unchanged_with_single_tag", tag_name: pact.consumer_version_tag_names.first)
  else
    message("messages.events.pact_published_unchanged_with_multiple_tags", tag_names: pact.consumer_version_tag_names.join(", "))
  end
end

#create_or_update_pact(params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pact_broker/pacts/service.rb', line 52

def create_or_update_pact params
  provider = pacticipant_repository.find_by_name_or_create params[:provider_name]
  consumer = pacticipant_repository.find_by_name_or_create params[:consumer_name]
  consumer_version = version_repository.find_by_pacticipant_id_and_number_or_create consumer.id, params[:consumer_version_number]
  existing_pact = pact_repository.find_by_version_and_provider(consumer_version.id, provider.id)

  if existing_pact
    update_pact params, existing_pact
  else
    create_pact params, consumer_version, provider
  end
end

#delete(params) ⇒ Object



45
46
47
48
49
50
# File 'lib/pact_broker/pacts/service.rb', line 45

def delete params
  logger.info "Deleting pact version with params #{params}"
  pacts = pact_repository.find_all_revisions(params[:consumer_name], params[:consumer_version_number], params[:provider_name])
  webhook_service.delete_all_webhook_related_objects_by_pact_publication_ids(pacts.collect(&:id))
  pact_repository.delete(params)
end

#delete_all_pact_publications_between(consumer, options) ⇒ Object



82
83
84
# File 'lib/pact_broker/pacts/service.rb', line 82

def delete_all_pact_publications_between consumer, options
  pact_repository.delete_all_pact_publications_between consumer, options
end

#delete_all_pact_versions_between(consumer, options) ⇒ Object



86
87
88
# File 'lib/pact_broker/pacts/service.rb', line 86

def delete_all_pact_versions_between consumer, options
  pact_repository.delete_all_pact_versions_between consumer, options
end

#disallowed_modification?(existing_pact, new_json_content) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/pact_broker/pacts/service.rb', line 180

def disallowed_modification?(existing_pact, new_json_content)
  !PactBroker.configuration.allow_dangerous_contract_modification && existing_pact && existing_pact.pact_version_sha != generate_sha(new_json_content)
end

#find_all_pact_versions_between(consumer, options) ⇒ Object



78
79
80
# File 'lib/pact_broker/pacts/service.rb', line 78

def find_all_pact_versions_between consumer, options
  pact_repository.find_all_pact_versions_between consumer, options
end

#find_by_consumer_version(params) ⇒ Object



41
42
43
# File 'lib/pact_broker/pacts/service.rb', line 41

def find_by_consumer_version params
  pact_repository.find_by_consumer_version(params[:consumer_name], params[:consumer_version_number])
end

#find_distinct_pacts_between(consumer, options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pact_broker/pacts/service.rb', line 104

def find_distinct_pacts_between consumer, options
  # Assumes pacts are sorted from newest to oldest
  all = pact_repository.find_all_pact_versions_between consumer, options
  distinct = []
  (0...all.size).each do | i |
    if i == all.size - 1
      distinct << all[i]
    else
      if all[i].json_content != all[i+1].json_content
        distinct << all[i]
      end
    end
  end
  distinct
end

#find_for_verification(provider_name, provider_version_branch, provider_version_tags, consumer_version_selectors, options) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pact_broker/pacts/service.rb', line 120

def find_for_verification(provider_name, provider_version_branch, provider_version_tags, consumer_version_selectors, options)
  explicitly_specified_verifiable_pacts = pact_repository
    .find_for_verification(provider_name, consumer_version_selectors)
    .collect do | selected_pact |
      # Todo move this into the repository
      squash_pacts_for_verification(provider_version_tags, selected_pact, options[:include_pending_status])
    end

  verifiable_wip_pacts =  if options[:include_wip_pacts_since]
                            specified_pact_version_shas = explicitly_specified_verifiable_pacts.collect(&:pact_version_sha)
                            pact_repository.find_wip_pact_versions_for_provider(provider_name, provider_version_branch, provider_version_tags, specified_pact_version_shas, options)
                          else
                            []
                          end

  explicitly_specified_verifiable_pacts + verifiable_wip_pacts
end

#find_for_verification_publication(pact_params, consumer_version_selector_hashes) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pact_broker/pacts/service.rb', line 138

def find_for_verification_publication(pact_params, consumer_version_selector_hashes)
  if consumer_version_selector_hashes&.any?
    selected_pacts = consumer_version_selector_hashes.collect do | consumer_version_selector_hash |
      find_pact_params = {
        consumer_name: pact_params.consumer_name,
        provider_name: pact_params.provider_name,
        pact_version_sha: pact_params.pact_version_sha,
        consumer_version_number: consumer_version_selector_hash[:consumer_version_number]
      }
      pact = find_pact(find_pact_params)
      resolved_selector = Selector.new(consumer_version_selector_hash.without(:consumer_version_number)).resolve(pact.consumer_version)
      SelectedPact.new(pact, Selectors.new(resolved_selector))
    end
    SelectedPact.merge_by_consumer_version_number(selected_pacts)
  else
    [SelectedPact.new(find_pact(pact_params), Selectors.new)]
  end
end

#find_latest_pact(params) ⇒ Object



25
26
27
# File 'lib/pact_broker/pacts/service.rb', line 25

def find_latest_pact params
  pact_repository.find_latest_pact(params[:consumer_name], params[:provider_name], params[:tag])
end

#find_latest_pactsObject



33
34
35
# File 'lib/pact_broker/pacts/service.rb', line 33

def find_latest_pacts
  pact_repository.find_latest_pacts
end

#find_latest_pacts_for_provider(provider_name, options = {}) ⇒ Object



90
91
92
# File 'lib/pact_broker/pacts/service.rb', line 90

def find_latest_pacts_for_provider provider_name, options = {}
  pact_repository.find_latest_pacts_for_provider provider_name, options[:tag]
end

#find_pact(params) ⇒ Object



37
38
39
# File 'lib/pact_broker/pacts/service.rb', line 37

def find_pact params
  pact_repository.find_pact(params[:consumer_name], params[:consumer_version_number], params[:provider_name], params[:pact_version_sha])
end

#find_pact_versions_for_provider(provider_name, options = {}) ⇒ Object



94
95
96
# File 'lib/pact_broker/pacts/service.rb', line 94

def find_pact_versions_for_provider provider_name, options = {}
  pact_repository.find_pact_versions_for_provider provider_name, options[:tag]
end

#find_previous_distinct_pact_version(params) ⇒ Object



98
99
100
101
102
# File 'lib/pact_broker/pacts/service.rb', line 98

def find_previous_distinct_pact_version params
  pact = find_pact params
  return nil if pact.nil?
  pact_repository.find_previous_distinct_pact pact
end

#merge_pact(params) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pact_broker/pacts/service.rb', line 65

def merge_pact params
  provider = pacticipant_repository.find_by_name_or_create params[:provider_name]
  consumer = pacticipant_repository.find_by_name_or_create params[:consumer_name]
  consumer_version = version_repository.find_by_pacticipant_id_and_number_or_create consumer.id, params[:consumer_version_number]
  existing_pact = pact_repository.find_by_version_and_provider(consumer_version.id, provider.id)

  params[:json_content] = Merger.merge_pacts(
    existing_pact.json_content, params[:json_content]
  )

  update_pact params, existing_pact
end

#search_for_latest_pact(params) ⇒ Object



29
30
31
# File 'lib/pact_broker/pacts/service.rb', line 29

def search_for_latest_pact params
  pact_repository.search_for_latest_pact(params[:consumer_name], params[:provider_name], params[:tag])
end