diff --git a/.github/workflows/project.yml b/.github/workflows/project.yml index dbdd8585eb..5a30a578af 100644 --- a/.github/workflows/project.yml +++ b/.github/workflows/project.yml @@ -218,8 +218,4 @@ jobs: env: GH_TOKEN: ${{ github.token }} RUN_ID: ${{ github.run_id }} - run: | - has_source=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN_ID/artifacts" --jq 'any(.artifacts[]; .name == "registry-source" and .expired == false)') - if [ "$has_source" = true ]; then - gh workflow run registry.yml --ref main -f provider=all -f source_run="$RUN_ID" - fi + run: bash scripts/dispatch-registry.sh diff --git a/scripts/dispatch-registry.sh b/scripts/dispatch-registry.sh new file mode 100644 index 0000000000..fe5c33c78d --- /dev/null +++ b/scripts/dispatch-registry.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -euo pipefail + +has_source=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN_ID/artifacts" --jq 'any(.artifacts[]; .name == "registry-source" and .expired == false)') +if [ "$has_source" != true ]; then + exit 0 +fi + +state=$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/registry.yml" --jq '.state') +if [ "$state" != active ]; then + echo "Registry transfers skipped: registry.yml is $state." + exit 0 +fi + +gh workflow run registry.yml --ref main -f provider=all -f source_run="$RUN_ID" diff --git a/test/bench/registry_dispatch_test.rb b/test/bench/registry_dispatch_test.rb new file mode 100644 index 0000000000..274b326ea6 --- /dev/null +++ b/test/bench/registry_dispatch_test.rb @@ -0,0 +1,57 @@ +require_relative "test_helper" + +class RegistryDispatchTest < Minitest::Test + def test_dispatches_an_active_workflow_with_source_artifacts + output, status, calls = dispatch(source: "true", state: "active") + + assert status.success?, output + assert_includes calls, "workflow run registry.yml --ref main -f provider=all -f source_run=123" + end + + def test_skips_a_disabled_workflow + output, status, calls = dispatch(source: "true", state: "disabled_manually") + + assert status.success?, output + assert_includes output, "Registry transfers skipped: registry.yml is disabled_manually." + refute_includes calls, "workflow run" + end + + def test_does_not_query_the_workflow_without_source_artifacts + output, status, calls = dispatch(source: "false", state: "active") + + assert status.success?, output + refute_includes calls, "actions/workflows" + refute_includes calls, "workflow run" + end + + def test_keeps_api_failures_fatal + output, status, calls = dispatch(source: "true", state: "active", api_status: "1") + + refute status.success?, output + refute_includes calls, "workflow run" + end + + private + def dispatch(source:, state:, api_status: "0") + Dir.mktmpdir do |dir| + gh = File.join(dir, "gh") + calls = File.join(dir, "calls") + File.write(gh, <<~SH) + #!/usr/bin/env bash + echo "$*" >> "$CALLS" + case "$2" in + */artifacts) echo "$SOURCE" ;; + */registry.yml) echo "$STATE"; exit "$API_STATUS" ;; + esac + SH + File.chmod(0o755, gh) + output, status = Open3.capture2e( + { "PATH" => "#{dir}:#{ENV.fetch("PATH")}", "CALLS" => calls, + "SOURCE" => source, "STATE" => state, "API_STATUS" => api_status, + "GITHUB_REPOSITORY" => "boringcache/benchmarks", "RUN_ID" => "123" }, + "bash", File.join(Bench::ROOT, "scripts/dispatch-registry.sh") + ) + [output, status, File.read(calls)] + end + end +end