# Parses YAML files and checks the shape of GitHub issue forms, so a broken workflow, compose file or issue form fails # CI instead of being skipped by GitHub (an issue form that does not parse is simply not offered). # Usage: ruby .github/scripts/check-yaml.rb FILE... require "yaml" def issue_form_problem(doc) return "the form is not a mapping" unless doc.is_a?(Hash) return "the form needs a name and a description" unless doc["name"].is_a?(String) && doc["description"].is_a?(String) return "the form needs a body list" unless doc["body"].is_a?(Array) && !doc["body"].empty? ids = [] doc["body"].each_with_index do |item, i| return "body item #{i + 1} has no type" unless item.is_a?(Hash) && item["type"].is_a?(String) attributes = item["attributes"] return "body item #{i + 1} has no attributes" unless attributes.is_a?(Hash) if item["type"] == "markdown" return "markdown item #{i + 1} has no value" unless attributes["value"].is_a?(String) else return "body item #{i + 1} has no label" unless attributes["label"].is_a?(String) ids << item["id"] if item["id"] end end return "duplicate ids #{ids.tally.select { |_, n| n > 1 }.keys.join(', ')}" if ids.uniq.size != ids.size nil end failures = 0 ARGV.each do |path| doc = YAML.safe_load(File.read(path), aliases: true, filename: path) if File.basename(File.dirname(path)) == "ISSUE_TEMPLATE" && File.basename(path) != "config.yml" problem = issue_form_problem(doc) raise problem if problem end puts "ok #{path}" rescue StandardError => e puts "::error file=#{path},title=YAML::#{e.message.lines.first.to_s.strip}" failures += 1 end exit(failures.zero? ? 0 : 1)