Skip to content

Gate fork-exposed workflows on github.repository_owner - #375

Merged
max-sixty merged 7 commits into
mainfrom
workflow-fork-guards
May 3, 2026
Merged

max-sixty merged 7 commits into
mainfrom
workflow-fork-guards

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Summary

Adds if: github.repository_owner == '<canonical>' to the six workflows whose triggers can fire from a fork's own Actions (tend-triage, tend-ci-fix, tend-nightly, tend-weekly, tend-review-runs, tend-notifications). Skipped: tend-review (uses pull_request_target — base repo only) and tend-mention (already filters fork PRs explicitly).

The owner is auto-detected by tend init:

  1. gh repo view --json nameWithOwner resolves the directory's default repo (canonical when upstream is configured or gh repo set-default was used).
  2. gh api repos/<owner>/<name> returns .source.owner.login — the root canonical, so chained forks (alice → bob → canonical) collapse to canonical in one call.

If gh is unavailable or either call fails, the guard is omitted with a warning — workflows still work, but fork contributors may see noisy red checks. Per-job jobs.<name>.if extras (RFC 7396 merge) provide an escape hatch for unusual setups.

github.repository_owner is the only check that works across all four affected event types: secrets aren't available in jobs.<job_id>.if, and github.event.repository.fork is null for schedule events.

Test plan

  • uv run pytest — 188 tests pass, including new fork-guard rendering snapshots and detect_canonical_owner coverage (chained forks, no-gh, API failure)
  • pre-commit run --all-files — clean
  • Verified end-to-end on ~/workspace/prql (origin = personal fork): detect_canonical_owner() returns 'PRQL'

max-sixty and others added 7 commits May 2, 2026 13:06
Workflows whose triggers can fire from a fork's own Actions
(`schedule`, `workflow_dispatch`, `workflow_run`, `issues`) previously
had no fork guard, so an OSS adopter's contributors could trigger
runs from their fork that immediately failed because the bot/Claude
secrets are empty there — surfacing as red checks.

Add a job-level `if: github.repository_owner == '<owner>'` to:
tend-ci-fix, tend-nightly, tend-weekly, tend-review-runs,
tend-notifications, tend-triage. tend-review (`pull_request_target`,
base-repo only) and tend-mention (existing `head.repo.full_name`
filter for review events) are deliberately not guarded.

`secrets` isn't allowed in `jobs.<job_id>.if` and
`github.event.repository.fork` is null for `schedule` events, so
comparing `github.repository_owner` against a known owner is the
only reliable check that works across all the affected events.

Owner is auto-detected from `git remote get-url origin` at init
time, mirroring the existing `default_branch` autodetection; an
explicit `repo_owner` in `.config/tend.toml` overrides. When neither
source yields an owner, the guard is omitted and `init` warns.

Co-Authored-By: Claude <[email protected]>
Per follow-up: there's no value in a user-overridable `repo_owner`
config field — `init` already auto-detects the owner from `git remote
get-url origin`, which is the right answer for every realistic case.
Removing the knob keeps the public API canonical (one path, no
options) and matches how `default_branch` is already handled.

`Config.repo_owner` stays on the dataclass (cli.init still injects
it after load); only the toml-parsing surface and example-doc
section go away. Tests that need the guard rendered now mutate
`cfg.repo_owner` directly to mirror cli.init's injection, which
also lets the regtest snapshots revert to the no-guard baseline
that downstream consumers see when auto-detection fails.

Co-Authored-By: Claude <[email protected]>
`origin` is the remote you cloned from, which in fork-based
maintainer workflows is the personal fork — auto-detecting the
owner from `git remote get-url origin` would inject the fork owner
into the guard, and the workflow would never match on the canonical
repo.

`gh repo view` resolves the *default* repo for the current
directory (the upstream remote when present, configurable via
`gh repo set-default` for ambiguous cases). That's the answer we
want. Reuses `checks.detect_repo()` which already wraps this call.

When `gh` is unavailable or no default repo is configured, the
warning text now points at the right fix.

Co-Authored-By: Claude <[email protected]>
From two passes of /reviewing-code on the fork-guard work:

- Inline the one-call-site `_combine_if` helper. Repo CLAUDE.md is
  explicit about no wrapper functions; the rendered shape now reads
  inline at the ci-fix call site.
- Compress `_fork_guard` docstring from ~10 lines to 6 — keeps the
  load-bearing facts (why secrets fail, why event.repository.fork is
  null on schedule, what `""` means) and drops the parts already
  explained at call sites and in DESIGN.md.

Coverage gaps the reviewer surfaced:

- `test_user_job_if_extra_replaces_fork_guard` pins the RFC 7396
  scalar-replacement behavior — user-supplied `[workflows.X.jobs.X]
  if = "..."` clobbers the rendered guard outright. Parametrized
  over triage (guard-only) and ci-fix (guard + conclusion check) so
  a future merge-strategy change is a deliberate choice, not an
  accident.
- `test_fork_guard_rendered_shape_regtest` snapshots all 6 guarded
  workflows with `repo_owner` set, locking down the rendered `if:`
  shape that downstream consumers actually see (the existing
  regtests run with auto-detect failed, so the guard line was
  never snapshotted).
- Three unit tests for `_detect_repo_owner_local` covering the
  strip-owner, gh-unavailable, and malformed-response paths, plus
  an init-warns-when-undetected integration test.

185 tests pass (was 173); pre-commit clean.

Co-Authored-By: Claude <[email protected]>
When `tend init` runs in a fork-only checkout (origin = personal
fork, no upstream), `gh repo view` returns the fork's owner — and
the previous code took that at face value. But the workflows are
committed and shipped to the canonical repo, so the guard string
must match the canonical owner regardless of where init runs from.
With the bug, a contributor regenerating workflows in their fork
would silently change the guard from `<canonical>` to themselves,
and the maintainer might merge that without noticing.

`detect_canonical_owner` (now in checks.py) does a two-step
resolution:

1. `gh repo view` for the directory's default repo. Already returns
   the canonical when `upstream` is configured or
   `gh repo set-default` has been run.
2. If that result is itself a fork, walk to `.source.owner.login`
   via the GitHub API. `source` is the *root* canonical, so
   chained forks (alice → bob → canonical) also resolve correctly.

Verified end-to-end against ~/workspace/prql (origin=max-sixty/prql,
upstream=PRQL/prql) → 'PRQL', and against a fresh fork-only clone
(origin=max-sixty/prql, no upstream, no set-default) → 'PRQL'.

Six unit tests cover non-fork, single-level fork, chained fork,
empty-source fallback, no-gh, and api-failure-keeps-view paths.
The integration test asserts the rendered guard matches the
detected canonical owner end-to-end.

Co-Authored-By: Claude <[email protected]>
Three concurrent reviews (reuse / quality / efficiency) converged on
the same finding: `detect_canonical_owner` had four fallback paths,
two of which were dead defensive code, one of which silently shipped
the wrong owner string.

The hazardous one: when `gh repo view` returned a fork AND the
follow-up `gh api` call failed (rate limit / auth / network), the
function fell through to the view's owner — i.e., the fork's owner.
Workflows then shipped with `if: github.repository_owner == '<fork>'`
and silently no-op'd on canonical, the exact failure mode this whole
branch was created to prevent. The accompanying test pinned this as
"strictly better than no guard at all," which isn't true: a missing
guard at least lets the workflow run; a wrong owner string skips it
forever.

Restructure to a single API call and parse the response as JSON
(matching the rest of checks.py — every other check uses
`json.loads`, this was the only spot using a jq template + string
split). The API returns `.fork`, `.owner.login`, and
`.source.owner.login` in one body, so:

  - non-fork: return `.owner.login` (the only path)
  - fork: return `.source.owner.login` (the only path; `source` is
    always populated when `fork=true` per the GitHub API contract,
    and `source` is the *root* so chained forks resolve in one call)
  - any failure: return None, cli.init warns, guard omitted

Each branch now returns a definite value; no fall-through, no
silent wrong-answer mode. The "fork=true but source empty" and
"len(parts) == 2" defensive branches go away with the jq template.
The `"/" not in repo` defensive check goes away too — `detect_repo`
already returns None for empty/missing.

Same end-to-end behavior on the two real cases I tested:
- `~/workspace/prql` (origin=fork, upstream=PRQL, set-default=PRQL): 'PRQL'
- fresh fork-only clone (origin=fork, no upstream): 'PRQL'

Trimmed `test_init_renders_guard_with_detected_canonical_owner` to
check one workflow rather than reading 6 — `test_fork_guard_rendered_shape_regtest`
already snapshots the rendered shape per-file.

188 tests pass (was 189 — one obsolete dead-state test removed),
pre-commit clean.

Co-Authored-By: Claude <[email protected]>
Move the trailing `\n` to the front of `if_block` so the literal template
keeps natural indentation (`triage:{if_block}` plus a real `    runs-on:`
line) instead of `{if_line}    runs-on:` with the indent split across the
variable boundary.

Output is byte-identical — snapshots unchanged.

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 9dc88f4 into main May 3, 2026
5 checks passed
@max-sixty
max-sixty deleted the workflow-fork-guards branch May 3, 2026 01:41
@max-sixty max-sixty mentioned this pull request May 8, 2026
2 of 3 tasks
max-sixty added a commit that referenced this pull request May 8, 2026
## Summary

- Bumps generator version 0.0.16 → 0.0.17
- 57 commits since 0.0.16; the headline change is #375 (gate
fork-exposed scheduled workflows on `github.repository_owner` so
consumer-repo forks no-op on the cron and stop generating cross-repo
CheckSuite notifications back to `tend-agent`)
- Other notable changes: workflow header stamps tend version (#316),
bot-token rebound to `GITHUB_TOKEN` (#373), review skill
draft/light-mode + polling improvements (#308, #311, #315, #362)

## Test plan

- [x] `uv run pytest` — 188 passed
- [x] `pre-commit run --all-files` — clean
- [ ] After merge: tag 0.0.17, PyPI publishes, regenerate tend's own
workflows with `uvx tend@latest init`

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request May 8, 2026
## Summary

Regenerates `.github/workflows/tend-*.yaml` against the freshly
published 0.0.17. Brings tend's own deployment in line with what
consumers will see after their nightly regen:

- **Fork guard (#375)**: scheduled jobs now carry `if:
github.repository_owner == 'max-sixty'`, so forks of tend (or
fork-of-fork chains) no-op on the cron instead of failing on missing
secrets and pinging `tend-agent` with cross-repo CheckSuite
notifications.
- **Header (#316)**: workflow comment now reports the generator version
it was rendered from.
- **GITHUB_TOKEN rebind (#373)**: the bot token is now exported as
`GITHUB_TOKEN` (previously `GH_TOKEN`).

## Test plan

- [x] `uvx [email protected] init` produced a clean diff (8 files, +19/-16)
limited to header, `if:` guard, and the token env-var rename
- [ ] Watch tomorrow's scheduled runs to confirm the guard does not gate
out tend's own canonical workflow

Co-authored-by: Claude <[email protected]>
tend-agent added a commit that referenced this pull request May 10, 2026
Remove links to specific past PRs (#154, #212, #263, #203, #268, #392, #375, #305, #318) from review-reviewers and running-in-ci, restating each as a structural rule (what shape is accepted, what shape is not). Extend "Authoring skills" in the root CLAUDE.md with a parallel "No specific past-case references" bullet so future skill edits don't re-introduce them.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
max-sixty added a commit that referenced this pull request Sep 19, 2026
The README said nothing about forks. A fork carries the generated
workflows, and once Actions is on there, every scheduled tick files a
run in the fork's Actions tab. The fork guard skips the jobs, so these
runs cost no runner time and start no agent, but they still show up,
about 50 a day from `tend-notifications` alone. The new paragraph at the
end of **Workflows** covers what those runs are, that GitHub turns a
public repo's scheduled workflows off after 60 days without activity,
and how a fork's owner turns them off sooner.

The second commit corrects the fork guard's rationale in
`macros.yaml.j2`. It said `github.event.repository.fork` is null for
`schedule` events. #375 stated that without testing it, and GitHub's
[2022-09-27
changelog](https://github.blog/changelog/2022-09-27-github-actions-additional-information-available-in-github-event-payload-for-scheduled-workflow-runs/)
added repository information to scheduled runs. The comment now gives
the reason that still holds: the owner check reads the `github` context,
which carries the owner for every event. It's a Jinja comment, so no
generated output changes.

> _This was written by Claude Code on behalf of max-sixty_

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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.

2 participants