Skip to content

Commit

Permalink
Refactor repository module
Browse files Browse the repository at this point in the history
  • Loading branch information
posquit0 committed Aug 28, 2024
1 parent 6b62457 commit 90d6554
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
19 changes: 9 additions & 10 deletions modules/repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,22 @@ No modules.
|------|-------------|------|---------|:--------:|
| <a name="input_name"></a> [name](#input\_name) | (Required) A name of the repository. | `string` | n/a | yes |
| <a name="input_access"></a> [access](#input\_access) | (Optional) A configuration for the repository access. `access` block as defined below.<br> (Optional) `collaborators` - A list of collaborators to the repository. Each item of `collaborators` block as defined below.<br> (Required) `username` - The GitHub username to add to the repository as a collaborator.<br> (Optional) `role` - The role to grant the collaborator in the repository. Valid values are `read`, `triage`, `write`, `maintain`, `admin` or the name of an existing custom repository role within the organisation. Default is `write`.<br> (Optional) `teams` - A list of teams to the repository. Each item of `teams` block as defined below.<br> (Required) `team` - The GitHub team id or the GitHub team slug.<br> (Optional) `role` - The role to grant the team in the repository. Valid values are `read`, `triage`, `write`, `maintain`, `admin` or the name of an existing custom repository role within the organisation. Default is `read`.<br> (Optional) `sync_enabled` - Whether to sync the repository access. Accesses added outside of the Terraform code will be removed. Defaults to `false`. | <pre>object({<br> collaborators = optional(list(object({<br> username = string<br> role = optional(string, "write")<br> })), [])<br> teams = optional(list(object({<br> team = string<br> role = optional(string, "read")<br> })), [])<br> sync_enabled = optional(bool, false)<br> })</pre> | `{}` | no |
| <a name="input_archive_on_destroy"></a> [archive\_on\_destroy](#input\_archive\_on\_destroy) | (Optional) Set to `true` to archive the repository instead of deleting on destroy. | `bool` | `false` | no |
| <a name="input_archive_on_destroy"></a> [archive\_on\_destroy](#input\_archive\_on\_destroy) | (Optional) Whether to archive the repository instead of deleting on destroy. Defaults to `false`. | `bool` | `false` | no |
| <a name="input_archived"></a> [archived](#input\_archived) | (Optional) Specify if the repository should be archived. Defaults to `false`. NOTE: Currently, the API does not support unarchiving. | `bool` | `false` | no |
| <a name="input_auto_merge_enabled"></a> [auto\_merge\_enabled](#input\_auto\_merge\_enabled) | (Optional) Whether to wait for merge requirements to be met and then merge automatically. Defaults to `false`. | `bool` | `false` | no |
| <a name="input_autolink_references"></a> [autolink\_references](#input\_autolink\_references) | (Optional) A list of autolink references for the repository. Each item of `autolink_references` block as defined below.<br> (Required) `key_prefix` - This prefix appended by a string will generate a link any time it is found in an issue, pull request, or commit.<br> (Required) `target_url_template` - The URL must contain <num> for the reference number.<br> (Optional) `is_alphanumeric` - Whether this autolink reference matches alphanumeric characters. If false, this autolink reference only matches numeric characters. Defaults to `false`. | <pre>set(object({<br> key_prefix = string<br> target_url_template = string<br> is_alphanumeric = optional(bool, false)<br> }))</pre> | `[]` | no |
| <a name="input_branches"></a> [branches](#input\_branches) | (Optional) A list of branches to create and manage within the repository. | `set(string)` | `[]` | no |
| <a name="input_default_branch"></a> [default\_branch](#input\_default\_branch) | (Optional) Set the default branch for the repository. Default is `main` branch. | `string` | `"main"` | no |
| <a name="input_delete_branch_on_merge"></a> [delete\_branch\_on\_merge](#input\_delete\_branch\_on\_merge) | (Optional) Automatically delete head branch after a pull request is merged. Defaults to `true`. | `bool` | `true` | no |
| <a name="input_deploy_keys"></a> [deploy\_keys](#input\_deploy\_keys) | (Optional) A list of deploy keys to grant access to the repository. A deploy key is a SSH key. Each item of `deploy_keys` block as defined below.<br> (Optional) `name` - A name of deploy key.<br> (Required) `key` - A SSH key. Begins with 'ssh-rsa', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521', 'ssh-ed25519', '[email protected]', or '[email protected]'.<br> (Optional) `writable` - Whether to allow write access to the repository. The key can be used to push to the repository if enabled. Defaults to `false`. | <pre>list(object({<br> name = optional(string)<br> key = string<br> writable = optional(bool, false)<br> }))</pre> | `[]` | no |
| <a name="input_description"></a> [description](#input\_description) | (Optional) A description of the repository. | `string` | `"Managed by Terraform."` | no |
| <a name="input_features"></a> [features](#input\_features) | (Optional) A list of enabled features on the repository. Available features: `ISSUES`, `PROJECTS`, `WIKI`. | `set(string)` | <pre>[<br> "ISSUES"<br>]</pre> | no |
| <a name="input_homepage"></a> [homepage](#input\_homepage) | (Optional) A URL of website describing the repository. | `string` | `null` | no |
| <a name="input_is_template"></a> [is\_template](#input\_is\_template) | (Optional) Set to `true` if this is a template repository. | `bool` | `false` | no |
| <a name="input_features"></a> [features](#input\_features) | (Optional) A list of enabled features on the repository. Available features: `ISSUES`, `PROJECTS`, `WIKI`. Defaults to `["ISSUES"]` | `set(string)` | <pre>[<br> "ISSUES"<br>]</pre> | no |
| <a name="input_homepage"></a> [homepage](#input\_homepage) | (Optional) A URL of website describing the repository. | `string` | `""` | no |
| <a name="input_is_template"></a> [is\_template](#input\_is\_template) | (Optional) Whether this is a template repository. Defaults to `false`. | `bool` | `false` | no |
| <a name="input_issue_labels"></a> [issue\_labels](#input\_issue\_labels) | (Optional) A list of issue labels for the repository. Each member of `issue_labels` block as defined below.<br> (Required) `name` - The name of the label.<br> (Required) `color` - A 6 character hex code, without the leading #, identifying the color of the label.<br> (Optional) `description` - A short description of the label. | <pre>set(object({<br> name = string<br> color = string<br> description = optional(string, "Managed by Terraform.")<br> }))</pre> | `[]` | no |
| <a name="input_merge_strategies"></a> [merge\_strategies](#input\_merge\_strategies) | (Optional) A list of allowed strategies for merging pull requests on the repository. Available strategies: `MERGE_COMMIT`, `SQUASH`, `REBASE`. | `set(string)` | <pre>[<br> "SQUASH",<br> "REBASE"<br>]</pre> | no |
| <a name="input_pages_cname"></a> [pages\_cname](#input\_pages\_cname) | (Optional) The custom domain for the repository. This can only be set after the repository has been created. | `string` | `null` | no |
| <a name="input_pages_enabled"></a> [pages\_enabled](#input\_pages\_enabled) | (Optional) Set to true to enable GitHub Pages for the repository. GitHub Pages is designed to host your personal, organization, or project pages from a GitHub repository. | `bool` | `false` | no |
| <a name="input_pages_source_branch"></a> [pages\_source\_branch](#input\_pages\_source\_branch) | (Optional) The repository branch used to publish the site's source files. Defaults to `gh-pages` branch. | `string` | `"gh-pages"` | no |
| <a name="input_pages_source_path"></a> [pages\_source\_path](#input\_pages\_source\_path) | (Optional) The repository directory path from which the site publishes. Defaults to `/`. | `string` | `"/"` | no |
| <a name="input_template"></a> [template](#input\_template) | (Optional) Use a template repository, license or gitignore to create the repository.this resource. `template` block as defined below.<br> (Optional) `gitignore` - Choose which files not to track from a list of templates. Use the name of the template without the extension. For example, `Haskell`.<br> (Optional) `init_readme` - Set to `true` to produce an initial commit with README.md in the repository.<br> (Optional) `license` - A license tells others what they can and can't do with your code. Use the name of the license template without the extension. For example, `mit` or `mpl-2.0`.<br> (Optional) `repository` - Start this repository with a template repository's contents. The full name of the repository is required. A string of the form `owner/repository`. | `any` | `{}` | no |
| <a name="input_merge_strategies"></a> [merge\_strategies](#input\_merge\_strategies) | (Optional) A list of allowed strategies for merging pull requests on the repository. Available strategies: `MERGE_COMMIT`, `SQUASH`, `REBASE`. Defaults to `["SQUASH", "REBASE"]`. | `set(string)` | <pre>[<br> "SQUASH",<br> "REBASE"<br>]</pre> | no |
| <a name="input_pages"></a> [pages](#input\_pages) | (Optional) A configuration of GitHub Pages for the repository. `pages` block as defined below.<br> (Optional) `enabled` - Whether to enable GitHub Pages for the repository. GitHub Pages is designed to host your personal, organization, or project pages from a GitHub repository. Defaults to `false`.<br> (Optional) `source` - A configuration of the repository source files for the site. `source` block as defined below.<br> (Optional) `branch` - The repository branch used to publish the site's source files. Defaults to `gh-pages` branch.<br> (Optional) `path` - The repository directory path from which the site publishes. Defaults to `/`.<br> (Optional) `cname` - The custom domain for the repository. This can only be set after the repository has been created. | <pre>object({<br> enabled = optional(bool, false)<br> source = optional(object({<br> branch = optional(string, "gh-pages")<br> path = optional(string, "/")<br> }), {})<br> cname = optional(string)<br> })</pre> | `{}` | no |
| <a name="input_template"></a> [template](#input\_template) | (Optional) Use a template repository, license or gitignore to create the repository.this resource. `template` block as defined below.<br> (Optional) `gitignore` - Choose which files not to track from a list of templates. Use the name of the template without the extension. For example, `Haskell`.<br> (Optional) `init_readme` - Whether to produce an initial commit with README.md in the repository. Defaults to `false`.<br> (Optional) `license` - A license tells others what they can and can't do with your code. Use the name of the license template without the extension. For example, `mit` or `mpl-2.0`.<br> (Optional) `repository` - Start this repository with a template repository's contents. The full name of the repository is required. A string of the form `owner/repository`. | <pre>object({<br> gitignore = optional(string)<br> init_readme = optional(bool, false)<br> license = optional(string)<br> repository = optional(string)<br> })</pre> | `{}` | no |
| <a name="input_topics"></a> [topics](#input\_topics) | (Optional) A list of topics for the repository. | `set(string)` | `[]` | no |
| <a name="input_visibility"></a> [visibility](#input\_visibility) | (Optional) Can be `public`, `private` or `internal`. `internal` visibility is only available if your organization is associated with an enterprise account using GitHub Enterprise Cloud or GitHub Enterprise Server 2.20+. | `string` | `"private"` | no |
| <a name="input_vulnerability_alerts"></a> [vulnerability\_alerts](#input\_vulnerability\_alerts) | (Optional) Set to true to enable security alerts for vulnerable dependencies. Enabling requires alerts to be enabled on the owner level. GitHub enables the alerts on public repos but disables them on private repos by default. | `bool` | `false` | no |
Expand All @@ -79,6 +77,7 @@ No modules.
|------|-------------|
| <a name="output_access"></a> [access](#output\_access) | The configuration for the repository access. |
| <a name="output_archived"></a> [archived](#output\_archived) | Whether the repository is archived. |
| <a name="output_auto_merge_enabled"></a> [auto\_merge\_enabled](#output\_auto\_merge\_enabled) | Whether to wait for merge requirements to be met and then merge automatically. |
| <a name="output_autolink_references"></a> [autolink\_references](#output\_autolink\_references) | A list of autolink references for the repository. |
| <a name="output_branches"></a> [branches](#output\_branches) | A list of the repository branches excluding initial branch. |
| <a name="output_default_branch"></a> [default\_branch](#output\_default\_branch) | The default branch of the repository. |
Expand Down
33 changes: 24 additions & 9 deletions modules/repository/main.tf
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
# INFO: Not supported attributes
# - `private`
# - `has_downloads`
# INFO: Use a separate resource
# - `topics`
# - `default_branch`
resource "github_repository" "this" {
name = var.name
description = var.description
homepage_url = var.homepage

visibility = var.visibility
is_template = var.is_template
archived = var.archived
archive_on_destroy = var.archive_on_destroy

auto_init = try(var.template.init_readme, false)
license_template = try(var.template.license, null)
gitignore_template = try(var.template.gitignore, null)

## Template
is_template = var.is_template

auto_init = var.template.init_readme
license_template = var.template.license
gitignore_template = var.template.gitignore

dynamic "template" {
for_each = try([var.template.repository], [])
for_each = var.template.repository != null ? [var.template.repository] : []

content {
owner = split("/", template.value)[0]
repository = split("/", template.value)[1]
}
}


## Features
has_issues = contains(var.features, "ISSUES")
has_projects = contains(var.features, "PROJECTS")
has_wiki = contains(var.features, "WIKI")


## Pull Request
allow_merge_commit = contains(var.merge_strategies, "MERGE_COMMIT")
allow_squash_merge = contains(var.merge_strategies, "SQUASH")
allow_rebase_merge = contains(var.merge_strategies, "REBASE")

allow_auto_merge = var.auto_merge_enabled
delete_branch_on_merge = var.delete_branch_on_merge
vulnerability_alerts = var.vulnerability_alerts


## Pages
dynamic "pages" {
for_each = var.pages_enabled ? ["go"] : []
for_each = var.pages.enabled ? [var.pages] : []

content {
source {
branch = var.pages_source_branch
path = var.pages_source_path
branch = pages.value.source.branch
path = pages.value.source.path
}
cname = try(var.pages_cname, null)
cname = pages.value.cname
}
}

Expand All @@ -53,6 +67,7 @@ resource "github_repository" "this" {
gitignore_template,
template,
topics,
has_downloads,
]
}
}
14 changes: 6 additions & 8 deletions modules/repository/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ output "merge_strategies" {
value = var.merge_strategies
}

output "auto_merge_enabled" {
description = "Whether to wait for merge requirements to be met and then merge automatically."
value = github_repository.this.allow_auto_merge
}

output "delete_branch_on_merge" {
description = "Automatically delete head branch after a pull request is merged."
value = github_repository.this.delete_branch_on_merge
Expand Down Expand Up @@ -148,12 +153,5 @@ output "deploy_keys" {

output "pages" {
description = "The repository's GitHub Pages configuration."
value = {
eanbled = var.pages_enabled
cname = var.pages_cname
source = {
branch = var.pages_source_branch
path = var.pages_source_path
}
}
value = var.pages
}
Loading

0 comments on commit 90d6554

Please sign in to comment.