Skip to content
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

Improve shared configuration syncing. #110

Merged
merged 1 commit into from
May 24, 2024
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
79 changes: 49 additions & 30 deletions .github/actions/sync/shared-config.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,78 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'fileutils'
require 'open3'
require 'pathname'
require "English"
require "fileutils"
require "open3"
require "pathname"

# This makes sense for a standalone script.
# rubocop:disable Style/TopLevelMethodDefinition
def git(*args)
system 'git', *args
exit $?.exitstatus unless $?.success?
system "git", *args
exit $CHILD_STATUS.exitstatus unless $CHILD_STATUS.success?
end
# rubocop:enable Style/TopLevelMethodDefinition

target_dir = Pathname(ARGV[0])
source_dir = ARGV[1]
target_directory = ARGV[0]
target_directory_path = Pathname(target_directory)
homebrew_repository_path = Pathname(ARGV[1])

puts 'Detecting changes…'
if !target_directory_path.directory? || !homebrew_repository_path.directory? || ARGV[2]
abort "Usage: #{$PROGRAM_NAME} <target_directory_path> <homebrew_repository_path>"
end

ruby_version = ".ruby-version"
rubocop_yml = ".rubocop.yml"

homebrew_ruby_version =
(homebrew_repository_path/"Library/Homebrew/vendor/portable-ruby-version").read.chomp.sub(/_\d+$/, "")
homebrew_rubocop_config = homebrew_repository_path/"Library/Homebrew/#{rubocop_yml}"

puts "Detecting changes…"
[
'.github/workflows/lock-threads.yml',
'.github/workflows/stale-issues.yml',
'.ruby-version',
].each do |glob|
src_paths = Pathname.glob(glob)
dst_paths = Pathname.glob(target_dir.join(glob))

dst_paths.each do |path|
FileUtils.rm_f path
end
ruby_version,
rubocop_yml,
".github/workflows/lock-threads.yml",
".github/workflows/stale-issues.yml",
].each do |file|
target_path = target_directory_path/file
target_path.dirname.mkpath

src_paths.each do |path|
target_dir.join(path.dirname).mkpath
FileUtils.cp path, target_dir.join(path)
case file
when ruby_version
target_path.write("#{homebrew_ruby_version}\n")
when rubocop_yml
FileUtils.cp homebrew_rubocop_config, target_path
else
FileUtils.cp file, target_path
end
end

out, err, status = Open3.capture3('git', '-C', target_dir.to_s, 'status', '--porcelain', '--ignore-submodules=dirty')
out, err, status = Open3.capture3("git", "-C", target_directory, "status", "--porcelain", "--ignore-submodules=dirty")
raise err unless status.success?

target_dir_changed = !out.chomp.empty?
target_directory_path_changed = !out.chomp.empty?

unless target_dir_changed
puts 'No changes detected.'
unless target_directory_path_changed
puts "No changes detected."
exit
end

git '-C', target_dir.to_s, 'add', '--all'
git "-C", target_directory, "add", "--all"

out, err, status = Open3.capture3('git', '-C', target_dir.to_s, 'diff', '--name-only', '--staged')
out, err, status = Open3.capture3("git", "-C", target_directory, "diff", "--name-only", "--staged")
raise err unless status.success?

modified_paths = out.lines.map(&:chomp)

modified_paths.each do |modified_path|
puts "Detected changes to #{modified_path}."
git '-C', target_dir.to_s, 'commit', modified_path, '--message', "#{File.basename(modified_path)}: update to match main configuration", '--quiet'
git "-C", target_directory, "commit", modified_path, "--message",
"#{File.basename(modified_path)}: update to match main configuration", "--quiet"
end
puts

File.open(ENV.fetch('GITHUB_OUTPUT'), 'a') do |f|
f.puts 'pull_request=true'
File.open(ENV.fetch("GITHUB_OUTPUT"), "a") do |f|
f.puts "pull_request=true"
end
19 changes: 17 additions & 2 deletions .github/workflows/sync-shared-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
pull_request:

permissions:
contents: read
Expand Down Expand Up @@ -45,11 +46,25 @@ jobs:
- Homebrew/rubydoc.brew.sh
fail-fast: false
steps:
- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: Clone main repository
uses: actions/checkout@v4
with:
persist-credentials: false

- run: brew install-bundler-gems --groups=style
if: ${{ matrix.repo == 'Homebrew/.github' }}
env:
HOMEBREW_DEVELOPER: 1

- run: brew style .github/actions/sync/*.rb
if: ${{ matrix.repo == 'Homebrew/.github' }}
env:
HOMEBREW_DEVELOPER: 1

- name: Clone secondary repository
uses: actions/checkout@v4
with:
Expand All @@ -71,10 +86,10 @@ jobs:
id: detect_changes
env:
HOMEBREW_GPG_PASSPHRASE: ${{ secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY_PASSPHRASE }}
run: ./.github/actions/sync/shared-config.rb 'vendor/${{ matrix.repo }}' '${{ matrix.repo }}' 'sync-shared-config'
run: ./.github/actions/sync/shared-config.rb 'vendor/${{ matrix.repo }}' '/home/linuxbrew/.linuxbrew/Homebrew'

- name: Create pull request
if: ${{ steps.detect_changes.outputs.pull_request == 'true' }}
if: ${{ github.ref == 'refs/heads/master' && steps.detect_changes.outputs.pull_request == 'true' }}
uses: peter-evans/create-pull-request@6d6857d36972b65feb161a90e484f2984215f83e
with:
path: vendor/${{ matrix.repo }}
Expand Down
80 changes: 80 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
inherit_from:
- ../.rubocop.yml

Bundler/GemFilename:
Enabled: false

Homebrew/MoveToExtendOS:
Enabled: true
Exclude:
- "{extend,test,requirements}/**/*"
- "os.rb"
Comment on lines +7 to +11
Copy link
Member

@Bo98 Bo98 May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this is intentionally Homebrew/brew specific like this cop (we don't want to block if OS.mac? in formulae etc). Is this the correct file?

The ../rubocop.yml isn't correct for any repository AFAIK, since if we're targeting Library/.rubocop.yml it should be ../../.. for taps (and nothing for non-taps).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, not correct, see #111 to fix.


Naming/PredicateName:
inherit_mode:
merge:
- AllowedMethods
AllowedMethods:
- is_32_bit?
- is_64_bit?

# Only enforce documentation for public APIs.
# Checked by the tests.yml syntax job
Style/Documentation:
AllowedConstants:
- Homebrew
Include:
- cask/cask.rb
- cask/dsl.rb
- cask/dsl/version.rb
- cask/url.rb
- development_tools.rb
- download_strategy.rb
- extend/ENV/super.rb
- extend/kernel.rb
- extend/pathname.rb
- formula.rb
- formula_assertions.rb
- formula_free_port.rb
- language/go.rb
- language/java.rb
- language/node.rb
- language/perl.rb
- language/python.rb
- livecheck/strategy/apache.rb
- livecheck/strategy/bitbucket.rb
- livecheck/strategy/cpan.rb
- livecheck/strategy/crate.rb
- livecheck/strategy/extract_plist.rb
- livecheck/strategy/git.rb
- livecheck/strategy/github_latest.rb
- livecheck/strategy/github_releases.rb
- livecheck/strategy/gnome.rb
- livecheck/strategy/gnu.rb
- livecheck/strategy/hackage.rb
- livecheck/strategy/json.rb
- livecheck/strategy/launchpad.rb
- livecheck/strategy/npm.rb
- livecheck/strategy/page_match.rb
- livecheck/strategy/pypi.rb
- livecheck/strategy/sourceforge.rb
- livecheck/strategy/sparkle.rb
- livecheck/strategy/xml.rb
- livecheck/strategy/xorg.rb
- livecheck/strategy/yaml.rb
- os.rb
- resource.rb
- utils/inreplace.rb
- utils/shebang.rb
- utils/string_inreplace_extension.rb
- version.rb
- tap.rb

Homebrew/NegateInclude:
Exclude:
# YARD runs stand-alone.
- yard/docstring_parser.rb

Style/DocumentationMethod:
Include:
- "formula.rb"