From 301c890a164acdb20fa7d43ad242040c1da70f75 Mon Sep 17 00:00:00 2001 From: Patrick Linnane Date: Thu, 19 Nov 2026 09:35:05 -0800 Subject: [PATCH] sync: keep newer action pins in synced workflows Signed-off-by: Patrick Linnane --- .github/actions/sync/shared-config.rb | 44 +++++++++++++-- .github/actions/sync/shared-config_spec.rb | 62 ++++++++++++++++++++++ .github/workflows/sync-shared-config.yml | 4 +- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/.github/actions/sync/shared-config.rb b/.github/actions/sync/shared-config.rb index a2b2dd2f..d09680e9 100755 --- a/.github/actions/sync/shared-config.rb +++ b/.github/actions/sync/shared-config.rb @@ -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{ + ^(?[ \t]*(?:-[ \t]+)?uses:[ \t]+)(?["']?) + (?[\w.-]+/[\w.-]+)(?/[^@\s"']+)?@(?\h{40})\k + (?[ \t]+\#[ \t]+)(?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] @@ -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| @@ -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" \ @@ -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. diff --git a/.github/actions/sync/shared-config_spec.rb b/.github/actions/sync/shared-config_spec.rb index 7bb87b94..efaee06f 100644 --- a/.github/actions/sync/shared-config_spec.rb +++ b/.github/actions/sync/shared-config_spec.rb @@ -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?("#!") @@ -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) diff --git a/.github/workflows/sync-shared-config.yml b/.github/workflows/sync-shared-config.yml index 1fe42235..200a659a 100644 --- a/.github/workflows/sync-shared-config.yml +++ b/.github/workflows/sync-shared-config.yml @@ -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