-
Notifications
You must be signed in to change notification settings - Fork 16
Fix: have a good default for approval flow on workflows #1029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR updates the default approval flow logic for workflows by basing the workflow decision on the template type and refactoring the environment creation logic. In addition, it updates test expectations and acceptance tests to better align with these changes.
- Update test expectations for Template calls and workflow type checks.
- Refactor environment creation into separate functions for template and templateless environments.
- Adjust approval field update logic and payload generation based on the workflow type.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
env0/resource_environment_test.go | Revised test expectations for Template calls and workflow type constant |
env0/resource_environment.go | Refactored environment creation and payload functions with updated approval flow logic |
Comments suppressed due to low confidence (2)
env0/resource_environment_test.go:2442
- Verify that increasing the expected Template call from 1 to 2 is intentional and that the test covers all interactions to avoid future brittle behavior.
mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(2).Return(template, nil)
env0/resource_environment.go:1040
- [nitpick] Confirm that determining the workflow status solely based on the template type covers all expected scenarios, particularly if additional types may be introduced later.
isWorkflow := templateType == client.WORKFLOW
/review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves the default behavior for the approval flow in workflow environments by basing decisions on the template type and providing a cleaner update mechanism. Key changes include:
- Using template.Type instead of sub_environment_configuration to determine workflow environments.
- Ignoring "approve_plan_automatically" when not set in the schema.
- Splitting environment creation logic into separate functions for templated and templateless environments, with accompanying updates to acceptance tests and CI workflows.
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
File | Description |
---|---|
env0/resource_environment_test.go | Updated test expectations to match new workflow logic |
env0/resource_environment.go | Refactored environment creation & validation logic for workflows |
env0/data_environment.go | Adjusted data reading to reflect the new approval default |
.github/workflows/*.yml | Updated CI configuration to use Ubuntu 24.04 |
Comments suppressed due to low confidence (2)
env0/resource_environment_test.go:2442
- Please confirm that the increased expectation from Times(1) to Times(2) for the Template call is intentional based on the new workflow logic, and update the test case description if necessary.
mock.EXPECT().Template(environment.LatestDeploymentLog.BlueprintId).Times(2).Return(template, nil)
env0/resource_environment.go:1044
- [nitpick] Ensure that using the equality check (templateType == client.WORKFLOW) reliably distinguishes workflow environments. If there are alternative representations for workflow environments, consider normalizing the input to avoid misclassification.
isWorkflow := templateType == client.WORKFLOW
f4782e9
to
9705a36
Compare
@@ -72,4 +72,4 @@ jobs: | |||
- name: Checkout code | |||
uses: actions/checkout@v4 | |||
- name: Run Harness tests | |||
run: go run tests/harness.go | |||
run: go run tests/harness.go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add empty line in the end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those are a lot of changes for the approval flow, can you explain what did you do here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. the change is a litte complex.
@@ -550,20 +550,66 @@ func createVariable(configurationVariable *client.ConfigurationVariable) any { | |||
} | |||
|
|||
// Validate that the template is assigned to the "project_id". | |||
func validateTemplateProjectAssignment(d *schema.ResourceData, apiClient client.ApiClientInterface) error { | |||
func validateTemplateProjectAssignment(d *schema.ResourceData, template *client.Template) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need the template in one of the upper functions, so here instead of getting it we pass it here.
we get it from the upper functions.
return nil | ||
} | ||
|
||
func createEnvironmentWithTemplate(d *schema.ResourceData, apiClient client.ApiClientInterface) (client.Environment, client.EnvironmentCreate, diag.Diagnostics) { | ||
templateId := d.Get("template_id").(string) | ||
|
||
template, err := apiClient.Template(templateId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is why this part was removed.
return nil | ||
} | ||
|
||
func createEnvironmentWithTemplate(d *schema.ResourceData, apiClient client.ApiClientInterface) (client.Environment, client.EnvironmentCreate, diag.Diagnostics) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have two usecases.
template and envrionment seperate resources.
template + environment one resource.
previousuly it was an if-else in the code. now it's two helper functions with a if-else.
} | ||
|
||
return nil | ||
environmentPayload, diagError := getCreatePayload(d, apiClient, template.Type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we pass the template.Type - with this we set a better default.
return environment, environmentPayload, nil | ||
} | ||
|
||
func createEnvironmentWithoutTemplate(d *schema.ResourceData, apiClient client.ApiClientInterface) (client.Environment, client.EnvironmentCreate, diag.Diagnostics) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
helper function 2 - environment + template together.
// Note: the blueprint id field of the environment is returned only during creation of a template without environment. | ||
// Afterward, it will be omitted from future response. | ||
// setEnvironmentSchema() sets the blueprint id in the resource (under "without_template_settings.0.id"). | ||
environment, err := apiClient.EnvironmentCreateWithoutTemplate(payload) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the payload contains the template details which means it contains the template Type which we need for better defaults.
|
||
environment, err = apiClient.EnvironmentCreate(environmentPayload) | ||
if !isTemplateless(d) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the if-else where we call the two helper functions.
@@ -1016,7 +1041,7 @@ func getCreatePayload(d *schema.ResourceData, apiClient client.ApiClientInterfac | |||
payload.AutoDeployOnPathChangesOnly = boolPtr(val.(bool)) | |||
} | |||
|
|||
_, isWorkflow := d.GetOk("sub_environment_configuration") | |||
isWorkflow := templateType == client.WORKFLOW |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and finally we decide if it's a workflow based on the templateType.
if isWorkflow is True, defaults will be set as needed. This code already exist.
The change is - we decide if it's a workflow based on the template type instead of checking if sub_environment_configuration exist.
// Don't update this value for workflow environments - this value should always be 'false'. | ||
if _, isWorkflow := d.GetOk("sub_environment_configuration"); !isWorkflow && environment.RequiresApproval != nil { | ||
//nolint:staticcheck // https://github.com/hashicorp/terraform-plugin-sdk/issues/817 | ||
if _, exists := d.GetOkExists("approve_plan_automatically"); exists && environment.RequiresApproval != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only make changes to approve_plan_automatically if someone has set a value for it.
it's tri-boolean value.
undefined, true, and false.
return nil | ||
} | ||
|
||
func createEnvironmentWithTemplate(d *schema.ResourceData, apiClient client.ApiClientInterface) (client.Environment, client.EnvironmentCreate, diag.Diagnostics) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
environemnt and template - seperate resources use-case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After talking seems ok to me
note name suggestions
@eranelbaz - going to merge. If you still want to make naming changes let me know and I create an additional PR. But note that I've used common naming conventions. |
Issue & Steps to Reproduce / Feature Request
fixes #1023
Solution