Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test

on:
pull_request:
branches: [master]
paths:
- 'terraform/**'
- '.github/workflows/test.yml'
push:
branches: [master]
paths:
- 'terraform/**'
- '.github/workflows/test.yml'
workflow_dispatch:

defaults:
run:
shell: bash

concurrency:
group: test-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
terraform:
permissions:
contents: read
name: Terraform
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- name: Setup terraform
uses: hashicorp/setup-terraform@5e8dbf3c6d9deaf4193ca7a8fb23f2ac83bb6c85 # v4.0.0
with:
terraform_version: 1.12.0
terraform_wrapper: false
- name: Check terraform formatting
run: terraform fmt -check terraform terraform/tests terraform/tests/fixtures
- name: Run terraform tests
run: terraform/tests/run.sh
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- shared action for adding a label to all repositories
- issue_label support
- new args for repositories and branch protection rules
- plan-time check that repository collaborator and team permissions are GitHub built-in roles (`pull`, `triage`, `moderate`, `push`, `maintain`, `admin`) or custom roles listed in `custom_repository_roles`, with a hint when a GitHub UI role name (`read`, `write`) is used
- Test workflow that runs `terraform fmt -check` and the Terraform tests in `terraform/tests` (with a mocked GitHub provider) on changes to `terraform/`

### Changed
- **BREAKING**: if your YAML grants custom repository roles to collaborators or teams, list them in `custom_repository_roles` in `terraform/locals_override.tf` before upgrading, or plans will fail (the Plan workflow uses the base branch's Terraform files, so merge the `locals_override.tf` change before any PR that grants a new custom role)
- workflows: added separate GitHub Actions environments for reading organization state, writing organization state, and pushing repository changes
- workflows: pin third-party actions to latest release SHAs and replan from the merged commit before applying
- docs: update template repository references from `github-mgmt-template` to `github-as-code`
Expand Down
4 changes: 2 additions & 2 deletions docs/EXAMPLE.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ repositories: # This group defines repositories (https://registry.terraform.io/p
contexts:
- Comment
strict: true
collaborators: # This group defines repository collaborators (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_collaborator)
collaborators: # This group defines repository collaborators (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_collaborator); keys are API permission names, so use pull for Read and push for Write
admin: # This array defines repository collaborators with permission=admin
- peter
maintain: # This array defines repository collaborators with permission=maintain
- adam
push: [] # This array defines repository collaborators with permission=push
triage: [] # This array defines repository collaborators with permission=triage
pull: [] # This array defines repository collaborators with permission=pull
teams: # This group defines teams with access to the repository (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository)
teams: # This group defines teams with access to the repository (https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team_repository); keys are API permission names, so use pull for Read and push for Write
admin: [] # This array defines teams with permission=admin
maintain: [] # This array defines teams with permission=maintain
push: # This array defines teams with permission=push
Expand Down
9 changes: 9 additions & 0 deletions terraform/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ locals {
advanced_security = false
config = yamldecode(file("${path.module}/../github/${local.organization}.yml"))
state = jsondecode(file("${path.module}/${local.organization}.tfstate.json"))
# Custom repository roles already created in the GitHub organization that collaborators and teams can be granted, in addition to GitHub's built-in roles.
custom_repository_roles = []
# GitHub's built-in repository roles by API name (Read is pull, Write is push), followed by the custom ones.
repository_permissions = concat(["pull", "triage", "moderate", "push", "maintain", "admin"], local.custom_repository_roles)
# Role names from the GitHub UI that people often write instead of the API name.
repository_permission_hints = {
"read" = "pull"
"write" = "push"
}
ignore = {
"repositories" = []
"teams" = []
Expand Down
12 changes: 12 additions & 0 deletions terraform/resources.tf
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ resource "github_repository_collaborator" "this" {

lifecycle {
ignore_changes = []

# var.resources is only set by the sync script's terraform import, which has no YAML permission to check.
precondition {
condition = var.resources != null || contains(local.repository_permissions, each.value.permission)
error_message = "repositories.${each.value.repository}.collaborators.${each.value.permission} in github/${local.organization}.yml: \"${each.value.permission}\" (granted to collaborator \"${try(each.value.username, each.key)}\") is not a repository permission this configuration allows.${contains(keys(local.repository_permission_hints), each.value.permission) ? " Use \"${local.repository_permission_hints[each.value.permission]}\" instead." : ""} Valid permissions: ${join(", ", local.repository_permissions)}. To use a custom repository role that exists in GitHub, add it to local.custom_repository_roles in terraform/locals_override.tf."
}
}
}

Expand Down Expand Up @@ -188,6 +194,12 @@ resource "github_team_repository" "this" {

lifecycle {
ignore_changes = []

# var.resources is only set by the sync script's terraform import, which has no YAML permission to check.
precondition {
condition = var.resources != null || contains(local.repository_permissions, each.value.permission)
error_message = "repositories.${each.value.repository}.teams.${each.value.permission} in github/${local.organization}.yml: \"${each.value.permission}\" (granted to team \"${try(each.value.team, each.value.team_id)}\") is not a repository permission this configuration allows.${contains(keys(local.repository_permission_hints), each.value.permission) ? " Use \"${local.repository_permission_hints[each.value.permission]}\" instead." : ""} Valid permissions: ${join(", ", local.repository_permissions)}. To use a custom repository role that exists in GitHub, add it to local.custom_repository_roles in terraform/locals_override.tf."
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repositories:
demo:
collaborators:
security-auditor: [alice]
teams:
security-auditor: [auditors]
teams:
auditors: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
custom_repository_roles = ["security-auditor"]
}
8 changes: 8 additions & 0 deletions terraform/tests/fixtures/repository_permissions_ui_names.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repositories:
demo:
collaborators:
read: [alice]
teams:
write: [developers]
teams:
developers: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repositories:
demo:
collaborators:
security-auditor: [alice]
teams:
security-auditor: [auditors]
teams:
auditors: {}
32 changes: 32 additions & 0 deletions terraform/tests/fixtures/repository_permissions_valid.state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"values": {
"root_module": {
"resources": [
{
"address": "github_repository.this[\"old\"]",
"mode": "managed",
"type": "github_repository",
"name": "this",
"index": "old",
"values": {"name": "old", "archived": true}
},
{
"address": "github_repository_collaborator.this[\"old:grace\"]",
"mode": "managed",
"type": "github_repository_collaborator",
"name": "this",
"index": "old:grace",
"values": {"repository": "old", "username": "grace", "permission": "push"}
},
{
"address": "github_team_repository.this[\"developers:old\"]",
"mode": "managed",
"type": "github_team_repository",
"name": "this",
"index": "developers:old",
"values": {"repository": "old", "team_id": "123", "permission": "push", "etag": ""}
}
]
}
}
}
25 changes: 25 additions & 0 deletions terraform/tests/fixtures/repository_permissions_valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
repositories:
active:
collaborators:
admin: [alice]
maintain: [bob]
moderate: [carol]
push: [dave]
triage: [erin]
pull: [frank]
teams:
admin: [admins]
maintain: [maintainers]
moderate: [moderators]
push: [developers]
triage: [triagers]
pull: [readers]
old:
archived: true
teams:
admins: {}
maintainers: {}
moderators: {}
developers: {}
triagers: {}
readers: {}
7 changes: 7 additions & 0 deletions terraform/tests/repository_permissions_custom_role.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# A custom repository role listed in local.custom_repository_roles plans cleanly.

mock_provider "github" {}

run "accepts_declared_custom_role" {
command = plan
}
12 changes: 12 additions & 0 deletions terraform/tests/repository_permissions_ui_names.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# GitHub UI role names (read, write) are not API permissions, so plan fails.

mock_provider "github" {}

run "rejects_ui_role_names" {
command = plan

expect_failures = [
github_repository_collaborator.this,
github_team_repository.this,
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# A custom repository role missing from local.custom_repository_roles fails plan.

mock_provider "github" {}

run "rejects_undeclared_custom_role" {
command = plan

expect_failures = [
github_repository_collaborator.this,
github_team_repository.this,
]
}
18 changes: 18 additions & 0 deletions terraform/tests/repository_permissions_valid.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Built-in permissions in the YAML, plus grants on an archived repository that
# come from Terraform state (which has team_id but no team name), plan cleanly.

mock_provider "github" {}

run "accepts_builtin_permissions" {
command = plan

assert {
condition = length(github_repository_collaborator.this) == 7
error_message = "Expected 6 collaborators from the YAML and 1 from the archived repository's state."
}

assert {
condition = length(github_team_repository.this) == 7
error_message = "Expected 6 team grants from the YAML and 1 from the archived repository's state."
}
}
35 changes: 35 additions & 0 deletions terraform/tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# Runs each tests/<case>.tftest.hcl against a scratch copy of this module whose
# github/<case>.yml comes from tests/fixtures/<case>.yml. A case can also
# provide tests/fixtures/<case>.state.json (state the config reads, such as
# grants on archived repositories) and tests/fixtures/<case>_override.tf
# (copied into the module, such as extra locals). The GitHub provider is
# mocked, so no credentials or network access to GitHub are needed.
set -euo pipefail

module="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT

mkdir -p "$work/github" "$work/terraform"
cp "$module"/*.tf "$work/terraform/"
cp -R "$module/tests" "$work/terraform/tests"
cd "$work/terraform"
terraform init -backend=false -input=false -no-color > /dev/null

status=0
for test in tests/*.tftest.hcl; do
name="$(basename "$test" .tftest.hcl)"
cp "tests/fixtures/$name.yml" "../github/$name.yml"
if [[ -f "tests/fixtures/$name.state.json" ]]; then
cp "tests/fixtures/$name.state.json" "$name.tfstate.json"
else
echo '{}' > "$name.tfstate.json"
fi
rm -f zz_case_override.tf
if [[ -f "tests/fixtures/${name}_override.tf" ]]; then
cp "tests/fixtures/${name}_override.tf" zz_case_override.tf
fi
TF_WORKSPACE="$name" terraform test -no-color -filter="$test" || status=1
done
exit "$status"