From a6acb679d53a7e1b41693f49b1f3b4920ff2d07a Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Tue, 24 Sep 2024 09:41:56 -0500 Subject: [PATCH 1/3] Feat: add data provider for workflows thru data env0_environment (#957) * Feat: add data provider for workflows thru data env0_environment * added another alias in the test * add sort --- env0/data_environment.go | 45 +++++++++++++++++++++++ env0/data_environment_test.go | 14 +++++++ tests/integration/012_environment/main.tf | 15 +++++--- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/env0/data_environment.go b/env0/data_environment.go index 5e385832..6ccb0ee8 100644 --- a/env0/data_environment.go +++ b/env0/data_environment.go @@ -2,6 +2,8 @@ package env0 import ( "context" + "slices" + "strings" "github.com/env0/terraform-provider-env0/client" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -98,6 +100,25 @@ func dataEnvironment() *schema.Resource { Description: "The token id used for repo integrations (Used by Gitlab or Azure DevOps)", Computed: true, }, + "sub_environment_configuration": { + Type: schema.TypeList, + Description: "the sub environments of the workflow environment. (Empty for non workflow environments)", + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Description: "the id of the sub environment", + Computed: true, + }, + "alias": { + Type: schema.TypeString, + Description: "sub environment alias name", + Computed: true, + }, + }, + }, + }, }, } } @@ -127,6 +148,30 @@ func dataEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta inter return diag.FromErr(err) } + subEnvironments := []interface{}{} + + if environment.LatestDeploymentLog.WorkflowFile != nil { + for alias, subenv := range environment.LatestDeploymentLog.WorkflowFile.Environments { + subEnvironment := map[string]interface{}{ + "id": subenv.EnvironmentId, + "alias": alias, + } + subEnvironments = append(subEnvironments, subEnvironment) + } + } + + slices.SortFunc(subEnvironments, func(a, b interface{}) int { + amap := a.(map[string]interface{}) + bmap := b.(map[string]interface{}) + + aalias := amap["alias"].(string) + balias := bmap["alias"].(string) + + return strings.Compare(aalias, balias) + }) + + d.Set("sub_environment_configuration", subEnvironments) + templateId := environment.LatestDeploymentLog.BlueprintId template, err := getTemplateById(templateId, meta) diff --git a/env0/data_environment_test.go b/env0/data_environment_test.go index 9981407b..ebb9ee3b 100644 --- a/env0/data_environment_test.go +++ b/env0/data_environment_test.go @@ -32,6 +32,16 @@ func TestEnvironmentDataSource(t *testing.T) { BlueprintId: template.Id, BlueprintRevision: "revision", Output: []byte(`{"a": "b"}`), + WorkflowFile: &client.WorkflowFile{ + Environments: map[string]client.WorkflowSubEnvironment{ + "db": { + EnvironmentId: "id_db", + }, + "compute": { + EnvironmentId: "id_compute", + }, + }, + }, }, } @@ -78,6 +88,10 @@ func TestEnvironmentDataSource(t *testing.T) { resource.TestCheckResourceAttr(accessor, "token_id", template.TokenId), resource.TestCheckResourceAttr(accessor, "github_installation_id", strconv.Itoa(template.GithubInstallationId)), resource.TestCheckResourceAttr(accessor, "bitbucket_client_key", template.BitbucketClientKey), + resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.0.alias", "compute"), + resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.0.id", "id_compute"), + resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.1.alias", "db"), + resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.1.id", "id_db"), ), }, }, diff --git a/tests/integration/012_environment/main.tf b/tests/integration/012_environment/main.tf index 41240d27..f0604ef1 100644 --- a/tests/integration/012_environment/main.tf +++ b/tests/integration/012_environment/main.tf @@ -219,7 +219,6 @@ resource "env0_template_project_assignment" "assignment_workflow" { project_id = env0_project.test_project.id } - resource "env0_variable_set" "variable_set1" { name = "variable-set-project-${random_string.random.result}" description = "description123" @@ -253,10 +252,10 @@ resource "env0_environment" "workflow-environment" { env0_template_project_assignment.assignment_workflow, env0_template_project_assignment.assignment_sub_environment_null_template ] - force_destroy = true - name = "environment-workflow-${random_string.random.result}" - project_id = env0_project.test_project.id - template_id = env0_template.workflow_template.id + force_destroy = true + name = "environment-workflow-${random_string.random.result}" + project_id = env0_project.test_project.id + template_id = env0_template.workflow_template.id configuration { name = "n1" @@ -268,7 +267,7 @@ resource "env0_environment" "workflow-environment" { sub_environment_configuration { alias = "rootService1" revision = "master" - approve_plan_automatically = var.second_run ? false : true + approve_plan_automatically = var.second_run ? false : true configuration { name = "sub_env1_var1" value = "hello" @@ -280,6 +279,10 @@ resource "env0_environment" "workflow-environment" { } } +data "env0_environment" "test-workflow" { + id = env0_environment.workflow-environment.id +} + resource "env0_environment" "mark_as_archived" { depends_on = [env0_template_project_assignment.assignment] name = "environment-mark-as-archived-${random_string.random.result}" From 4fb7d27f961e8854c7a4df6ed2af3f854d0e369f Mon Sep 17 00:00:00 2001 From: update generated docs action Date: Tue, 24 Sep 2024 14:42:45 +0000 Subject: [PATCH 2/3] Update docs --- docs/data-sources/environment.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/data-sources/environment.md b/docs/data-sources/environment.md index a228a8ec..c708f466 100644 --- a/docs/data-sources/environment.md +++ b/docs/data-sources/environment.md @@ -52,5 +52,14 @@ output "environment_name" { - `revision` (String) the last deployed revision - `run_plan_on_pull_requests` (Boolean) does pr plan enable - `status` (String) the status of the environment +- `sub_environment_configuration` (List of Object) the sub environments of the workflow environment. (Empty for non workflow environments) (see [below for nested schema](#nestedatt--sub_environment_configuration)) - `template_id` (String) the template id the environment is to be created from - `token_id` (String) The token id used for repo integrations (Used by Gitlab or Azure DevOps) + + +### Nested Schema for `sub_environment_configuration` + +Read-Only: + +- `alias` (String) +- `id` (String) From ee882b8e19c7dee054568917cd5bcdff80c1a812 Mon Sep 17 00:00:00 2001 From: Yaron Yarimi Date: Wed, 25 Sep 2024 08:49:49 +0300 Subject: [PATCH 3/3] Chore: Add assign reviewers GHA (#958) --- .github/workflows/assign-reviewers.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/assign-reviewers.yml diff --git a/.github/workflows/assign-reviewers.yml b/.github/workflows/assign-reviewers.yml new file mode 100644 index 00000000..50da9d7c --- /dev/null +++ b/.github/workflows/assign-reviewers.yml @@ -0,0 +1,20 @@ +name: Assign PR Reviewers + +on: + issue_comment: + types: [created] + +jobs: + assign-reviewers: + runs-on: ubuntu-latest + if: ${{ github.event.issue.pull_request != null && github.event.comment.body == '/review' }} + steps: + - name: Assign PR Reviewers + env: + GITHUB_TOKEN: ${{ secrets.ENV0_BOT_PAT }} + run: | + curl -X POST \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.issue.number }}/requested_reviewers \ + -d '{"team_reviewers":["env0-team"]}'