Skip to content

Commit 4924e96

Browse files
authored
Merge pull request #162 from replicatedhq/divolgin/channels
Don't list all channels when making a kots release
2 parents 9e8c21a + 7482e11 commit 4924e96

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

cli/cmd/channel_ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (r *runners) InitChannelList(parent *cobra.Command) {
1717
}
1818

1919
func (r *runners) channelList(cmd *cobra.Command, args []string) error {
20-
channels, err := r.api.ListChannels(r.appID, r.appType, r.appSlug)
20+
channels, err := r.api.ListChannels(r.appID, r.appType, r.appSlug, "")
2121
if err != nil {
2222
return err
2323
}

cli/cmd/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cmd
22

33
import (
4-
"github.com/pkg/errors"
54
"io"
65
"os"
76
"path/filepath"
87
"text/tabwriter"
98

9+
"github.com/pkg/errors"
10+
1011
"github.com/replicatedhq/replicated/pkg/kotsclient"
1112
"github.com/replicatedhq/replicated/pkg/shipclient"
1213

@@ -46,6 +47,11 @@ func init() {
4647
if enterpriseOriginFromEnv != "" {
4748
enterpriseOrigin = enterpriseOriginFromEnv
4849
}
50+
51+
graphqlOriginFromEnv := os.Getenv("REPLICATED_GRAPHQL_ORIGIN")
52+
if graphqlOriginFromEnv != "" {
53+
graphqlOrigin = graphqlOriginFromEnv
54+
}
4955
}
5056

5157
// RootCmd represents the base command when called without any subcommands

client/channel.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package client
22

33
import (
44
"fmt"
5-
"github.com/pkg/errors"
5+
"strings"
66

7+
"github.com/pkg/errors"
78
channels "github.com/replicatedhq/replicated/gen/go/v1"
89
"github.com/replicatedhq/replicated/pkg/types"
910
)
1011

11-
func (c *Client) ListChannels(appID string, appType string, appSlug string) ([]types.Channel, error) {
12+
func (c *Client) ListChannels(appID string, appType string, appSlug string, channelName string) ([]types.Channel, error) {
1213

1314
if appType == "platform" {
1415
platformChannels, err := c.PlatformClient.ListChannels(appID)
@@ -33,7 +34,7 @@ func (c *Client) ListChannels(appID string, appType string, appSlug string) ([]t
3334
} else if appType == "ship" {
3435
return c.ShipClient.ListChannels(appID)
3536
} else if appType == "kots" {
36-
return c.KotsHTTPClient.ListChannels(appID, appSlug)
37+
return c.KotsHTTPClient.ListChannels(appID, appSlug, channelName)
3738
}
3839

3940
return nil, errors.New("unknown app type")
@@ -71,7 +72,7 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
7172
if err := c.PlatformClient.CreateChannel(appID, name, description); err != nil {
7273
return nil, err
7374
}
74-
return c.ListChannels(appID, appType, appSlug)
75+
return c.ListChannels(appID, appType, appSlug, name)
7576
} else if appType == "ship" {
7677
if _, err := c.ShipClient.CreateChannel(appID, name, description); err != nil {
7778
return nil, err
@@ -81,32 +82,47 @@ func (c *Client) CreateChannel(appID string, appType string, appSlug string, nam
8182
if _, err := c.KotsClient.CreateChannel(appID, name, description); err != nil {
8283
return nil, err
8384
}
84-
return c.KotsHTTPClient.ListChannels(appID, appSlug)
85+
return c.KotsHTTPClient.ListChannels(appID, appSlug, name)
8586
}
8687

8788
return nil, errors.New("unknown app type")
8889
}
8990

90-
func (c *Client) GetOrCreateChannelByName(appID string, appType string, appSlug string, name string, description string, createIfAbsent bool) (*types.Channel, error) {
91-
allChannels, err := c.ListChannels(appID, appType, appSlug)
91+
func (c *Client) GetOrCreateChannelByName(appID string, appType string, appSlug string, nameOrID string, description string, createIfAbsent bool) (*types.Channel, error) {
92+
93+
gqlNotFoundErr := fmt.Sprintf("channel %s not found", nameOrID)
94+
channel, _, err := c.GetChannel(appID, appType, nameOrID)
95+
if err == nil {
96+
return &types.Channel{
97+
ID: channel.Id,
98+
Name: channel.Name,
99+
Description: channel.Description,
100+
ReleaseSequence: channel.ReleaseSequence,
101+
ReleaseLabel: channel.ReleaseLabel,
102+
}, nil
103+
} else if !strings.Contains(err.Error(), gqlNotFoundErr) {
104+
return nil, errors.Wrap(err, "get channel")
105+
}
106+
107+
allChannels, err := c.ListChannels(appID, appType, appSlug, nameOrID)
92108
if err != nil {
93109
return nil, err
94110
}
95111

96-
foundChannel, numMatching, err := c.findChannel(allChannels, name)
112+
foundChannel, numMatching, err := c.findChannel(allChannels, nameOrID)
97113

98114
if numMatching == 0 && createIfAbsent {
99-
updatedListOfChannels, err := c.CreateChannel(appID, appType, appSlug, name, description)
115+
updatedListOfChannels, err := c.CreateChannel(appID, appType, appSlug, nameOrID, description)
100116
if err != nil {
101-
return nil, errors.Wrapf(err, "create channel %q ", name)
117+
return nil, errors.Wrapf(err, "create channel %q ", nameOrID)
102118
}
103119
// for some reason CreateChannel returns the list of all channels,
104120
// so now we gotta go find the channel we just created
105-
channel, _, err := c.findChannel(updatedListOfChannels, name)
106-
return channel, errors.Wrapf(err, "find channel %q", name)
121+
channel, _, err := c.findChannel(updatedListOfChannels, nameOrID)
122+
return channel, errors.Wrapf(err, "find channel %q", nameOrID)
107123
}
108124

109-
return foundChannel, errors.Wrapf(err, "find channel %q", name)
125+
return foundChannel, errors.Wrapf(err, "find channel %q", nameOrID)
110126
}
111127

112128
func (c *Client) GetChannelByName(appID string, appType string, appSlug string, name string) (*types.Channel, error) {

pkg/kotsclient/channel.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package kotsclient
22

33
import (
44
"fmt"
5+
"net/http"
6+
"net/url"
7+
"os"
8+
59
"github.com/pkg/errors"
610
channels "github.com/replicatedhq/replicated/gen/go/v1"
711
"github.com/replicatedhq/replicated/pkg/graphql"
812
"github.com/replicatedhq/replicated/pkg/types"
9-
"net/http"
10-
"os"
1113
)
1214

1315
type GraphQLResponseGetChannel struct {
@@ -94,9 +96,13 @@ type ListChannelsResponse struct {
9496
Channels []*KotsChannel `json:"channels"`
9597
}
9698

97-
func (c *VendorV3Client) ListChannels(appID string, appSlug string) ([]types.Channel, error) {
99+
func (c *VendorV3Client) ListChannels(appID string, appSlug string, channelName string) ([]types.Channel, error) {
98100
var response = ListChannelsResponse{}
99-
url := fmt.Sprintf("/v3/app/%s/channels", appID)
101+
102+
v := url.Values{}
103+
v.Set("channelName", channelName)
104+
105+
url := fmt.Sprintf("/v3/app/%s/channels?%s", appID, v.Encode())
100106
err := c.DoJSON("GET", url, http.StatusOK, nil, &response)
101107
if err != nil {
102108
return nil, errors.Wrap(err, "list channels")

0 commit comments

Comments
 (0)