Skip to content

Commit 2fbffbf

Browse files
Refactor client to no longer use resource but rather the environment ids
1 parent ace5aaf commit 2fbffbf

File tree

3 files changed

+81
-64
lines changed

3 files changed

+81
-64
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/AlecAivazis/survey/v2 v2.3.7
77
github.com/MakeNowJust/heredoc/v2 v2.0.1
88
github.com/OctopusDeploy/go-octodiff v1.0.0
9-
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.82.0
9+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.84.2-0.20251006201940-df6027c5e647
1010
github.com/bmatcuk/doublestar/v4 v4.4.0
1111
github.com/briandowns/spinner v1.19.0
1212
github.com/google/uuid v1.3.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ github.com/OctopusDeploy/go-octodiff v1.0.0 h1:U+ORg6azniwwYo+O44giOw6TiD5USk8S4
4848
github.com/OctopusDeploy/go-octodiff v1.0.0/go.mod h1:Mze0+EkOWTgTmi8++fyUc6r0aLZT7qD9gX+31t8MmIU=
4949
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.82.0 h1:4Pc2W74VKp7Qm0uV0Dv99QKqRWg8WriVikdZPBpIZgY=
5050
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.82.0/go.mod h1:J1UdIilp41MRuFl+5xZm88ywFqJGYCCqxqod+/ZH8ko=
51+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.84.2-0.20251005222655-e09b9b4a67c4 h1:JKgbm6nIg5agURwR53yn17s6T6JNWGxGPkpibqldN1o=
52+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.84.2-0.20251005222655-e09b9b4a67c4/go.mod h1:J1UdIilp41MRuFl+5xZm88ywFqJGYCCqxqod+/ZH8ko=
53+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.84.2-0.20251006201940-df6027c5e647 h1:udkmpRbHDRbEc2MgSk/hjRGcI5NR23gjOa0OY2uzVho=
54+
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.84.2-0.20251006201940-df6027c5e647/go.mod h1:J1UdIilp41MRuFl+5xZm88ywFqJGYCCqxqod+/ZH8ko=
5155
github.com/bmatcuk/doublestar/v4 v4.4.0 h1:LmAwNwhjEbYtyVLzjcP/XeVw4nhuScHGkF/XWXnvIic=
5256
github.com/bmatcuk/doublestar/v4 v4.4.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
5357
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=

pkg/cmd/release/deploy/deploy.go

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"github.com/OctopusDeploy/cli/pkg/util/featuretoggle"
8-
"golang.org/x/exp/maps"
97
"io"
108
"sort"
119
"strings"
1210
"time"
1311

12+
"github.com/OctopusDeploy/cli/pkg/util/featuretoggle"
13+
"golang.org/x/exp/maps"
14+
1415
"github.com/OctopusDeploy/cli/pkg/apiclient"
1516

1617
"github.com/AlecAivazis/survey/v2"
@@ -414,9 +415,10 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
414415
// select release
415416

416417
var selectedRelease *releases.Release
418+
var selectedChannel *channels.Channel
417419
if options.ReleaseVersion == "" {
418420
// first we want to ask them to pick a channel just to narrow down the search space for releases (not sent to server)
419-
selectedChannel, err := selectors.Channel(octopus, asker, stdout, "Select channel", selectedProject)
421+
selectedChannel, err = selectors.Channel(octopus, asker, stdout, "Select channel", selectedProject)
420422
if err != nil {
421423
return err
422424
}
@@ -429,6 +431,10 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
429431
if err != nil {
430432
return err
431433
}
434+
selectedChannel, err = channels.GetByID(octopus, space.ID, selectedRelease.ChannelID)
435+
if err != nil {
436+
return err
437+
}
432438
_, _ = fmt.Fprintf(stdout, "Release %s\n", output.Cyan(selectedRelease.Version))
433439
}
434440
options.ReleaseVersion = selectedRelease.Version
@@ -439,67 +445,72 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
439445

440446
indicateMissingPackagesForReleaseFeatureToggleValue, err := featuretoggle.IsToggleEnabled(octopus, "indicate-missing-packages-for-release")
441447
if indicateMissingPackagesForReleaseFeatureToggleValue {
442-
proceed := promptMissingPackages(octopus, stdout, asker, selectedRelease);
448+
proceed := promptMissingPackages(octopus, stdout, asker, selectedRelease)
443449
if !proceed {
444450
return errors.New("aborting deployment creation as requested")
445451
}
446452
}
447453

448454
// machine selection later on needs to refer back to the environments.
449455
// NOTE: this is allowed to remain nil; environments will get looked up later on if needed
450-
var selectedEnvironments []*environments.Environment
451-
if isTenanted {
452-
var selectedEnvironment *environments.Environment
453-
if len(options.Environments) == 0 {
454-
deployableEnvironmentIDs, nextEnvironmentID, err := FindDeployableEnvironmentIDs(octopus, selectedRelease)
455-
if err != nil {
456-
return err
457-
}
458-
selectedEnvironment, err = selectDeploymentEnvironment(asker, octopus, deployableEnvironmentIDs, nextEnvironmentID)
459-
if err != nil {
460-
return err
461-
}
462-
options.Environments = []string{selectedEnvironment.Name} // executions api allows env names, so let's use these instead so they look nice in generated automationcmd
463-
} else {
464-
selectedEnvironment, err = selectors.FindEnvironment(octopus, options.Environments[0])
465-
if err != nil {
466-
return err
456+
var deploymentEnvironmentIds []string
457+
if selectedChannel.Type == channels.ChannelTypeLifecycle {
458+
var selectedEnvironments []*environments.Environment
459+
if isTenanted {
460+
var selectedEnvironment *environments.Environment
461+
if len(options.Environments) == 0 {
462+
deployableEnvironmentIDs, nextEnvironmentID, err := FindDeployableEnvironmentIDs(octopus, selectedRelease)
463+
if err != nil {
464+
return err
465+
}
466+
selectedEnvironment, err = selectDeploymentEnvironment(asker, octopus, deployableEnvironmentIDs, nextEnvironmentID)
467+
if err != nil {
468+
return err
469+
}
470+
options.Environments = []string{selectedEnvironment.Name} // executions api allows env names, so let's use these instead so they look nice in generated automationcmd
471+
} else {
472+
selectedEnvironment, err = selectors.FindEnvironment(octopus, options.Environments[0])
473+
if err != nil {
474+
return err
475+
}
476+
_, _ = fmt.Fprintf(stdout, "Environment %s\n", output.Cyan(selectedEnvironment.Name))
467477
}
468-
_, _ = fmt.Fprintf(stdout, "Environment %s\n", output.Cyan(selectedEnvironment.Name))
469-
}
470-
selectedEnvironments = []*environments.Environment{selectedEnvironment}
478+
selectedEnvironments = []*environments.Environment{selectedEnvironment}
479+
deploymentEnvironmentIds = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.ID })
471480

472-
// ask for tenants and/or tags unless some were specified on the command line
473-
if len(options.Tenants) == 0 && len(options.TenantTags) == 0 {
474-
options.Tenants, options.TenantTags, err = executionscommon.AskTenantsAndTags(asker, octopus, selectedRelease.ProjectID, selectedEnvironments, true)
481+
// ask for tenants and/or tags unless some were specified on the command line
475482
if len(options.Tenants) == 0 && len(options.TenantTags) == 0 {
476-
return errors.New("no tenants or tags available; cannot deploy")
477-
}
478-
if err != nil {
479-
return err
480-
}
481-
} else {
482-
if len(options.Tenants) > 0 {
483-
_, _ = fmt.Fprintf(stdout, "Tenants %s\n", output.Cyan(strings.Join(options.Tenants, ",")))
484-
}
485-
if len(options.TenantTags) > 0 {
486-
_, _ = fmt.Fprintf(stdout, "Tenant Tags %s\n", output.Cyan(strings.Join(options.TenantTags, ",")))
487-
}
488-
}
489-
} else {
490-
if len(options.Environments) == 0 {
491-
deployableEnvironmentIDs, nextEnvironmentID, err := FindDeployableEnvironmentIDs(octopus, selectedRelease)
492-
if err != nil {
493-
return err
494-
}
495-
selectedEnvironments, err = selectDeploymentEnvironments(asker, octopus, deployableEnvironmentIDs, nextEnvironmentID)
496-
if err != nil {
497-
return err
483+
options.Tenants, options.TenantTags, err = executionscommon.AskTenantsAndTags(asker, octopus, selectedRelease.ProjectID, selectedEnvironments, true)
484+
if len(options.Tenants) == 0 && len(options.TenantTags) == 0 {
485+
return errors.New("no tenants or tags available; cannot deploy")
486+
}
487+
if err != nil {
488+
return err
489+
}
490+
} else {
491+
if len(options.Tenants) > 0 {
492+
_, _ = fmt.Fprintf(stdout, "Tenants %s\n", output.Cyan(strings.Join(options.Tenants, ",")))
493+
}
494+
if len(options.TenantTags) > 0 {
495+
_, _ = fmt.Fprintf(stdout, "Tenant Tags %s\n", output.Cyan(strings.Join(options.TenantTags, ",")))
496+
}
498497
}
499-
options.Environments = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.Name })
500498
} else {
501-
if len(options.Environments) > 0 {
502-
_, _ = fmt.Fprintf(stdout, "Environments %s\n", output.Cyan(strings.Join(options.Environments, ",")))
499+
if len(options.Environments) == 0 {
500+
deployableEnvironmentIDs, nextEnvironmentID, err := FindDeployableEnvironmentIDs(octopus, selectedRelease)
501+
if err != nil {
502+
return err
503+
}
504+
selectedEnvironments, err = selectDeploymentEnvironments(asker, octopus, deployableEnvironmentIDs, nextEnvironmentID)
505+
if err != nil {
506+
return err
507+
}
508+
deploymentEnvironmentIds = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.ID })
509+
options.Environments = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.Name })
510+
} else {
511+
if len(options.Environments) > 0 {
512+
_, _ = fmt.Fprintf(stdout, "Environments %s\n", output.Cyan(strings.Join(options.Environments, ",")))
513+
}
503514
}
504515
}
505516
}
@@ -509,17 +520,18 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
509520
return err
510521
}
511522

512-
if len(selectedEnvironments) == 0 { // if the Q&A process earlier hasn't loaded environments already, we need to load them now
513-
selectedEnvironments, err = executionscommon.FindEnvironments(octopus, options.Environments)
523+
if len(deploymentEnvironmentIds) == 0 { // if the Q&A process earlier hasn't loaded environments already, we need to load them now
524+
selectedEnvironments, err := executionscommon.FindEnvironments(octopus, options.Environments)
514525
if err != nil {
515526
return err
516527
}
528+
deploymentEnvironmentIds = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.ID })
517529
}
518530

519531
var deploymentPreviewRequests []deployments.DeploymentPreviewRequest
520-
for _, environment := range selectedEnvironments {
532+
for _, environmentId := range deploymentEnvironmentIds {
521533
preview := deployments.DeploymentPreviewRequest{
522-
EnvironmentId: environment.ID,
534+
EnvironmentId: environmentId,
523535
// We ignore the TenantId here as we're just using the deployments previews for prompted variables.
524536
// Tenant variables do not support prompted variables
525537
TenantId: "",
@@ -632,13 +644,14 @@ func AskQuestions(octopus *octopusApiClient.Client, stdout io.Writer, asker ques
632644
}
633645

634646
if !isDeploymentTargetsSpecified {
635-
if len(selectedEnvironments) == 0 { // if the Q&A process earlier hasn't loaded environments already, we need to load them now
636-
selectedEnvironments, err = executionscommon.FindEnvironments(octopus, options.Environments)
647+
if len(deploymentEnvironmentIds) == 0 { // if the Q&A process earlier hasn't loaded environments already, we need to load them now
648+
selectedEnvironments, err := executionscommon.FindEnvironments(octopus, options.Environments)
637649
if err != nil {
638650
return err
639651
}
652+
deploymentEnvironmentIds = util.SliceTransform(selectedEnvironments, func(env *environments.Environment) string { return env.ID })
640653
}
641-
options.DeploymentTargets, err = askDeploymentTargets(octopus, asker, space.ID, selectedRelease.ID, selectedEnvironments)
654+
options.DeploymentTargets, err = askDeploymentTargets(octopus, asker, space.ID, selectedRelease.ID, deploymentEnvironmentIds)
642655
if err != nil {
643656
return err
644657
}
@@ -656,12 +669,12 @@ func validateDeployment(isTenanted bool, environments []string) error {
656669
return nil
657670
}
658671

659-
func askDeploymentTargets(octopus *octopusApiClient.Client, asker question.Asker, spaceID string, releaseID string, selectedEnvironments []*environments.Environment) ([]string, error) {
672+
func askDeploymentTargets(octopus *octopusApiClient.Client, asker question.Asker, spaceID string, releaseID string, deploymentEnvironmentIds []string) ([]string, error) {
660673
var results []string
661674

662675
// this is what the portal does. Can we do it better? I don't know
663-
for _, env := range selectedEnvironments {
664-
preview, err := deployments.GetReleaseDeploymentPreview(octopus, spaceID, releaseID, env.ID, true)
676+
for _, envID := range deploymentEnvironmentIds {
677+
preview, err := deployments.GetReleaseDeploymentPreview(octopus, spaceID, releaseID, envID, true)
665678
if err != nil {
666679
return nil, err
667680
}
@@ -762,7 +775,7 @@ func promptMissingPackages(octopus *octopusApiClient.Client, stdout io.Writer, a
762775
return true
763776
}
764777

765-
_, _ = fmt.Fprintf(stdout ,"Warning: The following packages are missing from the built-in feed for this release:\n")
778+
_, _ = fmt.Fprintf(stdout, "Warning: The following packages are missing from the built-in feed for this release:\n")
766779
for _, p := range missingPackages {
767780
_, _ = fmt.Fprintf(stdout, " - %s (Version: %s)\n", p.ID, p.Version)
768781
}

0 commit comments

Comments
 (0)