-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add update-variables commands for releases and runbook snapshots #589
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
Open
justin-newman
wants to merge
6
commits into
OctopusDeploy:main
Choose a base branch
from
justin-newman:feat/update-variable-snapshots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b76b70d
feat: add update-variables commands for release and runbook snapshots
justin-newman 86b0de6
Potential fix for pull request finding
justin-newman dd88da0
fix: return error if reading HTTP response body fails when updating v…
justin-newman f2adc90
fix: indicate when published snapshot is used and propagate errors in…
justin-newman a79d880
feat: add tests for release and runbook snapshot update-variables com…
justin-newman 4ad8b0b
feat: validate required project/version/runbook flags for update-vari…
justin-newman 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 |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package update_variables | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/MakeNowJust/heredoc/v2" | ||
| "github.com/OctopusDeploy/cli/pkg/cmd" | ||
| "github.com/OctopusDeploy/cli/pkg/cmd/release/progression/shared" | ||
| "github.com/OctopusDeploy/cli/pkg/constants" | ||
| "github.com/OctopusDeploy/cli/pkg/factory" | ||
| "github.com/OctopusDeploy/cli/pkg/output" | ||
| "github.com/OctopusDeploy/cli/pkg/question/selectors" | ||
| "github.com/OctopusDeploy/cli/pkg/util" | ||
| "github.com/OctopusDeploy/cli/pkg/util/flag" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| FlagProject = "project" | ||
| FlagVersion = "version" | ||
| FlagAliasReleaseNumberLegacy = "releaseNumber" | ||
| ) | ||
|
|
||
| type UpdateVariablesFlags struct { | ||
| Project *flag.Flag[string] | ||
| Version *flag.Flag[string] | ||
| } | ||
|
|
||
| func NewUpdateVariablesFlags() *UpdateVariablesFlags { | ||
| return &UpdateVariablesFlags{ | ||
| Project: flag.New[string](FlagProject, false), | ||
| Version: flag.New[string](FlagVersion, false), | ||
| } | ||
| } | ||
|
|
||
| type UpdateVariablesOptions struct { | ||
| *UpdateVariablesFlags | ||
| *cmd.Dependencies | ||
| } | ||
|
|
||
| func NewUpdateVariablesOptions(flags *UpdateVariablesFlags, dependencies *cmd.Dependencies) *UpdateVariablesOptions { | ||
| return &UpdateVariablesOptions{ | ||
| UpdateVariablesFlags: flags, | ||
| Dependencies: dependencies, | ||
| } | ||
| } | ||
|
|
||
| func NewCmdUpdateVariables(f factory.Factory) *cobra.Command { | ||
| updateVariablesFlags := NewUpdateVariablesFlags() | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "update-variables", | ||
| Short: "Update the variable snapshot for a release", | ||
| Long: "Update the variable snapshot for a release in Octopus Deploy", | ||
| Example: heredoc.Docf(` | ||
| $ %[1]s release update-variables --project MyProject --version 1.2.3 | ||
| $ %[1]s release update-variables -p MyProject -v 1.2.3 | ||
| `, constants.ExecutableName), | ||
| RunE: func(c *cobra.Command, args []string) error { | ||
| opts := NewUpdateVariablesOptions(updateVariablesFlags, cmd.NewDependencies(f, c)) | ||
| return updateVariablesRun(opts) | ||
| }, | ||
| } | ||
|
|
||
| flags := cmd.Flags() | ||
| flags.StringVarP(&updateVariablesFlags.Project.Value, updateVariablesFlags.Project.Name, "p", "", "Name or ID of the project") | ||
| flags.StringVarP(&updateVariablesFlags.Version.Value, updateVariablesFlags.Version.Name, "v", "", "Release version/number") | ||
|
|
||
| flags.SortFlags = false | ||
|
|
||
| flagAliases := make(map[string][]string, 1) | ||
| util.AddFlagAliasesString(flags, FlagVersion, flagAliases, FlagAliasReleaseNumberLegacy) | ||
|
|
||
| cmd.PreRunE = func(cmd *cobra.Command, args []string) error { | ||
| util.ApplyFlagAliases(cmd.Flags(), flagAliases) | ||
| return nil | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func updateVariablesRun(opts *UpdateVariablesOptions) error { | ||
| if !opts.NoPrompt { | ||
| if err := PromptMissing(opts); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if opts.Project.Value == "" { | ||
| return errors.New("project must be specified") | ||
| } | ||
| if opts.Version.Value == "" { | ||
| return errors.New("version must be specified") | ||
| } | ||
|
|
||
| releaseID, err := shared.GetReleaseID(opts.Client, opts.Client.GetSpaceID(), opts.Project.Value, opts.Version.Value) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
|
justin-newman marked this conversation as resolved.
|
||
| path := fmt.Sprintf("/api/%s/releases/%s/snapshot-variables", opts.Client.GetSpaceID(), releaseID) | ||
| req, err := http.NewRequest(http.MethodPost, path, nil) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resp, err := opts.Client.HttpSession().DoRawRequest(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
|
Comment on lines
+105
to
+115
Contributor
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. This functionality should be implemented in our go client so that it can be used in other scenarios than just the cli. |
||
|
|
||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
| body, readErr := io.ReadAll(resp.Body) | ||
| if readErr != nil { | ||
| return fmt.Errorf("failed to update variable snapshot (HTTP %d) and failed to read response body: %w", resp.StatusCode, readErr) | ||
| } | ||
| return fmt.Errorf("failed to update variable snapshot (HTTP %d): %s", resp.StatusCode, string(body)) | ||
| } | ||
|
|
||
| fmt.Fprintf(opts.Out, "Successfully updated variable snapshot for release '%s' (%s)\n", opts.Version.Value, output.Dim(releaseID)) | ||
| link := output.Bluef("%s/app#/%s/releases/%s", opts.Host, opts.Space.GetID(), releaseID) | ||
| fmt.Fprintf(opts.Out, "View this release on Octopus Deploy: %s\n", link) | ||
|
|
||
| if !opts.NoPrompt { | ||
| autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.GetSpaceNameOrEmpty(), opts.Project, opts.Version) | ||
| fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
justin-newman marked this conversation as resolved.
|
||
|
|
||
| func PromptMissing(opts *UpdateVariablesOptions) error { | ||
| var selectedProject *projects.Project | ||
| var err error | ||
|
|
||
| if opts.Project.Value == "" { | ||
| selectedProject, err = selectors.Project("Select the project containing the release", opts.Client, opts.Ask) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opts.Project.Value = selectedProject.GetName() | ||
| } else { | ||
| selectedProject, err = selectors.FindProject(opts.Client, opts.Project.Value) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if opts.Version.Value == "" { | ||
| selectedRelease, err := shared.SelectRelease(opts.Client, selectedProject, opts.Ask, "Update Variables for") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opts.Version.Value = selectedRelease.Version | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
180 changes: 180 additions & 0 deletions
180
pkg/cmd/release/update_variables/update_variables_test.go
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 |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| package update_variables_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/AlecAivazis/survey/v2" | ||
| cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root" | ||
| "github.com/OctopusDeploy/cli/pkg/question" | ||
| "github.com/OctopusDeploy/cli/test/fixtures" | ||
| "github.com/OctopusDeploy/cli/test/testutil" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/releases" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" | ||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| var rootResource = testutil.NewRootResource() | ||
|
|
||
| func TestReleaseUpdateVariables(t *testing.T) { | ||
| const spaceID = "Spaces-1" | ||
| const fireProjectID = "Projects-22" | ||
|
|
||
| space1 := fixtures.NewSpace(spaceID, "Default Space") | ||
|
|
||
| fireProject := fixtures.NewProject(spaceID, fireProjectID, "Fire Project", "Lifecycles-1", "ProjectGroups-1", "") | ||
| rDefault21 := fixtures.NewRelease(spaceID, "Releases-21", "2.1", fireProjectID, "Channels-1") | ||
| rDefault20 := fixtures.NewRelease(spaceID, "Releases-20", "2.0", fireProjectID, "Channels-1") | ||
|
|
||
| expectProjectLookup := func(t *testing.T, api *testutil.MockHttpServer) { | ||
| api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Fire Project").RespondWithStatus(404, "NotFound", nil) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects?partialName=Fire+Project"). | ||
| RespondWith(resources.Resources[*projects.Project]{ | ||
| Items: []*projects.Project{fireProject}, | ||
| }) | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| run func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) | ||
| }{ | ||
| {"noprompt: missing --project returns clear error", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
| cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
| defer api.Close() | ||
| rootCmd.SetArgs([]string{"release", "update-variables", "--version", "2.1", "--no-prompt"}) | ||
| return rootCmd.ExecuteC() | ||
| }) | ||
|
|
||
| api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
|
||
| _, err := testutil.ReceivePair(cmdReceiver) | ||
| assert.EqualError(t, err, "project must be specified") | ||
| }}, | ||
|
|
||
| {"noprompt: missing --version returns clear error", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
| cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
| defer api.Close() | ||
| rootCmd.SetArgs([]string{"release", "update-variables", "--project", fireProject.Name, "--no-prompt"}) | ||
| return rootCmd.ExecuteC() | ||
| }) | ||
|
|
||
| api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
|
||
| _, err := testutil.ReceivePair(cmdReceiver) | ||
| assert.EqualError(t, err, "version must be specified") | ||
| }}, | ||
|
|
||
| {"noprompt: posts to snapshot-variables endpoint and prints success", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
| cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
| defer api.Close() | ||
| rootCmd.SetArgs([]string{"release", "update-variables", "--project", fireProject.Name, "--version", "2.1", "--no-prompt"}) | ||
| return rootCmd.ExecuteC() | ||
| }) | ||
|
|
||
| expectProjectLookup(t, api) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Projects-22/releases/2.1").RespondWith(rDefault21) | ||
| api.ExpectRequest(t, "POST", "/api/Spaces-1/releases/Releases-21/snapshot-variables").RespondWith(nil) | ||
|
|
||
| _, err := testutil.ReceivePair(cmdReceiver) | ||
| assert.Nil(t, err) | ||
| assert.Contains(t, stdOut.String(), "Successfully updated variable snapshot for release '2.1'") | ||
| assert.Contains(t, stdOut.String(), "Releases-21") | ||
| assert.NotContains(t, stdOut.String(), "Automation Command:") | ||
| assert.Equal(t, "", stdErr.String()) | ||
| }}, | ||
|
|
||
| {"noprompt: legacy --releaseNumber alias maps to --version", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
| cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
| defer api.Close() | ||
| rootCmd.SetArgs([]string{"release", "update-variables", "--project", fireProject.Name, "--releaseNumber", "2.1", "--no-prompt"}) | ||
| return rootCmd.ExecuteC() | ||
| }) | ||
|
|
||
| expectProjectLookup(t, api) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Projects-22/releases/2.1").RespondWith(rDefault21) | ||
| api.ExpectRequest(t, "POST", "/api/Spaces-1/releases/Releases-21/snapshot-variables").RespondWith(nil) | ||
|
|
||
| _, err := testutil.ReceivePair(cmdReceiver) | ||
| assert.Nil(t, err) | ||
| assert.Contains(t, stdOut.String(), "Successfully updated variable snapshot for release '2.1'") | ||
| }}, | ||
|
|
||
| {"noprompt: server returns non-2xx status returns wrapped error", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
| cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
| defer api.Close() | ||
| rootCmd.SetArgs([]string{"release", "update-variables", "--project", fireProject.Name, "--version", "2.1", "--no-prompt"}) | ||
| return rootCmd.ExecuteC() | ||
| }) | ||
|
|
||
| expectProjectLookup(t, api) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Projects-22/releases/2.1").RespondWith(rDefault21) | ||
| api.ExpectRequest(t, "POST", "/api/Spaces-1/releases/Releases-21/snapshot-variables"). | ||
| RespondWithStatus(500, "500 Internal Server Error", "boom") | ||
|
|
||
| _, err := testutil.ReceivePair(cmdReceiver) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "failed to update variable snapshot (HTTP 500)") | ||
| assert.Contains(t, err.Error(), "boom") | ||
| }}, | ||
|
|
||
| {"interactive: prompts for project and release then posts", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
| cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
| defer api.Close() | ||
| rootCmd.SetArgs([]string{"release", "update-variables"}) | ||
| return rootCmd.ExecuteC() | ||
| }) | ||
|
|
||
| api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
|
||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/all").RespondWith([]*projects.Project{fireProject}) | ||
| _ = qa.ExpectQuestion(t, &survey.Select{ | ||
| Message: "Select the project containing the release", | ||
| Options: []string{fireProject.Name}, | ||
| }).AnswerWith(fireProject.Name) | ||
|
|
||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Projects-22/releases"). | ||
| RespondWith(resources.Resources[*releases.Release]{ | ||
| Items: []*releases.Release{rDefault21, rDefault20}, | ||
| }) | ||
| _ = qa.ExpectQuestion(t, &survey.Select{ | ||
| Message: "Select Release to Update Variables for Progression for", | ||
| Options: []string{rDefault21.Version, rDefault20.Version}, | ||
| }).AnswerWith(rDefault21.Version) | ||
|
|
||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Fire Project").RespondWithStatus(404, "NotFound", nil) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects?partialName=Fire+Project"). | ||
| RespondWith(resources.Resources[*projects.Project]{ | ||
| Items: []*projects.Project{fireProject}, | ||
| }) | ||
| api.ExpectRequest(t, "GET", "/api/Spaces-1/projects/Projects-22/releases/2.1").RespondWith(rDefault21) | ||
| api.ExpectRequest(t, "POST", "/api/Spaces-1/releases/Releases-21/snapshot-variables").RespondWith(nil) | ||
|
|
||
| _, err := testutil.ReceivePair(cmdReceiver) | ||
| assert.Nil(t, err) | ||
| assert.Contains(t, stdOut.String(), "Successfully updated variable snapshot for release '2.1'") | ||
| assert.Contains(t, stdOut.String(), "Automation Command:") | ||
| assert.Contains(t, stdOut.String(), "--project 'Fire Project'") | ||
| assert.Contains(t, stdOut.String(), "--version '2.1'") | ||
| }}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} | ||
| api, qa := testutil.NewMockServerAndAsker() | ||
| askProvider := question.NewAskProvider(qa.AsAsker()) | ||
| fac := testutil.NewMockFactoryWithSpaceAndPrompt(api, space1, askProvider) | ||
| rootCmd := cmdRoot.NewCmdRoot(fac, nil, askProvider) | ||
| rootCmd.SetOut(stdout) | ||
| rootCmd.SetErr(stderr) | ||
| test.run(t, api, qa, rootCmd, stdout, stderr) | ||
| }) | ||
| } | ||
| } |
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
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.
As this is a new command that has not previously existed, there's no need for a "legacy" alias.
The "legacy" aliases were added for backwards compatibility with commands that existed in the old .NET CLI.