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
44 changes: 41 additions & 3 deletions .github/actions/sync/shared-config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@ def safe_system(*args)
abort "Failed to run command: #{args.join(" ")}" if status.nil?
exit(status.exitstatus || 1) unless status.success?
end

# Synced repositories can update an action before its source repository does,
# so keep a target's pin wherever it is newer than the source's for the same repository.
def keep_newer_action_pins(contents, target_path)
return contents unless target_path.file?

# Match only active `uses:` keys, quoted or not, so commented-out pins never count.
pin = %r{
^(?<key>[ \t]*(?:-[ \t]+)?uses:[ \t]+)(?<quote>["']?)
(?<repository>[\w.-]+/[\w.-]+)(?<action>/[^@\s"']+)?@(?<sha>\h{40})\k<quote>
(?<comment>[ \t]+\#[ \t]+)(?<version>v?\d\S*)
}x
version = lambda do |match|
tag = match[:version].delete_prefix("v")
Gem::Version.new(tag) if Gem::Version.correct?(tag)
end

newest_target_pins = {}
target_path.read.scan(pin) do
match = Regexp.last_match
next unless (target_version = version.call(match))

repository = match[:repository].downcase
newest = newest_target_pins[repository]
newest_target_pins[repository] = match if newest.nil? || target_version > version.call(newest)
end

contents.gsub(pin) do
match = Regexp.last_match
newest = newest_target_pins[match[:repository].downcase]
source_version = version.call(match)
next match.to_s if newest.nil? || source_version.nil? || version.call(newest) <= source_version

"#{match[:key]}#{match[:quote]}#{match[:repository]}#{match[:action]}@#{newest[:sha]}" \
"#{match[:quote]}#{match[:comment]}#{newest[:version]}"
end
end
# rubocop:enable Style/TopLevelMethodDefinition

if ARGV[0].to_s.empty? || ARGV[1].to_s.empty? || ARGV[4]
Expand Down Expand Up @@ -130,8 +167,8 @@ def safe_system(*args)

target_gemfile_locks = []
dependabot_config_yaml = YAML.load_file(dependabot_template_yaml)
# This should be run after dependabot.yml for this repository (Monday)
# and after the sync-shared-config job for synced repositories (Wednesday).
# Every synced repository, including this one and `brew`, updates on Friday,
# between the Wednesday sync-shared-config jobs.
# This maximises the chance of a single sync per week handling both any
# changes and any dependabot updates.
dependabot_config_yaml["updates"].each do |update|
Expand Down Expand Up @@ -345,7 +382,7 @@ def safe_system(*args)
end
next if path == target_path.to_s

contents = path.read
contents = keep_newer_action_pins(path.read, target_path)
FileUtils.rm_f target_path
target_path.write(
"# This file is synced from `Homebrew/brew` by the `.github` repository, do not modify it directly.\n" \
Expand Down Expand Up @@ -450,6 +487,7 @@ def safe_system(*args)
Pathname(path).read
.chomp
end
contents = keep_newer_action_pins(contents, target_path)

header = "# This file is synced from the `.github` repository, do not modify it directly.\n"
# Keep a shebang on the first line so synced shell scripts still pass ShellCheck.
Expand Down
62 changes: 62 additions & 0 deletions .github/actions/sync/shared-config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def expect_coderabbit_config(opted_in: false, directory: target)
JS
end

def repin(contents, repository, sha, version)
contents.gsub(%r{(?<=uses: #{Regexp.escape(repository)})(/[^@\s]+)?@\h{40} # \S+}) do
"#{Regexp.last_match(1)}@#{sha} # #{version}"
end
end

def synced_contents(path)
contents = (source_repository/path).read
if contents.start_with?("#!")
Expand Down Expand Up @@ -541,6 +547,62 @@ def synced_contents(path)
expect_idempotent_sync
end

context "with action pins in synced workflows" do
let(:newer_sha) { "a" * 40 }
let(:newer_version) { "9999.1.1.1" }

it "keeps newer pins per repository and takes newer source pins, repeatedly" do
workflow = ".github/workflows/actionlint.yml"
newer = repin(synced_contents(workflow), "Homebrew/actions", newer_sha, newer_version)
write workflow, repin(newer, "actions/checkout", "b" * 40, "v1.0.0")
commit_fixture
sync_target
expect((target/workflow).read).to eq(newer)
expect_idempotent_sync
end

it "keeps newer pins in the docs workflow synced from brew" do
workflow = ".github/workflows/docs.yml"
write "docs/index.md"
commit_fixture
sync_target
synced = (target/workflow).read
newer = repin(synced, "Homebrew/actions", newer_sha, newer_version)
expect(newer).not_to eq(synced)
write workflow, newer
commit_fixture
sync_target
expect((target/workflow).read).to eq(newer)
expect_idempotent_sync
end

it "ignores pins without a comparable version" do
workflow = ".github/workflows/actionlint.yml"
write workflow, repin(synced_contents(workflow), "Homebrew/actions", newer_sha, "main")
commit_fixture
sync_target
expect((target/workflow).read).to eq(synced_contents(workflow))
end

it "ignores commented-out pins" do
workflow = ".github/workflows/actionlint.yml"
synced = synced_contents(workflow)
write workflow, "#{synced}# uses: Homebrew/actions/setup-homebrew@#{newer_sha} # #{newer_version}\n"
commit_fixture
sync_target
expect((target/workflow).read).to eq(synced)
end

it "keeps newer quoted and differently spaced pins" do
workflow = ".github/workflows/actionlint.yml"
newer = repin(synced_contents(workflow), "Homebrew/actions", newer_sha, newer_version)
write workflow, newer.gsub(%r{uses: (Homebrew/actions/\S+@\h{40})}, "uses: '\\1'")
commit_fixture
sync_target
expect((target/workflow).read).to eq(newer)
end
end

%w[root root-with-lock docs shared-docs dangling-docs first-sync-docs].each do |layout|
it "migrates the #{layout} Gemfile layout before updating lockfiles and is idempotent" do
if %w[root root-with-lock shared-docs].include?(layout)
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/sync-shared-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ on:
- main
pull_request:
schedule:
# This should be run after dependabot.yml for this repository (Monday)
# and before dependabot.yml for synced repositories (Friday).
# This should be run between Friday's dependabot.yml updates for this
# repository, `brew` and synced repositories.
# This maximises the chance of a single sync per week handling both any
# changes and any dependabot updates.
- cron: "0 8 * * 3" # Every Wednesday at 8 AM
Expand Down
Loading