Skip to content

Commit b494316

Browse files
committed
feat: add github repository, rulesets, and branch protection rules
1 parent 40d24a0 commit b494316

27 files changed

+1200
-0
lines changed

github/branch-protection/.terraform.lock.hcl

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugin "terraform" {
2+
enabled = true
3+
preset = "recommended"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform 1.3.1

github/branch-protection/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# GitHub repository branch protection rules
2+
3+
Add branch protection rule to your GitHub repository
4+
5+
## Authentication
6+
7+
- you must create a github app or a classic token with admin rights
8+
- then add the following in your provider configuration :
9+
10+
reference: https://registry.terraform.io/providers/integrations/github/latest/docs
11+
12+
### using an app (recommended)
13+
14+
```hcl
15+
provider "github" {
16+
owner = var.github_organization
17+
# set one of GITHUB_APP_ID, GITHUB_APP_INSTALLATION_ID, GITHUB_APP_PEM_FILE env var
18+
app_auth {}
19+
}
20+
```
21+
22+
### using a github token (classic)
23+
24+
```hcl
25+
provider "github" {
26+
owner = var.github_organization
27+
# and set GITHUB_TOKEN env var in your shell
28+
}
29+
```
30+
31+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
32+
## Requirements
33+
34+
| Name | Version |
35+
|------|---------|
36+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.3 |
37+
| <a name="requirement_github"></a> [github](#requirement\_github) | ~> 6.0 |
38+
| <a name="requirement_time"></a> [time](#requirement\_time) | ~> 0.9.1 |
39+
40+
## Providers
41+
42+
| Name | Version |
43+
|------|---------|
44+
| <a name="provider_github"></a> [github](#provider\_github) | ~> 6.0 |
45+
| <a name="provider_time"></a> [time](#provider\_time) | ~> 0.9.1 |
46+
47+
## Modules
48+
49+
No modules.
50+
51+
## Resources
52+
53+
| Name | Type |
54+
|------|------|
55+
| [github_branch_protection.main](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_protection) | resource |
56+
| [time_static.last_update](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/static) | resource |
57+
58+
## Inputs
59+
60+
| Name | Description | Type | Default | Required |
61+
|------|-------------|------|---------|:--------:|
62+
| <a name="input_allows_deletions"></a> [allows\_deletions](#input\_allows\_deletions) | Allow branch deletions | `bool` | `false` | no |
63+
| <a name="input_allows_force_pushes"></a> [allows\_force\_pushes](#input\_allows\_force\_pushes) | Allow force pushes | `bool` | `false` | no |
64+
| <a name="input_branch_pattern"></a> [branch\_pattern](#input\_branch\_pattern) | Branch name pattern to protect | `string` | `"main"` | no |
65+
| <a name="input_customer"></a> [customer](#input\_customer) | Customer for the current deployment | `string` | `""` | no |
66+
| <a name="input_enforce_admins"></a> [enforce\_admins](#input\_enforce\_admins) | Enforce required status checks for repository administrators | `bool` | `true` | no |
67+
| <a name="input_environment"></a> [environment](#input\_environment) | Environment for the current deployment | `string` | `""` | no |
68+
| <a name="input_force_push_bypassers"></a> [force\_push\_bypassers](#input\_force\_push\_bypassers) | List of actor IDs that can bypass force push restrictions | `any` | `[]` | no |
69+
| <a name="input_lock_branch"></a> [lock\_branch](#input\_lock\_branch) | Lock the branch | `bool` | `false` | no |
70+
| <a name="input_name"></a> [name](#input\_name) | The name of the branch protection rule | `string` | n/a | yes |
71+
| <a name="input_repository_id"></a> [repository\_id](#input\_repository\_id) | Name or id of the GitHub repository to protect | `string` | n/a | yes |
72+
| <a name="input_require_conversation_resolution"></a> [require\_conversation\_resolution](#input\_require\_conversation\_resolution) | Require conversation resolution before merging | `bool` | `true` | no |
73+
| <a name="input_require_signed_commits"></a> [require\_signed\_commits](#input\_require\_signed\_commits) | Require commits to be signed | `bool` | `true` | no |
74+
| <a name="input_required_linear_history"></a> [required\_linear\_history](#input\_required\_linear\_history) | Enforce a linear commit history | `bool` | `true` | no |
75+
| <a name="input_required_pull_request_reviews"></a> [required\_pull\_request\_reviews](#input\_required\_pull\_request\_reviews) | Require pull request reviews before merging | `any` | `{}` | no |
76+
| <a name="input_required_status_checks"></a> [required\_status\_checks](#input\_required\_status\_checks) | Require status checks to pass before merging | `any` | `{}` | no |
77+
| <a name="input_restrict_pushes"></a> [restrict\_pushes](#input\_restrict\_pushes) | Restrict who can push to the branch | `any` | `{}` | no |
78+
| <a name="input_tags"></a> [tags](#input\_tags) | Default tags to add to resources | `map(any)` | `{}` | no |
79+
80+
## Outputs
81+
82+
No outputs.
83+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource "github_branch_protection" "main" {
2+
#checkov:skip=CKV_GIT_5:invalid - it's up to the user choice
3+
repository_id = var.repository_id
4+
pattern = var.branch_pattern
5+
enforce_admins = var.enforce_admins
6+
require_signed_commits = var.require_signed_commits
7+
required_linear_history = var.required_linear_history
8+
require_conversation_resolution = var.require_conversation_resolution
9+
allows_deletions = var.allows_deletions
10+
allows_force_pushes = var.allows_force_pushes
11+
lock_branch = var.lock_branch
12+
force_push_bypassers = var.force_push_bypassers
13+
14+
dynamic "required_status_checks" {
15+
for_each = var.required_status_checks != {} ? [var.required_status_checks] : []
16+
content {
17+
strict = required_status_checks.strict
18+
contexts = required_status_checks.contexts
19+
}
20+
}
21+
22+
dynamic "required_pull_request_reviews" {
23+
for_each = var.required_pull_request_reviews != {} ? [var.required_pull_request_reviews] : []
24+
content {
25+
dismiss_stale_reviews = required_pull_request_reviews.dismiss_stale_reviews
26+
restrict_dismissals = required_pull_request_reviews.restrict_dismissals
27+
dismissal_restrictions = required_pull_request_reviews.dismissal_restrictions
28+
}
29+
}
30+
31+
dynamic "restrict_pushes" {
32+
for_each = var.restrict_pushes != {} ? [var.restrict_pushes] : []
33+
content {
34+
push_allowances = restrict_pushes.push_allowances
35+
}
36+
}
37+
}

github/branch-protection/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
locals {
2+
# tflint-ignore: terraform_unused_declarations
3+
interpolated_tags = merge({
4+
"Name" = var.name,
5+
"Customer" = var.customer,
6+
"Environment" = var.environment,
7+
"ManagedBy" = "Terraform",
8+
"LastModifiedAt" = time_static.last_update.rfc3339,
9+
},
10+
var.tags
11+
)
12+
}
13+
14+
resource "time_static" "last_update" {
15+
}

github/branch-protection/outputs.tf

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_version = "~> 1.3"
3+
required_providers {
4+
time = {
5+
source = "hashicorp/time",
6+
version = "~> 0.9.1"
7+
}
8+
github = {
9+
source = "integrations/github"
10+
version = "~> 6.0"
11+
}
12+
}
13+
}
14+
15+
provider "github" {
16+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
variable "name" {
2+
description = "The name of the branch protection rule"
3+
type = string
4+
}
5+
6+
variable "customer" {
7+
description = "Customer for the current deployment"
8+
type = string
9+
default = ""
10+
}
11+
12+
variable "environment" {
13+
description = "Environment for the current deployment"
14+
type = string
15+
default = ""
16+
}
17+
18+
variable "tags" {
19+
description = "Default tags to add to resources"
20+
type = map(any)
21+
default = {}
22+
}
23+
24+
# module specific variables
25+
variable "repository_id" {
26+
description = "Name or id of the GitHub repository to protect"
27+
type = string
28+
}
29+
30+
variable "branch_pattern" {
31+
description = "Branch name pattern to protect"
32+
type = string
33+
default = "main"
34+
}
35+
36+
variable "enforce_admins" {
37+
description = "Enforce required status checks for repository administrators"
38+
type = bool
39+
default = true
40+
}
41+
42+
variable "required_status_checks" {
43+
description = "Require status checks to pass before merging"
44+
type = any
45+
default = {}
46+
}
47+
48+
variable "required_pull_request_reviews" {
49+
description = "Require pull request reviews before merging"
50+
type = any
51+
default = {}
52+
}
53+
54+
variable "restrict_pushes" {
55+
description = "Restrict who can push to the branch"
56+
type = any
57+
default = {}
58+
}
59+
60+
variable "force_push_bypassers" {
61+
description = "List of actor IDs that can bypass force push restrictions"
62+
type = any
63+
default = []
64+
}
65+
66+
variable "require_signed_commits" {
67+
description = "Require commits to be signed"
68+
type = bool
69+
default = true
70+
}
71+
72+
variable "required_linear_history" {
73+
description = "Enforce a linear commit history"
74+
type = bool
75+
default = true
76+
}
77+
78+
variable "require_conversation_resolution" {
79+
description = "Require conversation resolution before merging"
80+
type = bool
81+
default = true
82+
}
83+
84+
variable "allows_deletions" {
85+
description = "Allow branch deletions"
86+
type = bool
87+
default = false
88+
}
89+
90+
variable "allows_force_pushes" {
91+
description = "Allow force pushes"
92+
type = bool
93+
default = false
94+
}
95+
96+
variable "lock_branch" {
97+
description = "Lock the branch"
98+
type = bool
99+
default = false
100+
}

github/repository-ruleset/.terraform.lock.hcl

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)