Skip to content

Commit ca7954d

Browse files
feat(branch): Add branch protections: (akur8-oss#4)
The GraphQL API does not support authentication with fine-grained personal access tokens. As such, branch protections is made using the REST API, that doesn't offer all features.
1 parent 5edf4e2 commit ca7954d

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module "repo" {
3636
|------|------|
3737
| [github_branch.default](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch) | resource |
3838
| [github_branch_default.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default) | resource |
39+
| [github_branch_protection_v3.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_protection_v3) | resource |
3940
| [github_repository.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) | resource |
4041
| [github_repository_tag_protection.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_tag_protection) | resource |
4142
| [github_team.maintain](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/team) | resource |
@@ -60,6 +61,7 @@ module "repo" {
6061
| <a name="input_archive_on_destroy"></a> [archive_on_destroy](#input_archive_on_destroy) | Set to false to delete the repository instead of archiving on destroy. | `bool` | `true` | no |
6162
| <a name="input_archived"></a> [archived](#input_archived) | Specifies if the repository should be archived. Defaults to false. NOTE Currently, the API does not support unarchiving. | `bool` | `false` | no |
6263
| <a name="input_auto_init"></a> [auto_init](#input_auto_init) | Set to `false` to prevent producing an initial commit in the repository. | `bool` | `true` | no |
64+
| <a name="input_branch_protections"></a> [branch_protections](#input_branch_protections) | List of branches to protect, allong with their configuration. | ```list(object({ branch = string, enforce_admins = bool, require_signed_commits = bool, require_conversation_resolution = bool, force_branch_update = bool, checks = list(string), dismiss_stale_reviews = bool, dismissal_teams = list(string), dismissal_users = list(string), require_code_owner_reviews = bool, required_approving_review_count = number }))``` | `[]` | no |
6365
| <a name="input_create_new_teams"></a> [create_new_teams](#input_create_new_teams) | Create new teams to delegate permissions on the repositor. | `bool` | `false` | no |
6466
| <a name="input_default_branch"></a> [default_branch](#input_default_branch) | The name of the repository branch. | `string` | `"main"` | no |
6567
| <a name="input_delete_branch_on_merge"></a> [delete_branch_on_merge](#input_delete_branch_on_merge) | Automatically delete head branch after a pull request is merged. Defaults to true. | `bool` | `true` | no |

branches.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
resource "github_branch" "default" {
2+
count = var.default_branch == "main" ? 0 : 1
3+
4+
repository = github_repository.this.name
5+
branch = var.default_branch
6+
}
7+
8+
resource "github_branch_default" "this" {
9+
repository = github_repository.this.name
10+
branch = var.default_branch == "main" ? "main" : github_branch.default[0].branch
11+
}
12+
13+
resource "github_branch_protection_v3" "this" {
14+
count = length(var.branch_protections)
15+
16+
repository = github_repository.this.name
17+
18+
branch = var.branch_protections[count.index].branch
19+
enforce_admins = var.branch_protections[count.index].enforce_admins
20+
require_signed_commits = var.branch_protections[count.index].require_signed_commits
21+
require_conversation_resolution = var.branch_protections[count.index].require_conversation_resolution
22+
23+
required_status_checks {
24+
strict = var.branch_protections[count.index].force_branch_update
25+
checks = var.branch_protections[count.index].checks
26+
}
27+
28+
required_pull_request_reviews {
29+
dismiss_stale_reviews = var.branch_protections[count.index].dismiss_stale_reviews
30+
dismissal_teams = var.branch_protections[count.index].dismissal_teams
31+
dismissal_users = var.branch_protections[count.index].dismissal_users
32+
require_code_owner_reviews = var.branch_protections[count.index].require_code_owner_reviews
33+
required_approving_review_count = var.branch_protections[count.index].required_approving_review_count
34+
}
35+
}

main.tf

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@ resource "github_repository" "this" {
4949
}
5050
}
5151

52-
resource "github_branch" "default" {
53-
count = var.default_branch == "main" ? 0 : 1
54-
55-
repository = github_repository.this.name
56-
branch = var.default_branch
57-
}
58-
59-
resource "github_branch_default" "this" {
60-
repository = github_repository.this.name
61-
branch = var.default_branch == "main" ? "main" : github_branch.default[0].branch
62-
}
63-
6452
resource "github_repository_tag_protection" "this" {
6553
count = length(var.tag_protections)
6654

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,21 @@ variable "tag_protections" {
164164
type = list(string)
165165
default = []
166166
}
167+
168+
variable "branch_protections" {
169+
description = "List of branches to protect, allong with their configuration."
170+
type = list(object({
171+
branch = string,
172+
enforce_admins = bool,
173+
require_signed_commits = bool,
174+
require_conversation_resolution = bool,
175+
force_branch_update = bool,
176+
checks = list(string),
177+
dismiss_stale_reviews = bool,
178+
dismissal_teams = list(string),
179+
dismissal_users = list(string),
180+
require_code_owner_reviews = bool,
181+
required_approving_review_count = number
182+
}))
183+
default = []
184+
}

0 commit comments

Comments
 (0)