Skip to content

ci: fail a PR that raises the go directive on a release branch - #4228

Open
rm3l wants to merge 7 commits into
redhat-developer:mainfrom
rm3l:ci/go-directive-policy
Open

rm3l wants to merge 7 commits into
redhat-developer:mainfrom
rm3l:ci/go-directive-policy

Conversation

@rm3l

@rm3l rm3l commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Description

A release branch's go directive in go.mod is frozen at branch cut: it is the branch's compatibility floor for every consumer and it selects the GODEBUG defaults compiled into the binary.

The same policy guard in rhdh-skills was defeated in practice: a base-image run from an installed copy of the rhdh-base-images skill that predated the guard raised go 1.26.0 → 1.27.0 on rhdh-must-gather release-2.1 (rhdh-must-gather#648), and the auto-approve workflow merged it. The same automation covers this repository, so the branch has to protect itself.

This adds hack/check-go-directive-policy.sh and a Go Directive Policy workflow that compares go.mod at the pull request base and head:

  • a raise fails the check with the revert instruction;
  • a revert is allowed, so a branch that already carries a raised directive can be repaired;
  • toolchain is not checked — it follows the go-toolset image on every branch;
  • a security fix that cannot land without the raise is allowed when the pull request carries the kind/security label, which Renovate adds to its vulnerability alerts. The label is the contract: without it the check fails, so accepting a raise is a deliberate reviewer action.

The check cannot be disabled by the pull request it judges

The workflow uses pull_request_target, so GitHub runs the workflow definition and hack/check-go-directive-policy.sh from the default branch, never from the pull request. The proposed head is read strictly as data: refs/pull/N/head is fetched into a private ref, go.mod is read with git show into a file, and the script compares two files without invoking git at all. Nothing from the pull request is checked out or executed. Permissions are contents: read only and no secrets are used.

pull_request_target also runs when the pull request's target branch does not carry this file, so the check covers a release branch as soon as main has it — no backports needed for release-2.1, release-1.10, or release-1.9.

PR acceptance criteria

  • Tests — fixture matrix below
  • Documentation — script header and workflow comments carry the policy, the exception contract, and the trust boundary

How to test changes / Special notes to the reviewer

# Replay a release-branch raise through the same path the workflow uses
git fetch --no-tags --depth=1 origin "+refs/heads/release-2.1:refs/policy/base" "+refs/pull/N/head:refs/policy/head"
git show refs/policy/base:go.mod > base.mod
git show refs/policy/head:go.mod > head.mod
./hack/check-go-directive-policy.sh --base-ref release-2.1 \
  --base-go-mod base.mod --head-go-mod head.mod; echo $?   # 1 on a raise

Verified locally against a fixture repository: raise on release-* fails; unchanged, revert, and toolchain-only change pass; main passes; a raise with kind/security passes; missing arguments exit 2. The fetch-and-read pipeline was exercised against real refs. shellcheck and actionlint are clean.

Still needed outside this PR: add Go Directive Policy to the required status checks for release-*. Without that, Prow/auto-merge still merges when the other required checks pass.

Assisted-by: OpenCode

A release branch's `go` directive is frozen at branch cut: it is the branch's
compatibility floor for every consumer and selects the GODEBUG defaults compiled
into the binary. A base-image run raised it on rhdh-must-gather release-2.1 from
a stale copy of the rhdh-base-images skill, and auto-merge landed it before
anyone noticed — skill-side guards cannot cover an installed copy that predates
them, so the branch has to protect itself.

The check compares go.mod at the pull request base and head. A raise fails,
with the revert instruction; a revert is allowed, so a branch that already
carries a raised directive can be repaired. `toolchain` is deliberately not
checked: it follows the go-toolset image on every branch.

A security fix that cannot land without the raise is the one exception, and the
contract is the `kind/security` label that Renovate adds to its vulnerability
alerts: without the label the check fails, so accepting the raise is a deliberate
reviewer action rather than an emergent side effect.

The script receives the base branch and decides whether the request is in scope,
so the check reports on every pull request instead of staying pending when a
paths filter does not match.

Adding the job to the branch protection rules for release-* is what makes it
block auto-merge.

Assisted-by: OpenCode
@rm3l
rm3l requested a review from a team as a code owner November 30, 2026 09:43
@rhdh-qodo-merge

rhdh-qodo-merge Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Block Go directive raises on release-branch PRs

✨ Enhancement ⚙️ Configuration changes 🕐 20-40 Minutes

Grey Divider

AI Description

• Add a PR check that rejects increases to a release branch’s go directive while allowing reverts
 and toolchain changes.
• Allow security exceptions through the kind/security label, making a raise an explicit reviewer
 decision.
• Run the check on every PR; backports and required-status-check configuration are still needed to
 block merges.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 0 capabilities

  • Fault-specific containment limits risk to the originating changed behavior.
Behavioral complexity 🟡 Medium 🧩 Interacting branches, Compatibility cases

  • Before: any base ref matching release-* reached release policy evaluation.
  • After: only exact numeric release names do, while retarget events and labeled raises still evaluate target branch and pin state.
  • New correctness conditions are exact branch-pattern compatibility, event-to-target alignment, pin-preservation precedence, and pinned versus unpinned bases; trusted ref pairing and cancellation remain existing interactions.
Sources

.github/workflows/go-directive-checks.yaml[30-33]

    # `edited` fires when the base branch is retargeted (e.g. main ->
    # release-2.1), so the check always judges the branch the pull request
    # ends up targeting, not the one it was opened against.
    types: [opened, synchronize, reopened, ready_for_review, labeled, unlabeled, edited]

hack/check-go-directive-policy.sh[133-139]

# Only release-<major>.<minor> counts (e.g. release-2.1, release-1.10). The
# automation parking branches that also carry the release- prefix, such as
# release-1.10-automated-base-images, match the release-* glob but are not
# release branches: whatever they carry is judged by the pull request that
# finally targets the release branch. Anything else — main, topic branches, the
# legacy X.Y.x branches — is out of scope.
if [[ ! "${BASE_REF}" =~ ^release-[0-9]+(\.[0-9]+)*$ ]]; then

hack/check-go-directive-policy.sh[161-167]

if [[ " ${LABELS} " == *" kind/security "* ]]; then
    # The label allows the raise, not a semantics change: when the base carries
    # a godebug default= pin, the raise must keep it unchanged, because that
    # pin is what preserves the branch-cut GODEBUG defaults across the raise.
    base_godebug=$(godebug_default "${BASE_GO_MOD}")
    head_godebug=$(godebug_default "${HEAD_GO_MOD}")
    if [[ -n "${base_godebug}" && "${head_godebug}" != "${base_godebug}" ]]; then
Potential damage 🟡 Medium 🩹 Incorrect results

  • Conservative damage bound for the originating changed behavior; downstream capabilities remain contained.
  • A wrong retarget, exact-branch, or GODEBUG-pin decision can block a valid release PR or allow an incompatible labeled raise.
  • The trusted-script boundary contains code substitution, not incorrect policy inputs or decisions.
  • A blocked decision can be corrected by changing the pull request or policy inputs.
  • If an incorrect allow permits a directive or GODEBUG-pin change to merge, reverting the policy stops future decisions, but the merged change may require a separate revert or compatibility review.
Sources

.github/workflows/go-directive-checks.yaml[30-33]

    # `edited` fires when the base branch is retargeted (e.g. main ->
    # release-2.1), so the check always judges the branch the pull request
    # ends up targeting, not the one it was opened against.
    types: [opened, synchronize, reopened, ready_for_review, labeled, unlabeled, edited]

hack/check-go-directive-policy.sh[133-139]

# Only release-<major>.<minor> counts (e.g. release-2.1, release-1.10). The
# automation parking branches that also carry the release- prefix, such as
# release-1.10-automated-base-images, match the release-* glob but are not
# release branches: whatever they carry is judged by the pull request that
# finally targets the release branch. Anything else — main, topic branches, the
# legacy X.Y.x branches — is out of scope.
if [[ ! "${BASE_REF}" =~ ^release-[0-9]+(\.[0-9]+)*$ ]]; then

hack/check-go-directive-policy.sh[161-167]

if [[ " ${LABELS} " == *" kind/security "* ]]; then
    # The label allows the raise, not a semantics change: when the base carries
    # a godebug default= pin, the raise must keep it unchanged, because that
    # pin is what preserves the branch-cut GODEBUG defaults across the raise.
    base_godebug=$(godebug_default "${BASE_GO_MOD}")
    head_godebug=$(godebug_default "${HEAD_GO_MOD}")
    if [[ -n "${base_godebug}" && "${head_godebug}" != "${base_godebug}" ]]; then
Assessed commit e8e361b
Diagram

graph TD
  PR["Pull request"] --> WF["Policy workflow"] --> SCOPE{"Release branch?"} -->|Yes| MOD["Compare go.mod"] --> RAISE{"Directive raised?"} -->|Yes| SECURITY{"Security label?"} -->|No| FAIL["Fail check"]
  SCOPE -->|No| PASS["Pass check"]
  RAISE -->|No| PASS
  SECURITY -->|Yes| PASS
Loading
High-Level Assessment

A repository-owned PR check is appropriate: the existing skill-side guard can be absent from older installed copies, while a paths-filtered workflow could leave a required check pending. Backport the workflow to active release branches and make its status check required; without both steps, this PR does not prevent auto-merges.

Files changed (2) +201 / -0

Enhancement (1) +149 / -0
check-go-directive-policy.shReject unapproved Go directive raises on release branches +149/-0

Reject unapproved Go directive raises on release branches

• Compares 'go.mod' directives at the PR base and head, failing a release-branch raise unless the PR has 'kind/security'. Non-release branches, unchanged directives, reverts, and toolchain-only changes pass; failures explain the policy and remedy.

hack/check-go-directive-policy.sh

Other (1) +52 / -0
go-directive-checks.yamlRun the Go directive policy check on every PR +52/-0

Run the Go directive policy check on every PR

• Adds a GitHub Actions workflow that fetches both commits and passes the PR’s base branch, commit SHAs, and labels to the policy script. It reruns on label changes and omits path filters so the check reports for every PR.

.github/workflows/go-directive-checks.yaml

@rhdh-qodo-merge

rhdh-qodo-merge Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Code Review by Qodo


View low (1)
Informational
1. Release-branch scope accepts deeper version names 🐞 Bug ⭐ New
Description
The scope regex ^release-[0-9]+(\.[0-9]+)*$ in go_directive's caller accepts any number of dot
components, so names like release-2 or release-2.1.3 are judged as release branches, while the
comment and commit message state the scope is exactly release-<major>.<minor>. A branch with a
patch-level or single-number suffix would be enforced even though the documented contract excludes
it, and a reader trusting the comment would misjudge which branches the guard covers.
Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The release-branch scope check in `hack/check-go-directive-policy.sh` uses `^release-[0-9]+(\.[0-9]+)*$`, which accepts names like `release-2` and `release-2.1.3`. The comment above it and the commit message say the scope is exactly `release-<major>.<minor>`, so the code and its documentation disagree.

## Fix Focus Areas
- hack/check-go-directive-policy.sh[133-139]

## Recommended Fix
Decide which behavior is intended. If only major.minor should be judged, change the regex on line 139 to `^release-[0-9]+\.[0-9]+$`. If deeper names should also be judged, update the comment on lines 133-138 and the header comment on lines 27-31 to say so. Either way, add fixture cases for `release-2`, `release-2.1`, and `release-2.1.3` to the test matrix so the scope is pinned.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


@rm3l

rm3l commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

/cherrypick release-2.1
/cherrypick release-1.10

@openshift-cherrypick-robot

Copy link
Copy Markdown
Contributor

@rm3l: once the present PR merges, I will cherry-pick it on top of release-1.10, release-2.1 in new PRs and assign them to you.

Details

In response to this:

/cherrypick release-2.1
/cherrypick release-1.10

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@rhdh-qodo-merge

Copy link
Copy Markdown

Important

The /generate_labels command by Qodo was deprecated on the 1st of October 2026 and is no longer available. We recommend switching to the latest Qodo review capabilities. Learn more

Review finding: a pull request could change the workflow or the script that
judges its own go directive, and pass.

Move the check to pull_request_target, which runs the workflow definition and
the script from the default branch rather than the pull request's merge commit.
The proposed head is now read strictly as data: its go.mod is fetched into a
file with `git show` and handed to the script, and nothing from the pull request
is checked out or executed. Permissions stay read-only and no secrets are used.

The script no longer touches git at all — it compares two files — so the trust
boundary is explicit: trusted code, untrusted data.

pull_request_target also runs for pull requests whose target branch does not
carry this file, so a release branch is covered as soon as main has it.

Assisted-by: OpenCode
Sonar gate S7631 ("Forked repository code should not be checked out in
privileged workflow contexts") flags any actions/checkout in a
pull_request_target workflow, whatever its ref. Drop it.

The job now initialises a scratch repository, fetches the default branch, the
target branch and the pull request head into private refs, reads the policy
script from the default-branch ref with `git show`, and reads go.mod from each
side as data. No tree is ever checked out, so the warning and the pattern it
guards against are both gone while the trust boundary is unchanged.

Assisted-by: OpenCode
Comment thread hack/check-go-directive-policy.sh Outdated
return 1
}

if [[ "${BASE_REF}" != release-* ]]; then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should this be explicitly for main rather than anything but a release-* branch? wondering if this will impact PRs on topic branches (and if that's good or bad).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

--base-ref is the target branch, so a topic branch PR only reaches this comparison if it targets a release branch. A PR from any topic branch against main is out of scope, which is intended per the policy.
That said, I realized that release-* alone seems too broad. I've updated the condition accordingly. Thanks.

exit 0
fi

if [[ " ${LABELS} " == *" kind/security "* ]]; then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is a cool nuance/rationale/workaround. wasn't aware of this rule.

@rm3l rm3l Nov 30, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah, had to clarify this recently on a security PR against release-1.10. The go directive isn't just a minimum toolchain version; it also selects some of the default settings that get compiled into the binary, so raising it on a release branch can silently change runtime behavior.

rm3l and others added 4 commits November 30, 2026 20:37
The trusted-ref copy is written to ${WORK}/check-go-directive-policy.sh so
the invocation in the next step matches the path in the repository rather
than a generic check.sh.

Document that --base-ref is the branch the pull request targets, so a pull
request against main or a topic branch is out of scope, and that a raise
parked on an intermediate branch is still caught by the pull request that
finally targets the release branch.

Co-authored-by: Nick Boldt <nickboldt@gmail.com>
Assisted-by: OpenCode
Address the remaining review findings from the Qodo pass:

- a go.mod path that cannot be read now exits 2 instead of passing as
  "no go directive"; the workflow always writes both files (empty when a
  side has no Go module), so an unreadable file means the invocation is
  wrong, not that a side predates the module;
- the workflow also runs on `edited`, so a pull request retargeted from
  main to a release branch is judged against the branch it ends up
  targeting rather than the one it was opened against;
- a kind/security-labeled raise must keep the base branch's
  `godebug default=` pin unchanged when the base carries one, because
  that pin is what preserves the branch-cut GODEBUG defaults across the
  raise; a base without a pin, a branch cut before the pin existed, has
  nothing to preserve and is not required to grow one.

Assisted-by: OpenCode
The release-* glob also matched automation parking branches such as
release-1.10-automated-base-images, which are intermediate branches, not
protected release branches — a pull request targeting one would have been
judged as if it targeted the release branch itself, and could have failed
the very base-image automation this check exists to guard.

The scope is now release-<major>.<minor> only (release-2.1, release-1.10),
matched on the whole branch name. Parking branches, topic branches, the
legacy X.Y.x branches, and main are out of scope: whatever they carry is
judged by the pull request that finally targets the release branch.

Co-authored-by: Nick Boldt <nickboldt@gmail.com>
Assisted-by: OpenCode
@sonarqubecloud

Copy link
Copy Markdown

@rm3l
rm3l requested a review from nickboldt November 30, 2026 20:10
@rm3l rm3l changed the title ci: fail a PR that raises the go directive on a release branch ci: fail a PR that raises the go directive on a release branch Nov 30, 2026

This branch has not been deployed

No deployments
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.

4 participants