refactor(ci): fetch the diff baseline instead of mirroring main

The sync job force-pushed the source repository's main onto this one so that
`origin/main` would mean "upstream main" for the three consumers that diff
against it. Carrying .github/workflows/** in that push is what GitHub refuses
for an App token, and `workflows` is not a GITHUB_TOKEN permission scope, so the
job needed an app installation that may write workflow files. Run 37169357364
showed the fork has no such secret at all.

Nothing reads the pushed branch. push_ci_policy.sh, affected_roles.sh and
new_roles.sh each fetch `origin main` into their own runner's clone before
reading it, and no caller queries the branch through the API. Pointing those
fetches at the configured source repository gives the same baseline read-only:
no push, no credential, no app permission to widen.

baseline_remote.sh resolves where that baseline comes from and keeps the
normalization and the disabled-value list in one place: `origin` for the source
repository itself, for a disabled sync and for a local clone without
GITHUB_REPOSITORY, and the upstream fetch URL otherwise. The fetches now carry
an explicit `main:refs/remotes/origin/main` refspec, which a URL needs and a
remote name tolerates.

The release guard keeps fetching `origin`: it asks whether a tag commit sits on
this repository's own main, which is a different question from the diff
baseline.

Removed: the sync-main job, its `needs` edge, main_from_repository.sh and the
directory that held it.

test_diff_affected_roles.py copies only the script under test into its fixture,
so it needed the new helper too. It also inherited the full environment, and a
GITHUB_REPOSITORY present in CI would have sent the fixture's baseline fetch to
the real upstream instead of its own bare remote: three of its five cases fail
that way, and the run takes 24s instead of 0.3s because it goes to the network.
The variable is now dropped alongside BASH_ENV.

Verified by running the helper in all four contexts and affected_roles.sh from
an empty refs/remotes/origin/main in a scratch clone as the fork, as the source
repository and locally; each populated the ref and completed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-10-04 05:44:45 +02:00
1 parent a3e8587d93
commit bf25eb2fc7
8 files changed
+35 -35

No files matched your search

-3
View File
@@ -1,3 +0,0 @@
# GitHub Sync Helpers 🔄
This directory contains shell helpers that synchronize repository refs before later workflow jobs derive CI scope from git history.
@@ -1,51 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
: "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}"
default_source="${CI_SYNC_MAIN_SOURCE_REPOSITORY_DEFAULT:-infinito-nexus/core}"
configured_source="${CI_SYNC_MAIN_SOURCE_REPOSITORY:-}"
configured_source_is_set="${CI_SYNC_MAIN_SOURCE_REPOSITORY_IS_SET:-false}"
if [[ "${configured_source_is_set}" == "true" ]]; then
source_repository="${configured_source}"
else
source_repository="${default_source}"
fi
source_trimmed="${source_repository//[[:space:]]/}"
source_lower="$(printf '%s' "${source_trimmed}" | tr '[:upper:]' '[:lower:]')"
case "${source_lower}" in
"" | "false" | "0" | "no" | "off" | "none")
echo "Main sync skipped because CI_SYNC_MAIN_SOURCE_REPOSITORY is disabled."
exit 0
;;
esac
normalize_repository() {
local value="${1}"
value="${value#https://github.com/}"
value="${value#http://github.com/}"
value="${value#ssh://git@github.com/}"
value="${value#git@github.com:}"
value="${value%.git}"
printf '%s' "${value}" | tr '[:upper:]' '[:lower:]'
}
target_repository="$(normalize_repository "${GITHUB_REPOSITORY}")"
source_repository="$(normalize_repository "${source_trimmed}")"
if [[ "${source_repository}" == "${target_repository}" ]]; then
echo "Main sync skipped because source repository matches current repository: ${GITHUB_REPOSITORY}."
exit 0
fi
if [[ "${source_repository}" != */* ]]; then
echo "ERROR: CI_SYNC_MAIN_SOURCE_REPOSITORY must be '<owner>/<repo>', a GitHub URL, or a disabled value." >&2
exit 1
fi
echo "Syncing ${GITHUB_REPOSITORY}:main from ${source_repository}:main."
git fetch "https://github.com/${source_repository}.git" main:refs/remotes/main-sync-source/main --force
git push origin refs/remotes/main-sync-source/main:refs/heads/main --force