Skip to content

feat(gh): replace branch-protection audit with read-only ruleset check #7756

Backlog

Description

@devops-cli-service

Split from #5237.

Problem: devops gh branch-protection audits the classic branch protection API. The live main ruleset holds the 9 rules and the 3 required checks. The classic audit does not read the ruleset. Issue #5237 names a separate issue for the replacement. Why deferred: The replacement needs code, tests and a read-only GitHub API or OpenTofu path. It does not fit in the docs-only ADR pull request of #5237. Owner decision 3 of 2026-11-12 binds the required checks to the GitHub Actions app. The read must not change a setting. Acceptance criteria: The command reads the main ruleset with a read-only GitHub API call or a read-only OpenTofu github_repository_ruleset plan. The command reports the 9 rules, the 3 required checks and their app binding. The command exits non-zero when the ruleset differs from the expected state. The command makes no write request. A dry run makes no external request. Tests cover the expected ruleset, a missing rule, a missing required check and a wrong app binding. uv run devops ci --check --only docs and the test row pass.

Proposed design

Problem statement

The devops gh branch-protection audit command checks the classic branch protection API. It does not read the main ruleset that holds the rules and the 3 required checks. The replacement must read the ruleset with a read-only OpenTofu plan, report the rules and the app-bound checks, and exit non-zero on drift.

Acceptance criteria

Requirement: Read-only ruleset plan

The devops gh branch-protection audit command SHALL read the main ruleset with tofu plan -detailed-exitcode -lock=false -out=<file> and tofu show -json <file>, use the read-only devops-settings-reader token, and MUST NOT run tofu apply.

Scenario: Plan without apply

  • WHEN a user runs devops gh branch-protection audit --stack <dir> on an initialized settings stack
  • THEN the process fake records tofu plan with -detailed-exitcode and -lock=false, records tofu show -json, receives the reader token, and records no tofu apply.

Requirement: Ruleset report

The command SHALL report each rule of the main ruleset and each required check with its app binding.

Scenario: Expected ruleset

  • WHEN the saved plan JSON holds the main ruleset with nine rules and three required checks bound to integration ID 15368
  • THEN the command prints the nine rule names and the three check contexts with 15368.

Requirement: Missing rule

The command SHALL exit non-zero when the main ruleset lacks a rule that the settings code expects.

Scenario: Missing rule

  • WHEN the saved plan JSON shows the live ruleset without one expected rule
  • THEN the command exits non-zero and names the missing rule.

Requirement: Missing required check

The command SHALL exit non-zero when the main ruleset lacks a required check that the settings code expects.

Scenario: Missing required check

  • WHEN the saved plan JSON shows the live ruleset without one expected required check
  • THEN the command exits non-zero and names the missing check.

Requirement: Wrong app binding

The command SHALL exit non-zero when a required check binds to an integration ID other than the GitHub Actions app ID 15368.

Scenario: Wrong app binding

  • WHEN the saved plan JSON shows a required check with an integration ID other than 15368
  • THEN the command exits non-zero and names the check and the wrong integration ID.

Requirement: Plan drift

The command SHALL exit non-zero when the plan reports any change to the main ruleset.

Scenario: Plan drift

  • WHEN the tofu plan exit code is 2
  • THEN the command exits non-zero and prints the planned changes.

Requirement: Dry run

A dry run MUST make no external request and start no process.

Scenario: Dry run

  • WHEN a user runs devops gh branch-protection audit --dry-run
  • THEN the command prints the planned tofu plan command and starts no OpenTofu or GitHub process.

Requirement: Docs and test checks

The change SHALL pass uv run devops ci --check --only docs and the test row.

Scenario: CI checks

  • WHEN the pull request runs uv run devops ci --check --only docs and the test row
  • THEN both checks pass.

Existing Tool Considered

No alternatives check ran before this revision, because alternatives is null. OpenTofu with the integrations/github provider and the github_repository_ruleset resource covers the read-only plan, the exit-2 drift signal and the integration_id app binding (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_ruleset). It lacks a devops gh entry point and a report that names each rule and each required check with its app binding. The command adds that report and runs no apply.

Key questions

  • Which source of truth should the read-only ruleset check use?: Use the OpenTofu settings code and a read-only tofu plan of github_repository_ruleset. Do not keep a second expected ruleset in Python. (best practice) [sources: docs/adr/0043-required-checks-and-main-ruleset-gate-merges.md:36, docs/adr/0026-github-settings-are-opentofu-code-the-owner-applies.md:33, docs/adr/0041-opentofu-holds-the-repository-settings.md:58, openspec/changes/7497-onboard-settings-opentofu/design.md:69]
    • Chosen: OpenTofu plan. The accepted ADR 0026 gives the settings to OpenTofu, ADR 0043 names a ruleset/OpenTofu read, and a plan reports a removed required check.
    • Rejected: Python expected-ruleset copy. It duplicates the OpenTofu settings and drifts from them.
    • Rejected: GitHub API with hard-coded rules. It duplicates the expected state and adds a second comparison path.
  • How should the command obtain structured rule and check data from the plan?: Run tofu plan -detailed-exitcode -lock=false -out=<file> with the refresh on, then run tofu show -json <file> and read the github_repository_ruleset values. Reuse the pattern of read_plan_drift, but do not pass -refresh-only. (best practice) [sources: src/devops_cli/tf/analysis.py:466, src/devops_cli/tf/analysis.py:474, src/devops_cli/tf/analysis.py:496, openspec/specs/tf-drift/spec.md:11, docs/adr/0041-opentofu-holds-the-repository-settings.md:92, https://opentofu.org/docs/cli/commands/plan/]
    • Chosen: Saved plan and tofu show -json. The JSON plan gives the live before values and the expected after values, and the plan makes no change.
    • Rejected: Parse the plan text. Text parsing is fragile and misses the nested app binding.
    • Rejected: Read the GitHub API directly. It duplicates the OpenTofu expected state and adds a second comparison path.
  • Must the check assert that each required check binds to the GitHub Actions app?: Yes. Compare each required_check.integration_id with the GitHub Actions app ID 15368. (best practice) [sources: docs/adr/0043-required-checks-and-main-ruleset-gate-merges.md:42, https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_ruleset, https://stackoverflow.com/questions/75063703/github-api-find-status-of-required-checks-on-a-pull-request-towards-protected-b, tests/support/github_repository.py:381, src/devops_cli/github/onboard/settings_rows.py:689]
    • Chosen: Assert the app binding. Owner decision 3 of 2026-11-12 binds the checks to the GitHub Actions app, and the provider holds integration_id.
    • Rejected: Check the context only. A check with the right name but another app would pass.
  • How should the command find the OpenTofu settings stack that holds the main ruleset?: Take a --stack path to a checkout of the root, as devops tf drift takes a directory. The public root is tf/github/dan-petty of dan-petty/infra and the private root is tf/github/dan-petty-private of dan-petty/homelab. (best practice) [sources: docs/adr/0041-opentofu-holds-the-repository-settings.md:50, docs/adr/0041-opentofu-holds-the-repository-settings.md:51, docs/commands/tf.md:317, openspec/changes/7497-onboard-settings-opentofu/tasks.md:90]
    • Chosen: --stack directory. The roots live in other repositories, and devops tf drift already takes a directory argument.
    • Rejected: Classic --policy-file YAML. ADR 0004 removed the YAML, and it is a second copy of the settings.
    • Rejected: Read the GitHub API with no stack. It bypasses the OpenTofu source of truth that ADR 0043 names.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/feature
    source/agent
    status/new
    value/medium
    effort/medium
    scope/gh
    on Nov 29, 2026
  2. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    ContributorAuthor
  3. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    ContributorAuthor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/ghsource/agentstatus/newtype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions