From f3924f2a579fa16450da65df264da5962cff41ff Mon Sep 17 00:00:00 2001
From: Anton KOVACH <2207136+antonkovach@users.noreply.github.com>
Date: Mon, 6 Mar 2023 09:32:36 +0100
Subject: [PATCH] feat: Add Pull Request support to 0-cicd-github (#1213)
* feat: Add Pull Request support to 0-cicd-github
The cloud-foundation-fabricrepository is continually evolving, and to help keep up with the changes, it would be beneficial to introduce a pull request mechanism to review and approve changes. This feature is 100% backward compatible, and by default, no pull request is created, and changes are committed directly to the main branch. However, an optional variable pull_request_config can be used to configure the title, body, head_ref, and base_ref of the pull request that will be created for the initial population or update of files. To create a pull request, in pull_request_config set the create attribute to true. base_ref defaults to main, and head_ref to the name of the head branch. If the head branch doesn't exist, it will be created from the base_ref branch.
* fix README.md
* fix pull_request_config title
---
fast/extras/0-cicd-github/README.md | 26 +++++++++++++++++++++++---
fast/extras/0-cicd-github/main.tf | 26 +++++++++++++++++++++++++-
fast/extras/0-cicd-github/variables.tf | 13 +++++++++++++
3 files changed, 61 insertions(+), 4 deletions(-)
diff --git a/fast/extras/0-cicd-github/README.md b/fast/extras/0-cicd-github/README.md
index 0bd0b5be1d..fc4a30c645 100644
--- a/fast/extras/0-cicd-github/README.md
+++ b/fast/extras/0-cicd-github/README.md
@@ -116,8 +116,27 @@ Initial population depends on a modules repository being configured in the `modu
### Commit configuration
-Finally, a `commit_config` variable is optional: it can be used to configure author, email and message used in commits for initial population of files, its defaults are probably fine for most use cases.
+An optional variable `commit_config` can be used to configure the author, email, and message used in commits for the initial population of files. Its defaults are probably fine for most use cases.
+### Pull Request configuration
+
+An optional variable `pull_request_config` can be used to configure the title, body, head_ref, and base_ref of the pull request created for the initial population or update of files. By default, no pull request is created. To create a pull request, set the `create` attribute to `true`. `base_ref` defaults to `main` and `head_ref` to the head branch name. If the head branch does not exist, it will be created from the base_ref branch.
+
+```hcl
+pull_request_config = {
+ create = true
+ title = "FAST: initial loading or update"
+ body = ""
+ base_ref = "main"
+ head_ref = "fast-loader"
+}
+# tftest skip
+```
+
+To start using a pull request workflow, if the initial loading was created without a pull request in the past, please use the following command to delete the actual branch files from the Terraform state to keep it in the current state:
+```bash
+terraform state list | grep github_repository_file | awk '{print "terraform state rm '\''"$1"'\''"}'
+```
@@ -126,7 +145,7 @@ Finally, a `commit_config` variable is optional: it can be used to configure aut
| name | description | resources |
|---|---|---|
| [cicd-versions.tf](./cicd-versions.tf) | Provider version. | |
-| [main.tf](./main.tf) | Module-level locals and resources. | github_actions_secret
· github_repository
· github_repository_deploy_key
· github_repository_file
· tls_private_key
|
+| [main.tf](./main.tf) | Module-level locals and resources. | github_actions_secret
· github_branch
· github_repository
· github_repository_deploy_key
· github_repository_file
· github_repository_pull_request
· tls_private_key
|
| [outputs.tf](./outputs.tf) | Module outputs. | |
| [providers.tf](./providers.tf) | Provider configuration. | |
| [variables.tf](./variables.tf) | Module variables. | |
@@ -138,7 +157,8 @@ Finally, a `commit_config` variable is optional: it can be used to configure aut
| [organization](variables.tf#L51) | GitHub organization. | string
| ✓ | |
| [commmit_config](variables.tf#L17) | Configure commit metadata. | object({…})
| | {}
|
| [modules_config](variables.tf#L28) | Configure access to repository module via key, and replacement for modules sources in stage repositories. | object({…})
| | null
|
-| [repositories](variables.tf#L56) | Repositories to create. | map(object({…}))
| | {}
|
+| [pull_request_config](variables.tf#L56) | Configure pull request metadata. | object({…})
| | {}
|
+| [repositories](variables.tf#L69) | Repositories to create. | map(object({…}))
| | {}
|
## Outputs
diff --git a/fast/extras/0-cicd-github/main.tf b/fast/extras/0-cicd-github/main.tf
index 81739cc6ed..37607435c2 100644
--- a/fast/extras/0-cicd-github/main.tf
+++ b/fast/extras/0-cicd-github/main.tf
@@ -136,10 +136,21 @@ resource "github_actions_secret" "default" {
)
}
+resource "github_branch" "default" {
+ for_each = (
+ try(var.pull_request_config.create, null) == true
+ ? local.repositories
+ : {}
+ )
+ repository = each.key
+ branch = var.pull_request_config.head_ref
+ source_branch = var.pull_request_config.base_ref
+}
+
resource "github_repository_file" "default" {
for_each = local.modules_repo == null ? {} : local.repository_files
repository = local.repositories[each.value.repository]
- branch = "main"
+ branch = try(var.pull_request_config.head_ref, "main")
file = each.value.name
content = (
endswith(each.value.name, ".tf") && local.modules_repo != null
@@ -161,3 +172,16 @@ resource "github_repository_file" "default" {
]
}
}
+
+resource "github_repository_pull_request" "default" {
+ for_each = (
+ try(var.pull_request_config.create, null) == true
+ ? local.repositories
+ : {}
+ )
+ base_repository = each.key
+ title = var.pull_request_config.title
+ body = var.pull_request_config.body
+ base_ref = var.pull_request_config.base_ref
+ head_ref = var.pull_request_config.head_ref
+}
diff --git a/fast/extras/0-cicd-github/variables.tf b/fast/extras/0-cicd-github/variables.tf
index ea378ee789..63854ffb66 100644
--- a/fast/extras/0-cicd-github/variables.tf
+++ b/fast/extras/0-cicd-github/variables.tf
@@ -53,6 +53,19 @@ variable "organization" {
type = string
}
+variable "pull_request_config" {
+ description = "Configure pull request metadata."
+ type = object({
+ create = optional(bool, false)
+ title = optional(string, "FAST: initial loading or update")
+ body = optional(string, "")
+ base_ref = optional(string, "main")
+ head_ref = optional(string, "fast-loader")
+ })
+ default = {}
+ nullable = false
+}
+
variable "repositories" {
description = "Repositories to create."
type = map(object({