#!/usr/bin/env ruby
# frozen_string_literal: true

require 'json'

missing_issues = []
missing_issue_message = <<~MSG
  The following quarantined tests are missing issue links or types or are formatted incorrectly:

  %s
  Please ensure that every quarantined test has
  `quarantine: { issue: "https://gitlab.com/gitlab-org/gitlab/-/issues/<ISSUE_NUMBER>", type: "<TYPE>" }`
  metadata tags with a valid issue link and type.
MSG

test_metadata_file = ARGV.shift

unless test_metadata_file
  puts "usage: #{__FILE__} <test_metadata_file>"
  exit 1
end

file = File.read(test_metadata_file)
data_hash = JSON.parse(file)

unless data_hash['examples'].count > 1
  puts "\nRspec output does not contain examples. Check test-metadata.json file.\n"
  exit 1
end

puts "\nAnalyzing quarantined test data...\n"

tests = data_hash['examples']

tests.each do |test|
  next unless test['quarantine']

  unless test['quarantine'].is_a?(Hash) && test['quarantine'].key?('issue') && test['quarantine'].key?('type')
    missing_issues.push(" ==> #{test['id']} - #{test['full_description']}\n")
  end
end

if missing_issues.empty?
  puts "\nNo errors found."
else
  puts "\e[31m"
  puts "*** Quarantine format violations detected! ***\n\n"

  puts missing_issue_message % missing_issues.join unless missing_issues.empty?
  puts "\nSee https://handbook.gitlab.com/handbook/engineering/testing/pipeline-triage/#long-term-quarantine"
  puts "\e[0m"
  exit 1
end
