Repository navigation
fix: Handle scenario where environment name query field isn't respected #771
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,30 +276,13 @@ func releasePromote(octopus *client.Client, space *spaces.Space, input any) erro | |
| return fmt.Errorf("project not found. %v", err) | ||
| } | ||
|
|
||
| envs, err := environments.Get(octopus, space.ID, environments.EnvironmentQuery{ | ||
| IDs: []string{params.FromEnvironment}, | ||
| Skip: 0, | ||
| Take: math.MaxInt32, | ||
| }) | ||
| fromEnv, err := getEnvironment(octopus, space, err, params.FromEnvironment) | ||
| if err != nil { | ||
| return fmt.Errorf("environment not found. %v", err) | ||
| } | ||
| // | ||
| if len(envs.Items) == 0 { | ||
| envs, err = environments.Get(octopus, space.ID, environments.EnvironmentQuery{ | ||
| Name: params.FromEnvironment, | ||
| Skip: 0, | ||
| Take: math.MaxInt32, | ||
| }) | ||
| if err != nil || len(envs.Items) == 0 { | ||
| return fmt.Errorf("environment not found. %v", err) | ||
| } | ||
| return err | ||
| } | ||
| // just grab the first environment | ||
| sourceEnv := envs.Items[0] | ||
|
|
||
| dashboardQuery := dashboard.DashboardDynamicQuery{ | ||
| Environments: []string{sourceEnv.ID}, | ||
| Environments: []string{fromEnv.ID}, | ||
| Projects: []string{proj.ID}, | ||
| // If using latest successful, we need previous dashboard items as well | ||
| // so that we can ensure we get a previous successful deployment | ||
|
|
@@ -315,7 +298,7 @@ func releasePromote(octopus *client.Client, space *spaces.Space, input any) erro | |
| // this is probably unnecessary, but it mimics the existing logic | ||
| var dashboardItems []*dashboard.DashboardItem | ||
| for _, item := range dynamicDashboard.Items { | ||
| if item.EnvironmentID == sourceEnv.ID && item.ProjectID == proj.ID { | ||
| if item.EnvironmentID == fromEnv.ID && item.ProjectID == proj.ID { | ||
| dashboardItems = append(dashboardItems, item) | ||
| } | ||
| } | ||
|
|
@@ -376,3 +359,50 @@ func compareDashboardItemsByRelease(a, b *dashboard.DashboardItem) int { | |
| // we multiply by -1 to order by descending | ||
| return versioning.Compare(aVersion, bVersion, (*versioning.OctopusVersion).Compare) * -1 | ||
| } | ||
|
|
||
| func getEnvironment(octopus *client.Client, space *spaces.Space, err error, nameOrID string) (*environments.Environment, error) { | ||
| envResponse, err := environments.Get(octopus, space.ID, environments.EnvironmentQuery{ | ||
| IDs: []string{nameOrID}, | ||
| Skip: 0, | ||
| Take: 1, // we are matching on ID, so there should only be one | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("environment not found. %v", err) | ||
| } | ||
|
|
||
| // double check the ID and return if true | ||
| if len(envResponse.Items) == 1 && envResponse.Items[0].ID == nameOrID { | ||
| return envResponse.Items[0], nil | ||
| } | ||
|
|
||
| // fallback on checking by name | ||
| if len(envResponse.Items) == 0 { | ||
| envResponse, err = environments.Get(octopus, space.ID, environments.EnvironmentQuery{ | ||
| Name: nameOrID, | ||
| Skip: 0, | ||
| Take: math.MaxInt32, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("environment not found. %v", err) | ||
| } | ||
| } | ||
|
|
||
| // if we have multiple responses, this means that the `Name` filter didn't work | ||
| // so we do the filtering ourselves | ||
| switch { | ||
| case len(envResponse.Items) == 0: | ||
| return nil, fmt.Errorf("environment not found") | ||
| case len(envResponse.Items) == 1 && strings.EqualFold(envResponse.Items[0].Name, nameOrID): | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these checks are case-insensitive to match Octopus Server's case-insensitive DB query |
||
| return envResponse.Items[0], nil | ||
| case len(envResponse.Items) > 1: | ||
| for _, item := range envResponse.Items { | ||
| // find an exact match to the partial match responses | ||
| if strings.EqualFold(item.Name, nameOrID) { | ||
| return item, nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // final fallback | ||
| return nil, fmt.Errorf("environment not found") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 case sensitive because ID's should be case-sensitive