-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrulesets.tf
More file actions
186 lines (167 loc) · 5.94 KB
/
rulesets.tf
File metadata and controls
186 lines (167 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Org-wide native push protection — max file size + banned extensions.
#
# Enforced at the git layer by GitHub's own push rules; no workflow runs.
# Thresholds and extension list come from config/rulesets-defaults.yml via
# local.push_protection_defaults, so this resource carries no magic numbers
# or hardcoded lists. Bypass actors are managed in the GitHub UI — this
# resource does not claim ownership of them, so manual exemptions for
# specific repos or actors persist across applies.
resource "github_organization_ruleset" "org_push_protection" {
name = "org-push-protection"
target = "push"
enforcement = var.org_push_protection_enforcement
conditions {
repository_name {
include = ["~ALL"]
exclude = []
}
}
rules {
max_file_size {
max_file_size = local.push_protection_defaults.max_file_size_mb
}
file_extension_restriction {
restricted_file_extensions = local.push_protection_defaults.banned_file_extensions
}
}
}
# Org-wide branch-protection — quality rules on every default branch.
#
# Reverse-engineered from the pre-Terraform "main" org ruleset (id
# 15555419) plus new directives: Conventional Commits enforcement and PR
# thread resolution. No bypass actors: rules apply to everyone including
# org admins, so an admin's own commits are still signed, linear, and
# Conventional-format. Review-count enforcement lives in a separate
# ruleset (org_review_gate) so admin bypass on review doesn't accidentally
# weaken these quality gates.
#
# Import-on-first-apply: the import block below adopts ruleset 15555419
# into Terraform state so apply reconciles instead of creating a duplicate.
import {
to = github_organization_ruleset.org_branch_protection
id = "15555419"
}
resource "github_organization_ruleset" "org_branch_protection" {
name = "org-branch-protection"
target = "branch"
enforcement = var.org_branch_protection_enforcement
conditions {
ref_name {
include = ["~DEFAULT_BRANCH"]
exclude = []
}
repository_name {
include = ["~ALL"]
exclude = []
}
}
rules {
required_linear_history = true
required_signatures = true
branch_name_pattern {
operator = local.branch_protection_defaults.branch_name_operator
pattern = local.branch_protection_defaults.branch_name_pattern
negate = false
name = ""
}
commit_message_pattern {
name = "conventional-commits"
operator = "regex"
pattern = local.branch_protection_defaults.commit_message_pattern
negate = false
}
pull_request {
required_approving_review_count = 0
dismiss_stale_reviews_on_push = false
require_code_owner_review = false
require_last_push_approval = false
required_review_thread_resolution = true
allowed_merge_methods = local.branch_protection_defaults.allowed_merge_methods
}
}
}
# Org-wide review gate — 1 approving review + CODEOWNER review on PRs.
#
# Separate ruleset (rather than rolled into org_branch_protection) so the
# OrganizationAdmin bypass below applies ONLY to review enforcement, not
# to signed commits, linear history, or commit format. Any OrganizationAdmin
# can merge their own PRs without external review; non-admin actors (bots,
# external contributors) must obtain the review and, on critical files,
# a CODEOWNER review.
#
# bypass_mode = "pull_request": admins bypass on merge only, not on push.
# Pushes still satisfy every other rule (signed, linear, conventional).
# Granting an additional account the OrganizationAdmin role extends this
# bypass to them — review the role assignments before adding admins.
resource "github_organization_ruleset" "org_review_gate" {
name = "org-review-gate"
target = "branch"
enforcement = var.org_review_gate_enforcement
conditions {
ref_name {
include = ["~DEFAULT_BRANCH"]
exclude = []
}
repository_name {
include = ["~ALL"]
exclude = []
}
}
rules {
pull_request {
required_approving_review_count = 1
dismiss_stale_reviews_on_push = false
require_code_owner_review = true
require_last_push_approval = false
required_review_thread_resolution = true
allowed_merge_methods = local.branch_protection_defaults.allowed_merge_methods
}
}
bypass_actors {
# OrganizationAdmin role: actor_id = 1 is the only valid value for this
# actor_type per the GitHub Rulesets API (a protocol constant, not a
# tunable threshold). Every OrganizationAdmin bypasses review on merge.
actor_id = 1
actor_type = "OrganizationAdmin"
bypass_mode = "pull_request"
}
}
# Org-wide markdown linting, enforced as a Required Workflow.
#
# Adopts the pre-Terraform "Required Workflows - All Branches" ruleset (id
# 17062292) so apply reconciles instead of creating a duplicate. Live state
# at import time: enforcement=disabled, ref_name=~ALL, do_not_enforce_on_create.
# Terraform code below uses ~ALL refs (matching live) and keeps
# do_not_enforce_on_create so brand-new repos aren't blocked on first push
# before their default branch exists. Enforcement variable defaults to
# `evaluate` (the legacy default) — pass `-var markdown_lint_enforcement=active`
# to flip on.
import {
to = github_organization_ruleset.markdown_lint
id = "17062292"
}
resource "github_organization_ruleset" "markdown_lint" {
name = "org-markdown-lint"
target = "branch"
enforcement = var.markdown_lint_enforcement
conditions {
ref_name {
include = ["~ALL"]
exclude = []
}
repository_name {
include = ["~ALL"]
exclude = []
}
}
rules {
required_workflows {
do_not_enforce_on_create = true
required_workflow {
repository_id = data.github_repository.dot_github.repo_id
path = ".github/workflows/markdownlint.yml"
ref = "refs/heads/main"
}
}
}
}