Skip to content

Commit 035f478

Browse files
gaima8tobiasehlerttobias-ds24
authored
feat(cutlery42): Various updates (#7)
* add support for allow_update_branch * updating readme regarding allow_update_branch * update versions.tf with required provider version * feat: add multiple github features: pages build-type, allow_updates, environments, actions variables * fix: wait_timer, users * feat: support force_push_bypassers * feat: add support for github provider v6.x * fix: add block conditionally --------- Co-authored-by: Tobias Lindberg <tobias.ehlert@gmail.com> Co-authored-by: Tobias Habermann <tobias.habermann@digistore24.team>
1 parent 44ac511 commit 035f478

File tree

8 files changed

+144
-8
lines changed

8 files changed

+144
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ See [variables.tf] and [examples/] for details and use-cases.
174174

175175
Default is `false`.
176176

177+
- [**`allow_update_branch`**](#var-allow_update_branch): *(Optional `bool`)*<a name="var-allow_update_branch"></a>
178+
179+
Set to `true` to suggest updating pull request branches.
180+
181+
Default is `false`.
182+
177183
- [**`allow_auto_merge`**](#var-allow_auto_merge): *(Optional `bool`)*<a name="var-allow_auto_merge"></a>
178184

179185
Set to `true` to allow [auto-merging](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/automatically-merging-a-pull-request)

README.tfdoc.hcl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ section {
206206
END
207207
}
208208

209+
variable "allow_update_branch" {
210+
type = bool
211+
default = false
212+
description = <<-END
213+
Set to `true` to suggest updating pull request branches.
214+
END
215+
}
216+
209217
variable "allow_auto_merge" {
210218
type = bool
211219
default = false

actions.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "github_actions_variable" "repository_variable" {
2+
for_each = var.variables
3+
repository = github_repository.repository.name
4+
variable_name = each.key
5+
value = each.value
6+
}

data.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
data "github_organization_teams" "all" {
2+
summary_only = true
3+
}
4+
5+
data "github_user" "user" {
6+
for_each = toset(length(var.environments) > 0 ? distinct(concat([for n, e in var.environments : e.reviewer_users]...)) : [])
7+
username = each.key
8+
}
9+
10+
locals {
11+
team_ids_by_slug = { for t in data.github_organization_teams.all.teams : (t.slug) => t.id }
12+
user_ids_by_name = { for u in data.github_user.user : (u.username) => u.id }
13+
}

environments.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
variable "environments" {
2+
type = map(object({
3+
reviewer_teams = optional(list(string), [])
4+
reviewer_users = optional(list(string), [])
5+
deployment_branch_policy = optional(object({
6+
protected_branches = bool
7+
custom_branch_policies = optional(bool)
8+
}))
9+
branch_patterns = optional(list(string), [])
10+
variables = optional(map(string), {})
11+
wait_timer = optional(number)
12+
prevent_self_review = optional(bool)
13+
}))
14+
default = {}
15+
}
16+
17+
resource "github_repository_environment" "this" {
18+
for_each = var.environments
19+
repository = github_repository.repository.name
20+
environment = each.key
21+
wait_timer = each.value.wait_timer
22+
prevent_self_review = each.value.prevent_self_review
23+
24+
dynamic "reviewers" {
25+
for_each = length(each.value.reviewer_teams) > 0 || length(each.value.reviewer_users) > 0 ? [true] : []
26+
content {
27+
teams = [for slug in each.value.reviewer_teams : try(local.team_ids_by_slug[slug], slug)]
28+
users = [for username in each.value.reviewer_users : local.user_ids_by_name[username]]
29+
}
30+
}
31+
32+
dynamic "deployment_branch_policy" {
33+
for_each = each.value.deployment_branch_policy != null ? [each.value.deployment_branch_policy] : (length(each.value.branch_patterns) > 0 ? [{}] : [])
34+
content {
35+
protected_branches = lookup(deployment_branch_policy.value, "protected_branches", false)
36+
custom_branch_policies = lookup(deployment_branch_policy.value, "custom_branch_policies", length(each.value.branch_patterns) > 0)
37+
}
38+
}
39+
}
40+
41+
resource "github_repository_deployment_branch_policy" "this" {
42+
for_each = merge([for envName, env in var.environments : merge([for bp in env.branch_patterns : { ("${envName}:${bp}") : {
43+
environment = envName
44+
branch_pattern = bp
45+
} }]...)]...)
46+
depends_on = [github_repository_environment.this]
47+
environment_name = each.value.environment
48+
repository = github_repository.repository.name
49+
name = each.value.branch_pattern
50+
}
51+
52+
resource "github_actions_environment_variable" "this" {
53+
for_each = merge([for envName, env in var.environments : merge([for k, v in env.variables : { ("${envName}:${k}") : {
54+
environment = envName
55+
key = k
56+
value = v
57+
} }]...)]...)
58+
depends_on = [github_repository_environment.this]
59+
variable_name = each.value.key
60+
environment = each.value.environment
61+
value = each.value.value
62+
repository = github_repository.repository.name
63+
}

main.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ locals {
1616
allow_merge_commit = var.allow_merge_commit == null ? lookup(var.defaults, "allow_merge_commit", true) : var.allow_merge_commit
1717
allow_rebase_merge = var.allow_rebase_merge == null ? lookup(var.defaults, "allow_rebase_merge", false) : var.allow_rebase_merge
1818
allow_squash_merge = var.allow_squash_merge == null ? lookup(var.defaults, "allow_squash_merge", false) : var.allow_squash_merge
19+
allow_update_branch = var.allow_update_branch == null ? lookup(var.defaults, "allow_update_branch", false) : var.allow_update_branch
1920
allow_auto_merge = var.allow_auto_merge == null ? lookup(var.defaults, "allow_auto_merge", false) : var.allow_auto_merge
2021
delete_branch_on_merge = var.delete_branch_on_merge == null ? lookup(var.defaults, "delete_branch_on_merge", true) : var.delete_branch_on_merge
2122
is_template = var.is_template == null ? lookup(var.defaults, "is_template", false) : var.is_template
@@ -65,6 +66,7 @@ locals {
6566
merge({
6667
strict = null
6768
contexts = []
69+
checks = []
6870
}, b.required_status_checks)] : []
6971
]
7072

@@ -106,6 +108,7 @@ resource "github_repository" "repository" {
106108
allow_merge_commit = local.allow_merge_commit
107109
allow_rebase_merge = local.allow_rebase_merge
108110
allow_squash_merge = local.allow_squash_merge
111+
allow_update_branch = local.allow_update_branch
109112
allow_auto_merge = local.allow_auto_merge
110113
delete_branch_on_merge = local.delete_branch_on_merge
111114
is_template = local.is_template
@@ -147,9 +150,8 @@ resource "github_repository" "repository" {
147150
path = try(var.pages.path, "/")
148151
}
149152
}
150-
151-
build_type = try(var.pages.build_type, null)
152153
cname = try(var.pages.cname, null)
154+
build_type = try(var.pages.build_type, null)
153155
}
154156
}
155157

@@ -223,10 +225,20 @@ resource "github_branch_protection" "branch_protection" {
223225
allows_deletions = try(var.branch_protections_v4[each.value].allows_deletions, false)
224226
allows_force_pushes = try(var.branch_protections_v4[each.value].allows_force_pushes, false)
225227
enforce_admins = try(var.branch_protections_v4[each.value].enforce_admins, true)
228+
force_push_bypassers = try(var.branch_protections_v4[each.value].force_push_bypassers, [])
226229
require_conversation_resolution = try(var.branch_protections_v4[each.value].require_conversation_resolution, false)
227230
require_signed_commits = try(var.branch_protections_v4[each.value].require_signed_commits, false)
228231
required_linear_history = try(var.branch_protections_v4[each.value].required_linear_history, false)
229232

233+
dynamic "restrict_pushes" {
234+
for_each = try(var.branch_protections_v4[each.value].blocks_creations, false) || length(try(var.branch_protections_v4[each.value].push_restrictions, [])) > 0 ? [1] : []
235+
236+
content {
237+
blocks_creations = try(var.branch_protections_v4[each.value].blocks_creations, false)
238+
push_allowances = try(var.branch_protections_v4[each.value].push_restrictions, [])
239+
}
240+
}
241+
230242
dynamic "required_pull_request_reviews" {
231243
for_each = try([var.branch_protections_v4[each.value].required_pull_request_reviews], [])
232244

@@ -280,6 +292,7 @@ resource "github_branch_protection_v3" "branch_protection" {
280292
content {
281293
strict = required_status_checks.value.strict
282294
contexts = required_status_checks.value.contexts
295+
checks = required_status_checks.value.checks
283296
}
284297
}
285298

variables.tf

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ variable "allow_rebase_merge" {
9090
default = null
9191
}
9292

93+
variable "allow_update_branch" {
94+
description = "(Optional) Set to true to suggest updating pull request branches. (Default: false)"
95+
type = bool
96+
default = null
97+
}
98+
9399
variable "allow_auto_merge" {
94100
description = "(Optional) Set to true to allow auto-merging pull requests on the repository. If enabled for a pull request, the pull request will merge automatically when all required reviews are met and status checks have passed. (Default: false)"
95101
type = bool
@@ -117,10 +123,10 @@ variable "auto_init" {
117123
variable "pages" {
118124
description = "(Optional) The repository's GitHub Pages configuration. (Default: {})"
119125
# type = object({
120-
# branch = string
121-
# path = string or null
122-
# cname = string
123-
# build_type = workflow or legacy (requires branch and optional path )
126+
# branch = string
127+
# path = string or null
128+
# cname = string
129+
# build_type = workflow or legacy (requires branch and optional path )
124130
# })
125131
type = any
126132
default = null
@@ -281,6 +287,7 @@ variable "branch_protections_v3" {
281287
# required_status_checks = object({
282288
# strict = bool
283289
# contexts = list(string)
290+
# checks = list(string)
284291
# })
285292
# required_pull_request_reviews = object({
286293
# dismiss_stale_reviews = bool
@@ -335,6 +342,8 @@ variable "branch_protections_v4" {
335342
# allows_deletions = optional(bool, false)
336343
# allows_force_pushes = optional(bool, false)
337344
# enforce_admins = optional(bool, false)
345+
# push_restrictions = optional(list(string), [])
346+
# force_push_bypassers = optional(list(string), [])
338347
# require_conversation_resolution = optional(bool, false)
339348
# require_signed_commits = optional(bool, false)
340349
# required_linear_history = optional(bool, false)
@@ -585,6 +594,25 @@ variable "merge_commit_message" {
585594
default = "PR_TITLE"
586595
}
587596

597+
variable "variables" {
598+
description = "(Optional) Configure action variables. For full details please check: https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_variable"
599+
type = map(string)
600+
601+
default = {}
602+
603+
validation {
604+
condition = length(var.variables) <= 500
605+
error_message = "Github restricts the number of Action variables per repository to 500"
606+
}
607+
608+
validation {
609+
condition = alltrue(concat([true], [
610+
for _, v in var.variables : length(v) <= 48 * 1000
611+
]))
612+
error_message = "Github restricts the maximum size of a single Action variable to 48KB"
613+
}
614+
}
615+
588616
# ------------------------------------------------------------------------------
589617
# MODULE CONFIGURATION PARAMETERS
590618
# These variables are used to configure the module.

versions.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
terraform {
66
required_version = "~> 1.0"
77

8-
# branch_protections_v3 are broken in >= 5.3
98
required_providers {
109
github = {
1110
source = "integrations/github"
12-
version = ">= 4.31, < 7.0"
11+
version = ">= 6.2, < 7.0"
1312
}
1413
}
1514
}

0 commit comments

Comments
 (0)