Module: RSpec::FlakeClassifier::Integrations::BuildkiteCollector

Defined in:
lib/rspec/flake/classifier/integrations.rb

Class Method Summary collapse

Class Method Details

.buildkite_trace(example) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/rspec/flake/classifier/integrations.rb', line 221

def buildkite_trace(example)
  collector = Integrations.constant("Buildkite::TestCollector")
  uploader = collector&.respond_to?(:uploader) ? collector.uploader : nil
  return nil unless uploader&.respond_to?(:traces)

  uploader.traces[example.id]
end

.flaky?(classification, labels) ⇒ Boolean

Returns:

  • (Boolean)


240
241
242
243
# File 'lib/rspec/flake/classifier/integrations.rb', line 240

def flaky?(classification, labels)
  hash = classification.respond_to?(:to_h) ? classification.to_h : classification
  hash&.fetch("status", nil) == "flaky" || labels.any?
end

.labels_for(classification) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/rspec/flake/classifier/integrations.rb', line 229

def labels_for(classification)
  hash = classification.respond_to?(:to_h) ? classification.to_h : classification
  Array(hash&.fetch("labels", nil)).map do |label|
    if label.respond_to?(:fetch)
      label.fetch("category", label.fetch(:category, label.to_s)).to_s
    else
      label.to_s
    end
  end
end

.tag_current_execution(tags) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/rspec/flake/classifier/integrations.rb', line 203

def tag_current_execution(tags)
  collector = Integrations.constant("Buildkite::TestCollector")
  return false unless collector&.respond_to?(:tag_execution)

  tags.each { |key, value| collector.tag_execution(key, value) }
  true
rescue StandardError
  false
end

.tag_execution(example:, signature:, classification:) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rspec/flake/classifier/integrations.rb', line 189

def tag_execution(example:, signature:, classification:)
  labels = labels_for(classification)
  tags = {
    "rspec_flake_classifier.flaky" => flaky?(classification, labels).to_s,
    "rspec_flake_classifier.labels" => labels.join(","),
    "rspec_flake_classifier.signature" => signature.digest.to_s
  }
  return if tag_current_execution(tags)

  tag_recorded_trace(example, tags)
rescue StandardError
  nil
end

.tag_recorded_trace(example, tags) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/rspec/flake/classifier/integrations.rb', line 213

def tag_recorded_trace(example, tags)
  trace = buildkite_trace(example)
  return unless trace&.respond_to?(:tags)
  return unless trace.tags.respond_to?(:[]=)

  tags.each { |key, value| trace.tags[key] = value }
end