From 8601aacd752775afcc4eb84c330a71c8d10a00bf Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Sat, 2 Mar 2024 23:07:30 -0500 Subject: [PATCH] tap_auditor: validate pypi_formula_mappings.json Signed-off-by: Michael Cho --- Library/Homebrew/Gemfile | 6 ++--- .../schemas/pypi_formula_mappings.schema.json | 25 +++++++++++++++++++ Library/Homebrew/tap_auditor.rb | 16 ++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 Library/Homebrew/data/schemas/pypi_formula_mappings.schema.json diff --git a/Library/Homebrew/Gemfile b/Library/Homebrew/Gemfile index 01d1fb91c4a5bf..aa2f1391ce680f 100644 --- a/Library/Homebrew/Gemfile +++ b/Library/Homebrew/Gemfile @@ -30,9 +30,6 @@ end group :man, optional: true do gem "ronn", require: false end -group :pr_upload, optional: true do - gem "json_schemer", require: false -end group :prof, optional: true do gem "ruby-prof", require: false gem "stackprof", require: false @@ -71,6 +68,9 @@ end group :audit, :bump_unversioned_casks, :livecheck, optional: true do gem "rexml", require: false end +group :audit, :pr_upload, optional: true do + gem "json_schemer", require: false +end # vendored gems (no group) gem "addressable" diff --git a/Library/Homebrew/data/schemas/pypi_formula_mappings.schema.json b/Library/Homebrew/data/schemas/pypi_formula_mappings.schema.json new file mode 100644 index 00000000000000..c8fb9e0a1b9452 --- /dev/null +++ b/Library/Homebrew/data/schemas/pypi_formula_mappings.schema.json @@ -0,0 +1,25 @@ +{ + "type": "object", + "additionalProperties": { + "type": ["string", "object"], + "properties": { + "package_name": {"type": "string"}, + "exclude_packages": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1 + }, + "extra_packages": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1 + }, + "dependencies": { + "type": "array", + "items": {"type": "string"}, + "minItems": 1 + } + }, + "additionalProperties": false + } +} diff --git a/Library/Homebrew/tap_auditor.rb b/Library/Homebrew/tap_auditor.rb index 2910236abd93dc..13f1c6100009ef 100644 --- a/Library/Homebrew/tap_auditor.rb +++ b/Library/Homebrew/tap_auditor.rb @@ -45,6 +45,15 @@ def audit_json_files rescue JSON::ParserError problem "#{file.to_s.delete_prefix("#{@path}/")} contains invalid JSON" end + + require "json_schemer" + schemer = JSONSchemer.schema(HOMEBREW_DATA_PATH/"schemas/pypi_formula_mappings.schema.json") + return if schemer.valid?(@tap_pypi_formula_mappings) + + problem <<~EOS + pypi_formula_mappings.json schema validation failed with following errors: + * #{schemer.validate(@tap_pypi_formula_mappings).map { _1.fetch("error") }.join("\n* ")} + EOS end sig { void } @@ -52,6 +61,13 @@ def audit_tap_formula_lists check_formula_list_directory "audit_exceptions", @tap_audit_exceptions check_formula_list_directory "style_exceptions", @tap_style_exceptions check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings + + @tap_pypi_formula_mappings.each_value do |formula_mapping| + next unless formula_mapping.is_a?(Hash) + next unless formula_mapping.key?("dependencies") + + check_formula_list "pypi_formula_mappings", formula_mapping["dependencies"] + end end sig { void }