2022-04-04 11:22:00 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
namespace :tw do
|
|
|
|
desc 'Generates a list of codeowners for documentation pages.'
|
|
|
|
task :codeowners do
|
|
|
|
CodeOwnerRule = Struct.new(:category, :writer)
|
2022-07-23 23:45:48 +05:30
|
|
|
DocumentOwnerMapping = Struct.new(:path, :writer) do
|
2022-11-25 23:54:43 +05:30
|
|
|
def writer_owns_directory?(mappings)
|
|
|
|
dir_mappings = mappings.select { |mapping| mapping.directory == directory }
|
|
|
|
|
|
|
|
dir_mappings.count { |mapping| mapping.writer == writer } / dir_mappings.length.to_f > 0.5
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def directory
|
|
|
|
@directory ||= File.dirname(path)
|
|
|
|
end
|
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
CODE_OWNER_RULES = [
|
2022-10-11 01:57:18 +05:30
|
|
|
CodeOwnerRule.new('Activation', '@phillipwells'),
|
|
|
|
CodeOwnerRule.new('Acquisition', '@phillipwells'),
|
|
|
|
CodeOwnerRule.new('Anti-Abuse', '@phillipwells'),
|
2022-11-25 23:54:43 +05:30
|
|
|
CodeOwnerRule.new('Authentication and Authorization', '@jglassman1'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Certify', '@msedlakjakubowski'),
|
|
|
|
CodeOwnerRule.new('Code Review', '@aqualls'),
|
|
|
|
CodeOwnerRule.new('Compliance', '@eread'),
|
|
|
|
CodeOwnerRule.new('Composition Analysis', '@rdickenson'),
|
2022-10-11 01:57:18 +05:30
|
|
|
CodeOwnerRule.new('Configure', '@phillipwells'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Container Registry', '@claytoncornell'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Contributor Experience', '@eread'),
|
|
|
|
CodeOwnerRule.new('Conversion', '@kpaizee'),
|
2022-07-16 23:28:13 +05:30
|
|
|
CodeOwnerRule.new('Database', '@aqualls'),
|
|
|
|
CodeOwnerRule.new('Development', '@sselhorn'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Distribution', '@axil'),
|
|
|
|
CodeOwnerRule.new('Distribution (Charts)', '@axil'),
|
|
|
|
CodeOwnerRule.new('Distribution (Omnibus)', '@axil'),
|
2022-07-16 23:28:13 +05:30
|
|
|
CodeOwnerRule.new('Documentation Guidelines', '@sselhorn'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Dynamic Analysis', '@rdickenson'),
|
2022-11-25 23:54:43 +05:30
|
|
|
CodeOwnerRule.new('Editor', '@ashrafkhamis'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Foundations', '@rdickenson'),
|
|
|
|
CodeOwnerRule.new('Fuzz Testing', '@rdickenson'),
|
|
|
|
CodeOwnerRule.new('Geo', '@axil'),
|
|
|
|
CodeOwnerRule.new('Gitaly', '@eread'),
|
2022-08-27 11:52:29 +05:30
|
|
|
CodeOwnerRule.new('Global Search', '@ashrafkhamis'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Import', '@eread'),
|
2022-07-16 23:28:13 +05:30
|
|
|
CodeOwnerRule.new('Infrastructure', '@sselhorn'),
|
2022-08-27 11:52:29 +05:30
|
|
|
CodeOwnerRule.new('Integrations', '@ashrafkhamis'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Knowledge', '@aqualls'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Application Performance', '@jglassman1'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Monitor', '@msedlakjakubowski'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Observability', '@msedlakjakubowski'),
|
|
|
|
CodeOwnerRule.new('Optimize', '@lciutacu'),
|
|
|
|
CodeOwnerRule.new('Package Registry', '@claytoncornell'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Pipeline Authoring', '@marcel.amirault'),
|
|
|
|
CodeOwnerRule.new('Pipeline Execution', '@marcel.amirault'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Pipeline Insights', '@marcel.amirault'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Portfolio Management', '@msedlakjakubowski'),
|
2022-11-25 23:54:43 +05:30
|
|
|
CodeOwnerRule.new('Product Analytics', '@lciutacu'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Product Intelligence', '@claytoncornell'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Product Planning', '@msedlakjakubowski'),
|
|
|
|
CodeOwnerRule.new('Project Management', '@msedlakjakubowski'),
|
2022-07-16 23:28:13 +05:30
|
|
|
CodeOwnerRule.new('Provision', '@fneill'),
|
|
|
|
CodeOwnerRule.new('Purchase', '@fneill'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Redirect', 'Redirect'),
|
|
|
|
CodeOwnerRule.new('Release', '@rdickenson'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Respond', '@msedlakjakubowski'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Runner', '@fneill'),
|
|
|
|
CodeOwnerRule.new('Runner SaaS', '@fneill'),
|
|
|
|
CodeOwnerRule.new('Pods', '@jglassman1'),
|
2022-11-25 23:54:43 +05:30
|
|
|
CodeOwnerRule.new('Security Policies', '@claytoncornell'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Source Code', '@aqualls'),
|
|
|
|
CodeOwnerRule.new('Static Analysis', '@rdickenson'),
|
|
|
|
CodeOwnerRule.new('Style Guide', '@sselhorn'),
|
|
|
|
CodeOwnerRule.new('Testing', '@eread'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Threat Insights', '@claytoncornell'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Tutorials', '@kpaizee'),
|
2022-07-16 23:28:13 +05:30
|
|
|
CodeOwnerRule.new('Utilization', '@fneill'),
|
2022-06-21 17:19:12 +05:30
|
|
|
CodeOwnerRule.new('Vulnerability Research', '@claytoncornell'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Workspace', '@lciutacu')
|
2022-04-04 11:22:00 +05:30
|
|
|
].freeze
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
CODEOWNERS_BLOCK_BEGIN = "# Begin rake-managed-docs-block"
|
|
|
|
CODEOWNERS_BLOCK_END = "# End rake-managed-docs-block"
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
Document = Struct.new(:group, :redirect) do
|
|
|
|
def has_a_valid_group?
|
|
|
|
group && !redirect
|
|
|
|
end
|
|
|
|
|
|
|
|
def missing_metadata?
|
|
|
|
!group && !redirect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.writer_for_group(category)
|
|
|
|
CODE_OWNER_RULES.find { |rule| rule.category == category }&.writer
|
|
|
|
end
|
|
|
|
|
|
|
|
errors = []
|
2022-07-23 23:45:48 +05:30
|
|
|
mappings = []
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
path = Rails.root.join("doc/**/*.md")
|
|
|
|
Dir.glob(path) do |file|
|
|
|
|
yaml_data = YAML.load_file(file)
|
|
|
|
document = Document.new(yaml_data['group'], yaml_data['redirect_to'])
|
|
|
|
|
|
|
|
if document.missing_metadata?
|
|
|
|
errors << file
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
writer = writer_for_group(document.group)
|
|
|
|
next unless writer
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
mappings << DocumentOwnerMapping.new(file.delete_prefix(Dir.pwd), writer) if document.has_a_valid_group?
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
deduplicated_mappings = Set.new
|
|
|
|
|
|
|
|
mappings.each do |mapping|
|
2022-11-25 23:54:43 +05:30
|
|
|
if mapping.writer_owns_directory?(mappings)
|
2022-07-23 23:45:48 +05:30
|
|
|
deduplicated_mappings.add("#{mapping.directory}/ #{mapping.writer}")
|
|
|
|
else
|
|
|
|
deduplicated_mappings.add("#{mapping.path} #{mapping.writer}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
new_docs_owners = deduplicated_mappings.sort.join("\n")
|
|
|
|
|
|
|
|
codeowners_path = Rails.root.join('.gitlab/CODEOWNERS')
|
|
|
|
current_codeowners_content = File.read(codeowners_path)
|
|
|
|
|
|
|
|
docs_replace_regex = Regexp.new("#{CODEOWNERS_BLOCK_BEGIN}\n[\\s\\S]*?\n#{CODEOWNERS_BLOCK_END}")
|
|
|
|
|
|
|
|
new_codeowners_content = current_codeowners_content
|
|
|
|
.gsub(docs_replace_regex, "#{CODEOWNERS_BLOCK_BEGIN}\n#{new_docs_owners}\n#{CODEOWNERS_BLOCK_END}")
|
|
|
|
|
|
|
|
File.write(codeowners_path, new_codeowners_content)
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
if errors.present?
|
|
|
|
puts "-----"
|
|
|
|
puts "ERRORS - the following files are missing the correct metadata:"
|
2022-08-27 11:52:29 +05:30
|
|
|
errors.map { |file| puts file.gsub(Dir.pwd, ".") }
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|