Skip to content

Commit 3bb3764

Browse files
Merge pull request #111 from mineiros-io/soerenmartius/github-branches
Add support for github_branches
2 parents a3c92b9 + 1debcb2 commit 3bb3764

File tree

7 files changed

+175
-13
lines changed

7 files changed

+175
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.15.0]
11+
1012
### Fixed
1113

1214
- Set correct alternative type for `deploy_keys` in README
1315

16+
### Added
17+
18+
- Add support for `github_branches`
19+
1420
## [0.14.0]
1521

1622
### Added
@@ -355,7 +361,8 @@ Please review plans and report regressions and issues asap so we can improve doc
355361
- This is the initial release of our GitHub Repository module with support for
356362
creating and managing GitHub Repositories for Organizations.
357363

358-
[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.14.0...HEAD
364+
[unreleased]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.15.0...HEAD
365+
[0.15.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.14.0...v0.15.0
359366
[0.14.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.13.0...v0.14.0
360367
[0.13.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.12.0...v0.13.0
361368
[0.12.0]: https://github.com/mineiros-io/terraform-github-repository/compare/v0.11.0...v0.12.0

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _Security related notice: Versions 4.7.0, 4.8.0, 4.9.0 and 4.9.1 of the Terrafor
2525
- [Repository Creation Configuration](#repository-creation-configuration)
2626
- [Teams Configuration](#teams-configuration)
2727
- [Collaborator Configuration](#collaborator-configuration)
28+
- [Branches Configuration](#branches-configuration)
2829
- [Deploy Keys Configuration](#deploy-keys-configuration)
2930
- [Branch Protections Configuration](#branch-protections-configuration)
3031
- [Issue Labels Configuration](#issue-labels-configuration)
@@ -62,6 +63,7 @@ features like Branch Protection or Collaborator Management.
6263
Template Repository
6364

6465
- **Extended Repository Features**:
66+
Branches,
6567
Branch Protection,
6668
Issue Labels,
6769
Handle Github Default Issue Labels,
@@ -259,8 +261,10 @@ See [variables.tf] and [examples/] for details and use-cases.
259261
- [**`default_branch`**](#var-default_branch): *(Optional `string`)*<a name="var-default_branch"></a>
260262

261263
The name of the default branch of the repository.
262-
NOTE: This can only be set after a repository has already been created, and after a correct reference has been created for the target branch inside the repository.
263-
This means a user will have to omit this parameter from the initial repository creation and create the target branch inside of the repository prior to setting this attribute.
264+
NOTE: The configured default branch must exist in the repository.
265+
If the branch doesn't exist yet, or if you are creating a new
266+
repository, please add the desired default branch to the `branches`
267+
variable, which will cause Terraform to create it for you.
264268

265269
Default is `""`.
266270

@@ -416,6 +420,32 @@ This is due to some terraform limitation and we will update the module once terr
416420

417421
Default is `[]`.
418422

423+
#### Branches Configuration
424+
425+
- [**`branches`**](#var-branches): *(Optional `list(branch)`)*<a name="var-branches"></a>
426+
427+
Can also be type `list(string)`. Create and manage branches within your repository.
428+
Additional constraints can be applied to ensure your branch is created from another branch or commit.
429+
Every `string` in the list will be converted internally into the `object` representation with the `name` argument being set to the `string`. `object` details are explained below.
430+
431+
Default is `[]`.
432+
433+
Each `branch` object in the list accepts the following attributes:
434+
435+
- [**`name`**](#attr-branches-name): *(**Required** `string`)*<a name="attr-branches-name"></a>
436+
437+
The name of the branch to create.
438+
439+
- [**`source_branch`**](#attr-branches-source_branch): *(Optional `string`)*<a name="attr-branches-source_branch"></a>
440+
441+
The branch name to start from. Uses the configured default branch per default.
442+
443+
- [**`source_sha`**](#attr-branches-source_sha): *(Optional `bool`)*<a name="attr-branches-source_sha"></a>
444+
445+
The commit hash to start from. Defaults to the tip of `source_branch`. If provided, `source_branch` is ignored.
446+
447+
Default is `true`.
448+
419449
#### Deploy Keys Configuration
420450

421451
- [**`deploy_keys`**](#var-deploy_keys): *(Optional `list(deploy_key)`)*<a name="var-deploy_keys"></a>
@@ -800,6 +830,12 @@ The following attributes are exported by the module:
800830
resource containing all arguments as specified above and the other
801831
attributes as specified below.
802832

833+
- [**`branches`**](#output-branches): *(`object(branches)`)*<a name="output-branches"></a>
834+
835+
All repository attributes as returned by the [`github_branch`]
836+
resource containing all arguments as specified above and the other
837+
attributes as specified below.
838+
803839
- [**`full_name`**](#output-full_name): *(`string`)*<a name="output-full_name"></a>
804840

805841
A string of the form "orgname/reponame".
@@ -856,6 +892,7 @@ The following attributes are exported by the module:
856892
### Terraform Github Provider Documentation
857893

858894
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository
895+
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch
859896
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_collaborator
860897
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_deploy_key
861898
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_project

README.tfdoc.hcl

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ section {
6565
Template Repository
6666
6767
- **Extended Repository Features**:
68+
Branches,
6869
Branch Protection,
6970
Issue Labels,
7071
Handle Github Default Issue Labels,
@@ -317,8 +318,10 @@ section {
317318
default = ""
318319
description = <<-END
319320
The name of the default branch of the repository.
320-
NOTE: This can only be set after a repository has already been created, and after a correct reference has been created for the target branch inside the repository.
321-
This means a user will have to omit this parameter from the initial repository creation and create the target branch inside of the repository prior to setting this attribute.
321+
NOTE: The configured default branch must exist in the repository.
322+
If the branch doesn't exist yet, or if you are creating a new
323+
repository, please add the desired default branch to the `branches`
324+
variable, which will cause Terraform to create it for you.
322325
END
323326
}
324327

@@ -529,6 +532,43 @@ section {
529532
}
530533
}
531534

535+
section {
536+
title = "Branches Configuration"
537+
538+
variable "branches" {
539+
type = list(branch)
540+
default = []
541+
description = <<-END
542+
Can also be type `list(string)`. Create and manage branches within your repository.
543+
Additional constraints can be applied to ensure your branch is created from another branch or commit.
544+
Every `string` in the list will be converted internally into the `object` representation with the `name` argument being set to the `string`. `object` details are explained below.
545+
END
546+
547+
attribute "name" {
548+
required = true
549+
type = string
550+
description = <<-END
551+
The name of the branch to create.
552+
END
553+
}
554+
555+
attribute "source_branch" {
556+
type = string
557+
description = <<-END
558+
The branch name to start from. Uses the configured default branch per default.
559+
END
560+
}
561+
562+
attribute "source_sha" {
563+
type = bool
564+
default = true
565+
description = <<-END
566+
The commit hash to start from. Defaults to the tip of `source_branch`. If provided, `source_branch` is ignored.
567+
END
568+
}
569+
}
570+
}
571+
532572
section {
533573
title = "Deploy Keys Configuration"
534574

@@ -1046,6 +1086,15 @@ section {
10461086
END
10471087
}
10481088

1089+
output "branches" {
1090+
type = object(branches)
1091+
description = <<-END
1092+
All repository attributes as returned by the [`github_branch`]
1093+
resource containing all arguments as specified above and the other
1094+
attributes as specified below.
1095+
END
1096+
}
1097+
10491098
output "full_name" {
10501099
type = string
10511100
description = <<-END
@@ -1138,6 +1187,7 @@ section {
11381187
title = "Terraform Github Provider Documentation"
11391188
content = <<-END
11401189
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository
1190+
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch
11411191
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_collaborator
11421192
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_deploy_key
11431193
- https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_project

main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,30 @@ resource "github_repository" "repository" {
144144
}
145145
}
146146

147+
# ---------------------------------------------------------------------------------------------------------------------
148+
# Manage branches
149+
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch
150+
# ---------------------------------------------------------------------------------------------------------------------
151+
152+
locals {
153+
branches_temp = [
154+
for b in var.branches : try({ name = tostring(b) }, b)
155+
]
156+
157+
branches = {
158+
for b in local.branches_temp : b.name => b
159+
}
160+
}
161+
162+
resource "github_branch" "branch" {
163+
for_each = local.branches
164+
165+
repository = github_repository.repository.name
166+
branch = each.value.name
167+
source_branch = try(each.value.source_branch, null)
168+
source_sha = try(each.value.source_sha, null)
169+
}
170+
147171
# ---------------------------------------------------------------------------------------------------------------------
148172
# Set default branch
149173
# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default
@@ -154,6 +178,8 @@ resource "github_branch_default" "default" {
154178

155179
repository = github_repository.repository.name
156180
branch = local.default_branch
181+
182+
depends_on = [github_branch.branch]
157183
}
158184

159185
# ---------------------------------------------------------------------------------------------------------------------

outputs.tf

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
output "repository" {
2-
value = github_repository.repository
3-
description = "All attributes and arguments as returned by the github_repository resource."
4-
}
1+
# ----------------------------------------------------------------------------------------------------------------------
2+
# OUTPUT CALCULATED VARIABLES (prefer full objects)
3+
# ----------------------------------------------------------------------------------------------------------------------
54

65
output "full_name" {
76
value = github_repository.repository.full_name
@@ -28,6 +27,20 @@ output "git_clone_url" {
2827
description = "URL that can be provided to git clone to clone the repository anonymously via the git protocol."
2928
}
3029

30+
# ----------------------------------------------------------------------------------------------------------------------
31+
# OUTPUT ALL RESOURCES AS FULL OBJECTS
32+
# ----------------------------------------------------------------------------------------------------------------------
33+
34+
output "repository" {
35+
value = github_repository.repository
36+
description = "All attributes and arguments as returned by the github_repository resource."
37+
}
38+
39+
output "branches" {
40+
value = github_branch.branch
41+
description = "A map of branch objects keyed by branch name."
42+
}
43+
3144
output "collaborators" {
3245
value = github_repository_collaborator.collaborator
3346
description = "A map of collaborator objects keyed by collaborator.name."
@@ -65,3 +78,7 @@ output "secrets" {
6578
value = [for secret in github_actions_secret.repository_secret : secret.secret_name]
6679
description = "List of secrets available."
6780
}
81+
82+
# ----------------------------------------------------------------------------------------------------------------------
83+
# OUTPUT MODULE CONFIGURATION
84+
# ----------------------------------------------------------------------------------------------------------------------

test/unit-complete/main.tf

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ module "repository" {
4949
archived = false
5050
topics = var.topics
5151

52-
admin_collaborators = ["terraform-test-user-1"]
52+
branches = [
53+
{
54+
name = "develop"
55+
},
56+
{
57+
name = "staging"
58+
},
59+
]
5360

61+
admin_collaborators = ["terraform-test-user-1"]
5462

5563
admin_team_ids = [
5664
github_team.team.id
@@ -140,9 +148,15 @@ resource "github_branch" "development" {
140148
module "repository-with-defaults" {
141149
source = "../.."
142150

143-
name = var.repository_with_defaults_name
144-
description = var.repository_with_defaults_description
145-
defaults = var.repository_defaults
151+
name = var.repository_with_defaults_name
152+
description = var.repository_with_defaults_description
153+
defaults = var.repository_defaults
154+
default_branch = "development"
155+
156+
branches = [
157+
"development",
158+
"prod",
159+
]
146160
}
147161

148162
# ---------------------------------------------------------------------------------------------------------------------

variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ variable "name" {
1313
# These variables have defaults, but may be overridden.
1414
# ---------------------------------------------------------------------------------------------------------------------
1515

16+
variable "branches" {
17+
description = "(Optional) A list of branches to be created in this repository."
18+
type = any
19+
# type = list(object({
20+
# name = string
21+
# source_branch = optional(string)
22+
# source_sha = optional(string)
23+
# }))
24+
default = []
25+
}
26+
1627
variable "defaults" {
1728
description = "(Optional) Overwrite defaults for various repository settings"
1829
type = any

0 commit comments

Comments
 (0)