Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ensure-alphabetize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ on:
paths:
- "Gemfile"
- "script/alphabetize"
- "script/suggest-alphabetize"
- "lib/alphabetize*.rb"
- "test/alphabetize_suggestions_test.rb"
- ".github/workflows/ensure-alphabetize.yml"
- "_data/**"
pull_request:
paths:
- "Gemfile"
- "script/alphabetize"
- "script/suggest-alphabetize"
- "lib/alphabetize*.rb"
- "test/alphabetize_suggestions_test.rb"
- ".github/workflows/ensure-alphabetize.yml"
- "_data/**"

Expand All @@ -28,6 +34,8 @@ jobs:
uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
with:
bundler-cache: true
- name: Test formatting suggestions
run: bundle exec ruby test/alphabetize_suggestions_test.rb
Comment thread
mlinksva marked this conversation as resolved.
Resolved
- name: Ensure Orgs are sorted
run: |
bundle exec script/alphabetize
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/suggest-alphabetize.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Suggest organization formatting

on:
pull_request_target:
types: [opened, synchronize, reopened]
paths:
- "_data/*.yml"

permissions:
contents: read
pull-requests: write

concurrency:
group: suggest-alphabetize-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
suggest:
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
# Never check out PR code: this job has permission to post reviews.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: ruby/setup-ruby@de492cf36cbab201753d720ce4f4e336cd688c94 # v1.336.0
- name: Suggest canonical organization formatting
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: ruby script/suggest-alphabetize
19 changes: 19 additions & 0 deletions lib/alphabetize.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'yaml'

module Alphabetize
def self.organization_list?(key, value)
key.is_a?(String) && value.is_a?(Array) && value.all?(String)
end

def self.format(contents)
data = YAML.safe_load(contents)
valid = data.is_a?(Hash) && data.all? { |key, value| organization_list?(key, value) }
raise ArgumentError, 'Organization data must map group names to lists of strings' unless valid

output = data.sort_by { |key, _value| key.upcase }.to_h.transform_values { |value| value.uniq.sort_by(&:upcase) }
# Preserve the data file's header comment and canonical spacing.
contents.lines.first + output.to_yaml.gsub(/^(.+?):$/, "\n\\1:").gsub(/^- /, ' - ').gsub(/\A---\n/m, '')
end
end
79 changes: 79 additions & 0 deletions lib/alphabetize_suggestions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

require 'digest'
require 'open3'
require 'tempfile'
require_relative 'alphabetize'

module AlphabetizeSuggestions
def self.visible_lines(patch)
line = 0
patch.to_s.lines.each_with_object(Set.new) do |text, lines|
if (match = text.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/))
line = match[1].to_i
elsif text.start_with?(' ', '+')
lines << line
line += 1
end
end
end

def self.diff(original, formatted, context: 0)
Tempfile.create('alphabetize-original') do |before|
Tempfile.create('alphabetize-formatted') do |after|
before.write(original)
after.write(formatted)
[before, after].each(&:flush)
output, error, status = Open3.capture3('diff', "-U#{context}", '--', before.path, after.path)
raise "Cannot generate formatting diff: #{error}" unless [0, 1].include?(status.exitstatus)

output
end
end
end

def self.replacement(original, formatted)
before = original.lines
after = formatted.lines
prefix = 0
prefix += 1 while prefix < [before.length, after.length].min && before[prefix] == after[prefix]
suffix = 0
suffix += 1 while suffix < [before.length, after.length].min - prefix && before[-suffix - 1] == after[-suffix - 1]
count = before.length - prefix - suffix
[count.zero? ? prefix : prefix + 1, count, after.slice(prefix, after.length - prefix - suffix)]
end

def self.comment(path, start, count, replacement, original)
if count.zero?
anchor = [start, 1].max
replacement = start.zero? ? replacement + [original.first] : [original[anchor - 1]] + replacement
start = anchor
count = 1
end
text = replacement.join
text += "\n" unless text.empty? || text.end_with?("\n")
fence = '`' * [3, text.scan(/`+/).map(&:length).max.to_i + 1].max
body = "Alphabetize and normalize organization data:\n\n#{fence}suggestion\n#{text}#{fence}"
marker = "<!-- alphabetize:#{Digest::SHA256.hexdigest([path, start, count, body].join("\n"))} -->"
result = { path: path, line: start + count - 1, side: 'RIGHT', body: "#{body}\n#{marker}" }
result.merge!(start_line: start, start_side: 'RIGHT') if count > 1
result
end

def self.for_file(path, original, patch)
raise ArgumentError, 'Unsafe organization data path' unless path.match?(%r{\A_data/[A-Za-z0-9_-]+\.yml\z})

formatted = Alphabetize.format(original)
return [[], false, ''] if formatted == original

visible = visible_lines(patch)
# One replacement keeps moves atomic, even when only their source is in the PR diff.
suggestion = comment(path, *replacement(original, formatted), original.lines)
first = suggestion.fetch(:start_line, suggestion.fetch(:line))
reviewable = (first..suggestion.fetch(:line)).all? { |line| visible.include?(line) }
incomplete = !reviewable || suggestion.fetch(:body).bytesize >= 60_000
comments = incomplete ? [] : [suggestion]
fallback = diff(original, formatted, context: 3)
[comments, incomplete, "--- a/#{path}\n+++ b/#{path}\n#{fallback.lines.drop(2).join}"]
end
end
21 changes: 2 additions & 19 deletions script/alphabetize
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@

# Alphabetizes the content of all YAML files in the _data directory

require 'open-uri'
require 'json'
require 'yaml'
require_relative '../lib/alphabetize'

files = Dir["#{File.expand_path('../_data/', File.dirname(__FILE__))}/*.yml"]

files.each do |file|
contents = File.read(file)
comment = contents.lines.first

data = YAML.safe_load(contents)
data = data.sort_by { |k, _v| k.upcase }

output = {}
data.each do |key, value|
value = value.uniq.sort_by(&:upcase)
output[key] = value
end

# Add extra line break between groups, indent arrays, and strip header
formatted_output = comment + output.to_yaml.gsub(/^(.+?):$/, "\n\\1:").gsub(/^- /, ' - ').gsub(/\A---\n/m, '')

File.write(file, formatted_output)
File.write(file, Alphabetize.format(File.read(file)))
end
75 changes: 75 additions & 0 deletions script/suggest-alphabetize
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'base64'
require 'json'
require 'open3'
require_relative '../lib/alphabetize_suggestions'

def api(path, body = nil, paginate: false)
args = ['gh', 'api', path]
args += ['--paginate', '--slurp'] if paginate
args += ['--method', 'POST', '--input', '-'] if body
output, error, status = Open3.capture3(*args, stdin_data: body ? JSON.generate(body) : '')
abort "GitHub API request failed: #{error}" unless status.success?

JSON.parse(output)
end

repo = ENV.fetch('GITHUB_REPOSITORY')
number = Integer(ENV.fetch('PR_NUMBER'))
pr = api("repos/#{repo}/pulls/#{number}")
abort 'Pull request is not open' unless pr.fetch('state') == 'open'
head = pr.fetch('head').fetch('sha')
files = api("repos/#{repo}/pulls/#{number}/files?per_page=100", paginate: true).flatten
existing = api("repos/#{repo}/pulls/#{number}/comments?per_page=100", paginate: true).flatten
comments = []
patches = []
incomplete = false
files.each do |file|
path = file.fetch('filename')
next unless path.match?(%r{\A_data/[^/]+\.yml\z}) && file.fetch('status') != 'removed'

abort 'Unsafe organization data path' unless path.match?(%r{\A_data/[A-Za-z0-9_-]+\.yml\z})
head_repo = pr.fetch('head').fetch('repo').fetch('full_name')
pinned = api("repos/#{head_repo}/contents/#{path}?ref=#{head}")
abort "Pull request file changed during formatting: #{path}; wait for its next run." unless pinned.fetch('sha') == file.fetch('sha')
blob = api("repos/#{head_repo}/git/blobs/#{file.fetch('sha')}")
abort "Organization data is too large: #{path}" if blob.fetch('size') > 1_000_000
original = Base64.strict_decode64(blob.fetch('content').delete("\n")).force_encoding(Encoding::UTF_8)
abort "Organization data is not valid UTF-8: #{path}" unless original.valid_encoding?
suggestions, partial, patch = AlphabetizeSuggestions.for_file(path, original, file['patch'])
patches << patch unless patch.empty?
incomplete ||= partial
comments.concat(suggestions)
end
comments.reject! do |comment|
marker = comment.fetch(:body).lines.last.strip
existing.any? do |old|
old.fetch('user').fetch('login') == 'github-actions[bot]' && old['commit_id'] == head && old.fetch('body').include?(marker)
end
end
abort 'Too many formatting suggestions; run script/alphabetize locally.' if comments.length > 50
body = 'Suggested organization-data formatting fixes.'
if incomplete
patch = patches.join("\n")
fence = '`' * [3, patch.scan(/`+/).map(&:length).max.to_i + 1].max
body += "\n\nSome corrections are outside the PR diff and cannot be inline suggestions. " \
"Run `bundle exec script/alphabetize` and commit the result, or apply this complete formatting patch:\n\n" \
"#{fence}diff\n#{patch}#{fence}"
end
abort 'Formatting patch is too large; run script/alphabetize locally.' if body.bytesize > 60_000
marker = "<!-- alphabetize-review:#{Digest::SHA256.hexdigest("#{head}\n#{body}")} -->"
reviews = api("repos/#{repo}/pulls/#{number}/reviews?per_page=100", paginate: true).flatten
already_posted = reviews.any? do |review|
review.fetch('user').fetch('login') == 'github-actions[bot]' && review.fetch('body').include?(marker)
end
if comments.empty? && (!incomplete || already_posted)
puts 'No new inline formatting suggestions to post.'
else
current = api("repos/#{repo}/pulls/#{number}")
abort 'Pull request changed during formatting; wait for its next run.' unless current.fetch('head').fetch('sha') == head
api("repos/#{repo}/pulls/#{number}/reviews",
{ commit_id: head, event: 'COMMENT', body: "#{body}\n#{marker}", comments: comments })
puts "Posted #{comments.length} formatting suggestions."
end
Loading
Loading