Skip to content

feat(migrate-consumers): open fork PRs when the machine user lacks write access - #6

Merged
Sayt-0 merged 3 commits into
mainfrom
feat/migrate-consumers-fork-fallback
Jun 23, 2026
Merged

Sayt-0 merged 3 commits into
mainfrom
feat/migrate-consumers-fork-fallback

Conversation

@Sayt-0

@Sayt-0 Sayt-0 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

The first real migrate-consumers run opened PRs on only 16 of 64 consumers; the other 48 failed because the machine user lacks write access on those repos. This adds a fork-PR fallback so those repos can still receive a migration PR.

Per repo, after the rewrite, a route is resolved:

  • direct: the machine user has write, so commit the branch into the repo and open a same-repo PR (unchanged path).
  • fork: no write but allow_forking=true, so fork under the machine user, create the signed commit on the fork, and open a cross-repo PR (head owner:branch). Needs only read on the upstream, like an external contribution.
  • skip: no write and forking disabled. Reported under Skipped for a manual update.

The route is resolved before the dry-run gate, so a dry run reports the route per repo (route: direct|fork|skip) without any writes.

Coverage

  • 43 of the 48 not-yet-migrated repos are forkable.
  • 5 have forking disabled (billing, infra-tailscale, jit, registry-tools, secrets-engine-private) and are skipped for a write grant or a manual update.

Robustness

  • Idempotent re-runs: a fork PR is looked up via the REST pulls endpoint (head=owner:branch), because gh pr list --head does not support that form and would never match a fork PR.
  • Wrong-repo guard: proceeds only when the fork parent is the upstream repo, so a name collision in the machine user namespace cannot cause a commit into an unrelated repo.
  • Clean diff: the fork default branch is force-synced to upstream before committing.
  • A failed default-branch lookup skips that one repo instead of aborting the whole run.

Validation

  • bash -n and YAML parse pass.
  • Routing logic unit-tested across direct, fork, and skip in both dry and real modes, plus the collision guard.
  • Live fork-to-PR mechanics remain to be validated by a single-repo pilot (repos=docker/cli, dry-run=false) before the full run.

@Sayt-0
Sayt-0 requested a review from derekmisler June 22, 2026 20:14

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assessment: 🟡 NEEDS ATTENTION

Fork-PR fallback logic is sound overall — routing, collision guard, idempotent PR lookup, and dry-run reporting all work correctly. Two error-handling gaps in the new fork route could cause confusing failures or hard-to-diagnose skips.

Comment thread .github/workflows/migrate-consumers.yml Outdated
COMMIT_REPO="$REPO"
PR_HEAD="$BRANCH"
else
FORK_OWNER=$(gh api user --jq .login)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] No error check on gh api user --jq .login — silent empty FORK_OWNER causes misleading downstream failures

When ROUTE=fork, FORK_OWNER is resolved via:

FORK_OWNER=$(gh api user --jq .login)

Two failure paths exist:

  1. gh api user exits non-zero (network error, token lacks user scope): under set -euo pipefail, a non-zero exit from a command substitution assigned directly to a variable aborts the entire workflow run, not just this one repo. The whole migration job fails with no per-repo SUMMARY_SKIPPED entry and no actionable error pointing to the root cause.

  2. gh exits 0 but .login is empty (e.g., GitHub App token returning a different JSON shape, or jq emitting an empty string for a null field): FORK_OWNER is silently set to "", producing FORK="/repo-name". Subsequent gh api "repos//repo-name" calls fail with confusing 404s, and the repo ends up in (fork not ready) — a misleading message that hides the real problem.

Suggested fix:

FORK_OWNER=$(gh api user --jq .login) || {
  echo "::warning::Could not resolve machine-user login for $REPO — skipping"
  SUMMARY_SKIPPED+="- ${REPO} (could not resolve fork owner)"$'\n'
  cleanup_workdir
  continue
}
if [ -z "$FORK_OWNER" ]; then
  echo "::warning::Machine-user login resolved to empty for $REPO — skipping"
  SUMMARY_SKIPPED+="- ${REPO} (empty fork owner)"$'\n'
  cleanup_workdir
  continue
fi

Comment thread .github/workflows/migrate-consumers.yml Outdated
echo "No write access on $REPO — opening a fork PR from ${FORK}"
# Fork is idempotent (no-op when it already exists) and created
# asynchronously, so poll until the API can see it.
gh repo fork "$REPO" --clone=false --default-branch-only >/dev/null 2>&1 || true

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[MEDIUM] Fork creation errors silently swallowed — (fork not ready) is misleading when gh repo fork itself fails

gh repo fork "$REPO" --clone=false --default-branch-only >/dev/null 2>&1 || true

The >/dev/null 2>&1 || true combination discards both output and exit code. When fork creation fails (rate limit, transient API error, permissions issue), the error message is gone and the polling loop runs for 30 seconds (10×3 s) before emitting "Fork did not become available in time — skipping". This message is accurate for the async-readiness case but misleading when the fork creation itself failed — an operator seeing it cannot distinguish "fork API is slow" from "fork creation was rejected."

The || true is intentional for idempotency (some gh versions return non-zero when the fork already exists), but that only justifies ignoring the exit code — it doesn't require discarding stderr.

Suggested fix — capture stderr so it surfaces in the skip warning:

FORK_ERR=$(gh repo fork "$REPO" --clone=false --default-branch-only 2>&1 >/dev/null || true)
# ... polling loop ...
if [ -z "$FORK_READY" ]; then
  echo "::warning::Fork ${FORK} did not become available — skipping $REPO${FORK_ERR:+ (fork error: $FORK_ERR)}"
  ...
fi

@derekmisler derekmisler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

two issues in the new fork path worth fixing before the pilot run: a jq null/false mismatch in the routing logic that can silently skip forkable repos, and a missing error handler on FORK_OWNER that would abort the whole step on a transient API failure. everything else looks solid.

Comment thread .github/workflows/migrate-consumers.yml Outdated
CAN_PUSH=$(gh api "repos/$REPO" --jq '.permissions.push' 2>/dev/null || echo "false")
if [ "$CAN_PUSH" = "true" ]; then
ROUTE="direct"
elif [ "$(gh api "repos/$REPO" --jq '.allow_forking' 2>/dev/null || echo "false")" = "true" ]; then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

issue: jq returns the string "null" (not "false") when .allow_forking is absent from the API response. || echo "false" only fires when the whole gh invocation fails, not when the field is missing. so a repo with no permissions.push key and no allow_forking key both fall through here and get routed to skip, silently missing the fork path.

fix: cache the response and use // false in jq to coerce null to a boolean:

REPO_META=$(gh api "repos/$REPO" 2>/dev/null || echo '{}')
CAN_PUSH=$(echo "$REPO_META" | jq -r '.permissions.push // false')
ALLOW_FORKING=$(echo "$REPO_META" | jq -r '.allow_forking // false')

bonus: one API call instead of two, which also closes the race where settings change between the two calls.

Comment thread .github/workflows/migrate-consumers.yml Outdated
COMMIT_REPO="$REPO"
PR_HEAD="$BRANCH"
else
FORK_OWNER=$(gh api user --jq .login)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

issue: FORK_OWNER=$(gh api user --jq .login) has no error handler, and set -euo pipefail is active. every other fallible command in this loop has a || { ... cleanup_workdir; continue; } guard; this one is missing it. a transient API failure (rate limit, network blip, auth issue) aborts the entire migration step immediately instead of skipping just this one repo.

fix:

FORK_OWNER=$(gh api user --jq .login) || {
  echo "::warning::could not resolve machine-user login -- skipping $REPO"
  SUMMARY_SKIPPED+="- ${REPO} (fork-owner lookup failed)"$'\n'
  cleanup_workdir
  continue
}

@derekmisler derekmisler left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ci is green, approving!

@Sayt-0
Sayt-0 merged commit a317d63 into main Jun 23, 2026
13 checks passed
@Sayt-0
Sayt-0 deleted the feat/migrate-consumers-fork-fallback branch June 23, 2026 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants