Module: PactBroker::Matrix::Service

Extended by:
Service, PactBroker::Messages, Repositories, Services
Includes:
Logging
Included in:
Service
Defined in:
lib/pact_broker/matrix/service.rb

Constant Summary

Constants included from Repositories

Repositories::REPOSITORY_FACTORIES

Constants included from Services

Services::SERVICE_FACTORIES

Instance Method Summary collapse

Methods included from Repositories

branch_repository, branch_version_repository, get_repository, integration_repository, label_repository, matrix_repository, pact_repository, pacticipant_repository, register_default_repositories, register_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_service, 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 PactBroker::Messages

message, pluralize, validation_message, validation_message_at_index

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Instance Method Details

#can_i_deploy(selectors, options = {}) ⇒ Object



19
20
21
22
23
# File 'lib/pact_broker/matrix/service.rb', line 19

def can_i_deploy(selectors, options = {})
  # No point doing the deployment status summary if no versions are specified.
  query_results = find(selectors, options)
  QueryResultsWithDeploymentStatusSummary.new(query_results, DeploymentStatusSummary.new(query_results))
end

#can_i_merge(pacticipant_name: nil, pacticipant: nil, latest_main_branch_version: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pact_broker/matrix/service.rb', line 25

def can_i_merge(pacticipant_name: nil, pacticipant: nil, latest_main_branch_version: nil)
  # first we find the pacticipant by name (or use the one passed in) if pacticipant is nil
  if pacticipant.nil?
    pacticipant = pacticipant_service.find_pacticipant_by_name(pacticipant_name)
    raise PactBroker::Error.new("No pacticipant found with name '#{pacticipant_name}'") unless pacticipant
  else
    pacticipant_name = pacticipant.name
  end
   
  # then we find the latest version from the main branch if not passed in
  if latest_main_branch_version.nil?
    latest_main_branch_version = version_service.find_latest_version_from_main_branch(pacticipant)
    raise PactBroker::Error.new("No main branch version found for pacticipant '#{pacticipant_name}'") unless latest_main_branch_version
  end

  selectors = PactBroker::Matrix::UnresolvedSelector.from_hash(
    pacticipant_name: pacticipant_name, 
    pacticipant_version_number: latest_main_branch_version.number
  )
   
  options = { main_branch: true, latest: true, latestby: "cvp" }
  query_results = can_i_deploy([selectors], options)
   
  query_results.deployable?
end

#find(selectors, options = {}) ⇒ Object



51
52
53
54
# File 'lib/pact_broker/matrix/service.rb', line 51

def find selectors, options = {}
  logger.info "Querying matrix", selectors: selectors, options: options
  matrix_repository.find(selectors, options)
end

#find_for_consumer_and_provider(params, options = {}) ⇒ Object



56
57
58
59
# File 'lib/pact_broker/matrix/service.rb', line 56

def find_for_consumer_and_provider params, options = {}
  selectors = [ UnresolvedSelector.new(pacticipant_name: params[:consumer_name]), UnresolvedSelector.new(pacticipant_name: params[:provider_name]) ]
  can_i_deploy(selectors, options)
end

#find_for_consumer_and_provider_with_tags(params) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pact_broker/matrix/service.rb', line 61

def find_for_consumer_and_provider_with_tags params
  consumer_selector = UnresolvedSelector.new(
    pacticipant_name: params[:consumer_name],
    tag: params[:tag],
    latest: true
  )
  provider_selector = UnresolvedSelector.new(
    pacticipant_name: params[:provider_name],
    tag: params[:provider_tag],
    latest: true
  )
  selectors = [consumer_selector, provider_selector]
  options = { latestby: "cvpv" }
  if validate_selectors(selectors).empty?
    matrix_repository.find(selectors, options).first
  else
    nil
  end
end

#validate_options(options = {}) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/pact_broker/matrix/service.rb', line 112

def validate_options(options = {})
  error_messages = []

  options.fetch(:ignore_selectors, []).each do | s |
    if s[:pacticipant_name].nil?
      error_messages << "Please specify the pacticipant name to ignore"
    else
      if s.key?(:pacticipant_version_number) && s.key?(:latest)
        error_messages << "A version number and latest flag cannot both be specified for #{s[:pacticipant_name]} to ignore"
      end
    end
  end

  destination_identifiers = [options[:tag], options[:environment_name], options[:main_branch]&.to_s].compact

  if destination_identifiers.size > 1
    error_messages << message("errors.validation.cannot_specify_more_than_one_destination_identifier")
  end

  if options[:latest] && options[:environment_name]&.not_blank?
    error_messages << message("errors.validation.cannot_specify_latest_and_environment")
  end

  if options[:environment_name]&.not_blank? && environment_service.find_by_name(options[:environment_name]).nil?
    error_messages << message("errors.validation.environment_with_name_not_found", name: options[:environment_name])
  end

  if options[:limit] && options[:limit].to_i < 1
    error_messages << message("errors.validation.invalid_limit")
  end

  error_messages
end

#validate_selectors(selectors, options = {}) ⇒ Object

TODO create a proper contract for this rubocop: disable Metrics/CyclomaticComplexity



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pact_broker/matrix/service.rb', line 83

def validate_selectors selectors, options = {}
  error_messages = []

  selectors.each do | s |
    if s[:pacticipant_name].nil?
      error_messages << "Please specify the pacticipant name"
    else
      # TODO a bunch more validation
      if s.key?(:pacticipant_version_number) && s.key?(:latest)
        error_messages << "A version number and latest flag cannot both be specified for #{s[:pacticipant_name]}"
      end
    end
  end

  selectors.collect{ |selector| selector[:pacticipant_name] }.compact.each do | pacticipant_name |
    unless pacticipant_service.find_pacticipant_by_name(pacticipant_name)
      error_messages << "Pacticipant #{pacticipant_name} not found"
    end
  end

  if selectors.size == 0
    error_messages << "Please provide 1 or more version selectors."
  end

  error_messages + validate_options(options)
end