Skip to content

[WIP] Check repository permissions at plan time - #266

Draft
BigLep wants to merge 4 commits into
ipdxco:masterfrom
BigLep:validate-repository-permissions
Draft

BigLep wants to merge 4 commits into
ipdxco:masterfrom
BigLep:validate-repository-permissions

Conversation

@BigLep

@BigLep BigLep commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds a plan-time check that repository collaborator and team permissions in the YAML config are GitHub built-in roles (pull, triage, moderate, push, maintain, admin, per the team and collaborator REST docs) or custom repository roles declared in local.custom_repository_roles.

  • terraform/resources.tf: a precondition on github_repository_collaborator and github_team_repository. It is skipped when var.resources is set, which only the sync script's terraform import does. Grants on archived repositories come from Terraform state and are checked too; the message falls back to team_id there because state has no team attribute.
  • terraform/locals.tf: custom_repository_roles (default [], overridable in locals_override.tf), the resulting repository_permissions list, and hints mapping the GitHub UI names read → pull and write → push.
  • docs/EXAMPLE.yml: notes that keys are API permission names.
  • terraform/tests/: terraform test cases with a mocked GitHub provider, and run.sh, which runs each case against a scratch copy of the module with that case's YAML fixture (plus optional <case>.state.json and <case>_override.tf).
  • .github/workflows/test.yml: new Test workflow that runs terraform fmt -check and terraform/tests/run.sh on PRs and pushes that touch terraform/.
  • CHANGELOG.md: the new check and workflow, plus a BREAKING note for orgs that grant custom roles.

Why

The provider does not validate these permission values (custom role names are allowed), so a config using the UI role name planned cleanly and only failed with 422 Validation Failed on apply, after merge. This happened in FilOzone/github-mgmt#75 with teams: write:. With this change, the PR's plan fails instead (message shown under Validation).

Validation

terraform/tests/run.sh (run in CI by the new Test workflow) covers:

Case Expected
all six built-in permissions for collaborators and teams, plus collaborator and team grants on an archived repository read from state (team_id, no team) plan passes
collaborator under read, team under write both preconditions fail
custom role listed in custom_repository_roles plan passes
custom role not listed both preconditions fail

The suite fails against master (no check, so the expected failures are missing) and against this PR's first commit (moderate rejected, and Unsupported attribute "team" for the archived grant), so it catches both the original problem and the regression the review found.

Also checked by hand with Terraform 1.12.0 and the real provider (dummy token, -refresh=false): the error text below for PR #75's YAML; that a precondition inside lifecycle survives an *_override.tf that replaces the resource's lifecycle block; and that a bogus permission passed through var.resources skips the check. The sync script's terraform import path was exercised by a Codex review against a local GET-only mock of the GitHub API and succeeded.

repositories.foc-devnet.teams.write in github/FilOzone.yml: "write" (granted to
team "ozzyhelper") is not a repository permission this configuration allows.
Use "push" instead. Valid permissions: pull, triage, moderate, push,
maintain, admin. To use a custom repository role that exists in GitHub, add
it to local.custom_repository_roles in terraform/locals_override.tf.

Upgrade impact

Organizations that already grant custom repository roles in their YAML will see plan fail after upgrading until they list those roles in custom_repository_roles in locals_override.tf. Because the Plan workflow takes only github/ from the PR, a new custom role must be declared in a merged PR before a PR that grants it. The CHANGELOG says both.

Not covered

  • terraform fmt -check skips terraform/bootstrap/, whose aws.tf is already unformatted on master.
  • A resources_override.tf that sets permission directly on these resources bypasses the check; it validates the config value, not the provider argument.
  • github/.schema.json and the TypeScript config types still allow only the built-in permission keys, so custom roles are flagged by editors using the schema. This predates the PR.

GitHub only accepts pull, triage, push, maintain, admin, or a custom
repository role as a collaborator or team permission, and the provider
does not validate it. A config using the UI role name (e.g. write)
planned cleanly and failed with 422 on apply.

Add preconditions on github_repository_collaborator and
github_team_repository so plan fails with the YAML path, a hint for
read/write, and the list of valid permissions. Custom roles can be
declared in local.custom_repository_roles.
Team grants on archived repositories come from Terraform state, which
has team_id but no team attribute, so the error message expression
broke the plan. Fall back to team_id (and the resource key for
collaborators).

Add GitHub's built-in moderate role, reword the error so it does not
claim GitHub will reject a custom role that is merely undeclared, and
note the custom role upgrade step in the CHANGELOG.
terraform/tests/run.sh runs each tests/<case>.tftest.hcl against a
scratch copy of the module with that case's YAML fixture (and optional
state and override files) and a mocked GitHub provider. Cases cover
every built-in permission, grants on archived repositories read from
state, GitHub UI role names, and declared and undeclared custom roles.

The Test workflow runs terraform fmt -check and these tests on pull
requests and pushes that touch terraform/.
.gitignore excludes *.tfstate.*, so the fixture never reached CI.
run.sh now copies tests/fixtures/<case>.state.json into place.
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.

1 participant