Class: PactBroker::Index::Service

Inherits:
Object
  • Object
show all
Extended by:
Logging, Repositories, Services
Defined in:
lib/pact_broker/index/service.rb

Constant Summary

Constants included from Logging

Logging::LOG_DIR, Logging::LOG_FILE_NAME

Class Method Summary collapse

Methods included from Repositories

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

Methods included from Services

badge_service, certificate_service, group_service, index_service, label_service, matrix_service, pact_service, pacticipant_service, tag_service, verification_service, version_service, webhook_service

Methods included from Logging

included, log_error, logger, logger=

Class Method Details

.build_index_item_for_tagged_pact(latest_pact, tag) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/pact_broker/index/service.rb', line 112

def self.build_index_item_for_tagged_pact latest_pact, tag
  pact = pact_service.find_latest_pact consumer_name: latest_pact.consumer_name, provider_name: latest_pact.provider_name, tag: tag
  return nil unless pact
  return nil if pact.id == latest_pact.id
  verification = verification_repository.find_latest_verification_for pact.consumer_name, pact.provider_name, tag
  PactBroker::Domain::IndexItem.create pact.consumer, pact.provider, pact, false, verification, [], [], [tag]
end

.build_index_item_rows(pact, tags) ⇒ Object



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

def self.build_index_item_rows(pact, tags)
  index_items = [build_latest_pact_index_item(pact, tags)]
  tags.each do | tag |
    index_items << build_index_item_for_tagged_pact(pact, tag)
  end
  index_items.compact
end

.build_latest_pact_index_item(pact, tags) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/pact_broker/index/service.rb', line 104

def self.build_latest_pact_index_item pact, tags
  latest_verification = verification_service.find_latest_verification_for(pact.consumer, pact.provider)
  webhooks = webhook_service.find_by_consumer_and_provider pact.consumer, pact.provider
  triggered_webhooks = webhook_service.find_latest_triggered_webhooks pact.consumer, pact.provider
  tag_names = pact.consumer_version_tag_names.select{ |name| tags.include?(name) }
  PactBroker::Domain::IndexItem.create pact.consumer, pact.provider, pact, true, latest_verification, webhooks, triggered_webhooks, tag_names
end

.find_index_items(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pact_broker/index/service.rb', line 16

def self.find_index_items options = {}
  rows = []
  overall_latest_publication_ids = nil

  rows = PactBroker::Matrix::HeadRow
    .select_all_qualified
    .eager(:latest_triggered_webhooks)
    .eager(:webhooks)
    .order(:consumer_name, :provider_name)
    .eager(:consumer_version_tags)
    .eager(:provider_version_tags)

  if !options[:tags]
    rows = rows.where(consumer_version_tag_name: nil).all
    overall_latest_publication_ids = rows.collect(&:pact_publication_id)
  end

  if options[:tags]
    if options[:tags].is_a?(Array)
      rows = rows.where(consumer_version_tag_name: options[:tags]).or(consumer_version_tag_name: nil)
    end

    rows = rows.all
    overall_latest_publication_ids = rows.select{|r| !r[:consumer_version_tag_name] }.collect(&:pact_publication_id).uniq

    # Smoosh all the rows with matching pact publications together
    # and collect their consumer_head_tag_names
    rows = rows
      .group_by(&:pact_publication_id)
      .values
      .collect{|group| [group.last, group.collect{|r| r[:consumer_version_tag_name]}.compact] }
      .collect{ |(row, tag_names)| row.consumer_head_tag_names = tag_names; row }
  end

  index_items = []
  rows.sort.each do | row |
    tag_names = []
    if options[:tags]
      tag_names = row.consumer_version_tags.collect(&:name)
    end

    overall_latest = overall_latest_publication_ids.include?(row.pact_publication_id)
    latest_verification = if overall_latest
      verification_repository.find_latest_verification_for row.consumer_name, row.provider_name
    else
      tag_names.collect do | tag_name |
        verification_repository.find_latest_verification_for row.consumer_name, row.provider_name, tag_name
      end.compact.sort do | v1, v2 |
        # Some provider versions have nil orders, not sure why
        # Sort by execution_date instead of order
        v1.execution_date <=> v2.execution_date
      end.last
    end

    index_items << PactBroker::Domain::IndexItem.create(
      row.consumer,
      row.provider,
      row.pact,
      overall_latest,
      latest_verification,
      row.webhooks,
      row.latest_triggered_webhooks,
      row.consumer_head_tag_names,
      row.provider_version_tags.select(&:latest?)
    )
  end

  index_items
end

.tags_for(pact, options) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/pact_broker/index/service.rb', line 86

def self.tags_for(pact, options)
  if options[:tags] == true
    tag_service.find_all_tag_names_for_pacticipant(pact.consumer_name)
  elsif options[:tags].is_a?(Array)
    options[:tags]
  else
    []
  end
end