# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/github/delphi-dapd-apollo-sync-finalizer/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "github_repositories" {
  source = "git@github.com:theorchard/terraform-github.git//?ref=2.1.4"
  github_repository_names = {
    delphi-dapd-apollo-sync-finalizer = {
      branch_protection_enabled        = true
      branch_protection_enforce_admins = true
      repo_auto_init                   = true
      archived_repository              = false
      github_code_owner_reviews        = false
      visibility                       = "private"
      description                      = "Managed by Terraform."
      has_downloads                    = false
      homepage_url                     = ""
    }
  }

  github_push_teams  = ["delphi-team", "mergers", "delphi-be-team", "delphi-python-team"]

  allow_merge_commit = true

  branch_protection_required_status_checks = true
  github_code_owner_reviews                = false
}

resource "github_branch_protection" "branch_protection" {
  # checkov:skip=CKV_GIT_5:We required only one reviewer in the established workflow.
  # checkov:skip=CKV_GIT_6:We don't require Git commits to be GPG signed.
  for_each = toset(["develop", "release"])

  repository_id  = module.github_repositories.github_repository_node_id["delphi-dapd-apollo-sync-finalizer"]
  pattern        = each.key
  enforce_admins = true

  required_status_checks {
    strict = true
  }

  required_pull_request_reviews {
    dismiss_stale_reviews      = true
    require_code_owner_reviews = false
  }
}
