文件
AiXun-DataGov/script/quality/verify-business-units.rb
T
2026-11-20 15:32:57 +08:00

168 行
8.5 KiB
Ruby
可执行文件

#!/usr/bin/env ruby
# frozen_string_literal: true
require "pathname"
require "yaml"
require "rexml/document"
require "rexml/xpath"
# 010 grants only these direct Governance test dependencies, not production coupling.
# Resource is not a business unit and remains outside the original four-unit guard.
module BusinessUnitApiDependencies
TEST_TARGETS = %w[dataops dataproduct resource].freeze
MAVEN_NAMESPACE = { "m" => "http://maven.apache.org/POM/4.0.0" }.freeze
def self.test_exception?(source_id, target_id, group_id, scope, direct)
source_id == "governance" && TEST_TARGETS.include?(target_id) &&
group_id == "cn.aixunkeji" && scope == "test" && direct
end
def self.violations(source_id, pom_xml, declared, expected_ids)
document = REXML::Document.new(pom_xml)
raise ArgumentError, "POM DOCTYPE is not allowed" if document.doctype
raise ArgumentError, "invalid Maven project namespace" unless
document.root&.name == "project" && document.root.namespace == MAVEN_NAMESPACE.fetch("m")
direct = REXML::XPath.match(document, "/m:project/m:dependencies/m:dependency", MAVEN_NAMESPACE)
profiles = REXML::XPath.match(document,
"/m:project/m:profiles/m:profile/m:dependencies/m:dependency", MAVEN_NAMESPACE)
(direct.map { |node| [node, true] } + profiles.map { |node| [node, false] }).map do |node, is_direct|
values = %w[groupId artifactId scope].to_h do |field|
elements = REXML::XPath.match(node, "m:#{field}", MAVEN_NAMESPACE)
raise ArgumentError, "duplicate POM dependency field" if elements.length > 1
[field, elements.first&.text&.strip]
end
artifact = values.fetch("artifactId")
target = artifact&.delete_prefix("aixun-api-")
next unless artifact == "aixun-api-#{target}" && (expected_ids - [source_id]).include?(target)
next if test_exception?(source_id, target, values.fetch("groupId"), values.fetch("scope"), is_direct)
next if declared.include?(target)
"#{source_id}->#{target} scope=#{values.fetch('scope') || 'compile(default)'}"
end.compact
end
end
if $PROGRAM_NAME == __FILE__
root = Pathname.new(__dir__).join("../..").realpath
registry_path = root.join("docs/architecture/business-unit-boundaries.yml")
registry = YAML.safe_load(registry_path.read)
units = registry.fetch("units")
expected_ids = %w[integration dataops governance dataproduct]
def fail_check(message)
warn "business-unit verification failed: #{message}"
exit 1
end
contract_paths = %w[m2-golden-chain.yml m5-governance.yml m6-business-extension.yml asset-upgrade.yml].map do |name|
root.join("contracts/datagov", name)
end
contracts = contract_paths.map do |path|
fail_check("missing milestone contract #{path}") unless path.file?
YAML.safe_load(path.read)
end
m2_contract = contracts.first
m6_contract = contracts.fetch(2)
upgrade_contract = contracts.fetch(3)
fail_check("unexpected private Resource API consumers") unless
upgrade_contract.fetch("resourceApiConsumers") == %w[dataproduct governance dataops]
allowed_api_dependencies = {}
contracts.each do |contract|
contract.fetch("crossUnitApiDependencies", {}).each do |source_id, target_ids|
fail_check("unknown contract source unit #{source_id}") unless expected_ids.include?(source_id)
Array(target_ids).each do |target_id|
fail_check("unknown contract target unit #{target_id}") unless expected_ids.include?(target_id)
fail_check("self dependency is forbidden for #{source_id}") if source_id == target_id
allowed_api_dependencies[source_id] ||= []
allowed_api_dependencies[source_id] << target_id unless allowed_api_dependencies[source_id].include?(target_id)
end
end
end
fail_check("registry must contain exactly four units") unless units.map { |unit| unit.fetch("id") } == expected_ids
fail_check("M0 must not enable the business services") unless registry.fetch("runtimeEnabledInM0") == false
fail_check("M0 must not claim external integration") unless registry.fetch("externalComponentsIntegrated") == false
%w[serviceName nacosDataId gatewayPath permissionNamespace packageRoot ownerRole].each do |field|
values = units.map { |unit| unit.fetch(field) }
fail_check("#{field} values must be unique") unless values.uniq.size == units.size
end
units.each do |unit|
id = unit.fetch("id")
module_path = root.join(unit.fetch("module"))
api_path = root.join(unit.fetch("apiModule"))
package_root = unit.fetch("packageRoot")
expected_service = "aixun-#{id}"
fail_check("missing module #{module_path}") unless module_path.directory?
fail_check("missing API module #{api_path}") unless api_path.directory?
fail_check("missing owner file for #{id}") unless module_path.join("OWNERS.md").file?
fail_check("unexpected service name for #{id}") unless unit.fetch("serviceName") == expected_service
fail_check("unexpected Nacos dataId for #{id}") unless unit.fetch("nacosDataId") == "#{expected_service}.yml"
fail_check("unexpected Gateway path for #{id}") unless unit.fetch("gatewayPath") == "/api/#{id}/**"
fail_check("unexpected permission namespace for #{id}") unless unit.fetch("permissionNamespace") == "aixun:#{id}:"
java_files = [module_path, api_path].flat_map { |path| path.glob("src/**/*.java") }
fail_check("#{id} has no Java skeleton") if java_files.empty?
java_files.each do |java_file|
package_line = java_file.each_line.find { |line| line.start_with?("package ") }
fail_check("missing package in #{java_file}") if package_line.nil?
fail_check("wrong package root in #{java_file}") unless package_line.include?(package_root)
end
application = YAML.safe_load(module_path.join("src/main/resources/application.yml").read)
app_name = application.dig("spring", "application", "name")
fail_check("application/Nacos name mismatch for #{id}") unless app_name == expected_service
module_text = module_path.glob("{pom.xml,src/main/**/*}").select(&:file?).map(&:read).join("\n")
fail_check("#{id} contains a Mapper in M0") if module_text.match?(/@Mapper\b|interface\s+\w+Mapper\b/)
fail_check("#{id} contains a database foreign key in M0") if module_text.match?(/\bFOREIGN\s+KEY\b/i)
begin
violations = BusinessUnitApiDependencies.violations(id, module_path.join("pom.xml").read,
Array(allowed_api_dependencies.fetch(id, [])), expected_ids)
fail_check("#{id} depends on another unit API without a declared milestone contract: #{violations.join(', ')}") unless
violations.empty?
# Resource remains outside the four business units. Only the three explicit direct production callers are approved.
xml = REXML::Document.new(module_path.join("pom.xml").read)
REXML::XPath.match(xml, "//m:dependency[m:artifactId='aixun-api-resource']", BusinessUnitApiDependencies::MAVEN_NAMESPACE).each do |node|
scope = node.elements["scope"]&.text || "compile"
group = node.elements["groupId"]&.text
direct = node.parent&.name == "dependencies" && node.parent.parent == xml.root
test_only = BusinessUnitApiDependencies.test_exception?(id, "resource", group, scope, direct)
approved = upgrade_contract.fetch("resourceApiConsumers").include?(id) && group == "cn.aixunkeji" && scope == "compile" && direct
fail_check("#{id} has unapproved Resource API dependency") unless test_only || approved
end
rescue REXML::ParseException, ArgumentError
fail_check("#{id} has invalid dependency XML")
end
end
module_names = root.join("aixun-modules").children.select(&:directory?).map { |path| path.basename.to_s }
fail_check("a fifth adapter deployment module exists") if module_names.any? { |name| name.match?(/adapter/i) }
compose_text = root.join("script/datagov/docker-compose.local.yml").read
enabled_units = m6_contract.dig("scope", "enabledUnits")
forbidden_units = m6_contract.dig("scope", "forbiddenUnits")
fail_check("M6 must statically enable exactly four business units") unless enabled_units == expected_ids
fail_check("M6 must not forbid an approved business unit") unless forbidden_units == []
enabled_units.each do |id|
service_name = units.find { |unit| unit.fetch("id") == id }.fetch("serviceName")
fail_check("#{service_name} must be enabled for the current milestone contract") unless
compose_text.include?(service_name)
end
forbidden_units.each do |id|
service_name = units.find { |unit| unit.fetch("id") == id }.fetch("serviceName")
fail_check("#{service_name} must remain disabled for the current milestone contract") if
compose_text.include?(service_name)
end
puts "Business-unit verification passed: 4 units, declared cross-unit APIs only, M6 static activation includes dataproduct, no fifth adapter service."
end