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
|
2023-04-23 21:23:45 +05:30
|
|
|
@directory ||= "#{File.dirname(path)}/"
|
2022-07-23 23:45:48 +05:30
|
|
|
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'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Commerce Integrations', '@drcatherinepope'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Composition Analysis', '@rdickenson'),
|
2022-10-11 01:57:18 +05:30
|
|
|
CodeOwnerRule.new('Configure', '@phillipwells'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Container Registry', '@dianalogan'),
|
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-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Observability', '@drcatherinepope'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Optimize', '@lciutacu'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Package Registry', '@dianalogan'),
|
2022-04-04 11:22:00 +05:30
|
|
|
CodeOwnerRule.new('Pipeline Authoring', '@marcel.amirault'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Pipeline Execution', '@drcatherinepope'),
|
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'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Product Intelligence', '@dianalogan'),
|
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'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Security Policies', '@dianalogan'),
|
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'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Threat Insights', '@dianalogan'),
|
2023-01-13 00:05:48 +05:30
|
|
|
CodeOwnerRule.new('Tutorials', '@kpaizee'),
|
2022-07-16 23:28:13 +05:30
|
|
|
CodeOwnerRule.new('Utilization', '@fneill'),
|
2023-04-23 21:23:45 +05:30
|
|
|
CodeOwnerRule.new('Vulnerability Research', '@dianalogan'),
|
2023-03-17 16:20:25 +05:30
|
|
|
CodeOwnerRule.new('Organization', '@lciutacu')
|
2022-04-04 11:22:00 +05:30
|
|
|
].freeze
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
ERRORS_EXCLUDED_FILES = [
|
|
|
|
'/doc/architecture'
|
|
|
|
].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'])
|
2023-03-04 22:38:38 +05:30
|
|
|
relative_file = file.delete_prefix(Dir.pwd)
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
if document.missing_metadata?
|
2023-03-04 22:38:38 +05:30
|
|
|
errors << relative_file unless ERRORS_EXCLUDED_FILES.any? { |element| relative_file.starts_with?(element) }
|
2022-04-04 11:22:00 +05:30
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
writer = writer_for_group(document.group)
|
|
|
|
next unless writer
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
mappings << DocumentOwnerMapping.new(relative_file, writer) if document.has_a_valid_group?
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
transformed_mappings = mappings.map do |mapping|
|
2022-11-25 23:54:43 +05:30
|
|
|
if mapping.writer_owns_directory?(mappings)
|
2023-04-23 21:23:45 +05:30
|
|
|
DocumentOwnerMapping.new(mapping.directory, mapping.writer)
|
2022-07-23 23:45:48 +05:30
|
|
|
else
|
2023-04-23 21:23:45 +05:30
|
|
|
DocumentOwnerMapping.new(mapping.path, mapping.writer)
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
deduplicated_mappings = Set.new
|
|
|
|
|
|
|
|
transformed_mappings
|
|
|
|
.reject { |mapping| transformed_mappings.any? { |m| m.path == mapping.directory && m.writer == mapping.writer } }
|
|
|
|
.each { |mapping| deduplicated_mappings.add("#{mapping.path} #{mapping.writer}") }
|
|
|
|
|
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
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
if current_codeowners_content == new_codeowners_content
|
|
|
|
puts "~ CODEOWNERS already up to date".color(:yellow)
|
|
|
|
else
|
|
|
|
puts "✓ CODEOWNERS updated".color(:green)
|
|
|
|
end
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
if errors.present?
|
2023-03-04 22:38:38 +05:30
|
|
|
puts ""
|
|
|
|
puts "✘ Files with missing metadata found:".color(:red)
|
|
|
|
errors.map { |file| puts file }
|
2022-04-04 11:22:00 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|