Repository navigation
Suggest organization formatting fixes on pull requests #1362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.