Skip to content

Commit 7bc18af

Browse files
authored
Merge pull request #95 from replicatedhq/createIfAbsent
Fix an issue that prevented promoting ship releases with "release create"
2 parents 6344e8d + e04253d commit 7bc18af

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

cli/cmd/release_create.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error {
5555
}
5656

5757
// we check this again below, but lets be explicit and fail fast
58-
if r.args.createReleasePromoteEnsureChannel && r.appType != "kots" {
59-
return errors.Errorf("the flag --ensure-channel is only supported for KOTS applications, app %q is of type %q", r.appID, r.appType)
58+
if r.args.createReleasePromoteEnsureChannel && !(r.appType == "ship" || r.appType == "kots") {
59+
return errors.Errorf("the flag --ensure-channel is only supported for KOTS and Ship applications, app %q is of type %q", r.appID, r.appType)
6060
}
6161

6262
if r.args.createReleaseYaml != "" && r.args.createReleaseYamlFile != "" {
@@ -92,7 +92,7 @@ func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error {
9292
var promoteChanID string
9393
if r.args.createReleasePromote != "" {
9494
var err error
95-
promoteChanID, err = r.getOrCreateChannelForPromotion()
95+
promoteChanID, err = r.getOrCreateChannelForPromotion(r.args.createReleasePromoteEnsureChannel)
9696
if err != nil {
9797
return errors.Wrapf(err, "get or create channel %q for promotion", promoteChanID)
9898
}
@@ -129,7 +129,7 @@ func (r *runners) releaseCreate(cmd *cobra.Command, args []string) error {
129129
return nil
130130
}
131131

132-
func (r *runners) getOrCreateChannelForPromotion() (string, error) {
132+
func (r *runners) getOrCreateChannelForPromotion(createIfAbsent bool) (string, error) {
133133

134134
description := "" // todo: do we want a flag for the desired channel description
135135

@@ -138,7 +138,7 @@ func (r *runners) getOrCreateChannelForPromotion() (string, error) {
138138
r.appType,
139139
r.args.createReleasePromote,
140140
description,
141-
true,
141+
createIfAbsent,
142142
); if err != nil {
143143
return "", errors.Wrapf(err, "get-or-create channel %q", r.args.createReleasePromote)
144144
}

client/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *Client) CreateChannel(appID string, appType string, name string, descri
7272
}
7373
return c.ListChannels(appID, appType)
7474
} else if appType == "ship" {
75-
if err := c.ShipClient.CreateChannel(appID, name, description); err != nil {
75+
if _, err := c.ShipClient.CreateChannel(appID, name, description); err != nil {
7676
return nil, err
7777
}
7878
return c.ShipClient.ListChannels(appID)
@@ -91,7 +91,7 @@ func (c *Client) GetChannelByName(appID string, appType string, name string, des
9191
if appType == "platform" {
9292
return c.PlatformClient.GetChannelByName(appID, name, description, createIfAbsent)
9393
} else if appType == "ship" {
94-
return nil, errors.New("ensure-channel operations are not supported for ship applications")
94+
return c.ShipClient.GetChannelByName(appID, name, description, createIfAbsent)
9595
} else if appType == "kots" {
9696
return c.KotsClient.GetChannelByName(appID, name, description, createIfAbsent)
9797
}

pkg/shipclient/channel.go

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package shipclient
22

33
import (
4+
"fmt"
5+
"github.com/pkg/errors"
46
channels "github.com/replicatedhq/replicated/gen/go/v1"
57
"github.com/replicatedhq/replicated/pkg/graphql"
68
"github.com/replicatedhq/replicated/pkg/types"
@@ -16,6 +18,15 @@ type GraphQLResponseGetChannel struct {
1618
Errors []graphql.GQLError `json:"errors,omitempty"`
1719
}
1820

21+
type GraphQLResponseCreateChannel struct {
22+
Data *ShipCreateChannelData `json:"data,omitempty"`
23+
Errors []graphql.GQLError `json:"errors,omitempty"`
24+
}
25+
26+
type ShipCreateChannelData struct {
27+
ShipChannel *ShipChannel `json:"createChannel"`
28+
}
29+
1930
type ShipGetChannelData struct {
2031
ShipChannel *ShipChannel `json:"getChannel"`
2132
}
@@ -111,8 +122,8 @@ mutation createChannel($appId: String!, $channelName: String!, $description: Str
111122
}
112123
}`
113124

114-
func (c *GraphQLClient) CreateChannel(appID string, name string, description string) error {
115-
response := graphql.ResponseErrorOnly{}
125+
func (c *GraphQLClient) CreateChannel(appID string, name string, description string) (*types.Channel, error) {
126+
response := GraphQLResponseCreateChannel{}
116127

117128
request := graphql.Request{
118129
Query: createChannelQuery,
@@ -124,15 +135,52 @@ func (c *GraphQLClient) CreateChannel(appID string, name string, description str
124135
}
125136

126137
if err := c.ExecuteRequest(request, &response); err != nil {
127-
return err
138+
return nil, err
128139
}
140+
return &types.Channel{
141+
ID: response.Data.ShipChannel.ID,
142+
Name: response.Data.ShipChannel.Name,
143+
Description: response.Data.ShipChannel.Description,
144+
ReleaseSequence: response.Data.ShipChannel.CurrentSequence,
145+
ReleaseLabel: response.Data.ShipChannel.CurrentVersion,
146+
}, nil
147+
}
129148

130-
return nil
149+
func (c *GraphQLClient) GetChannelByName(appID string, name string, description string, create bool) (*types.Channel, error) {
150+
allChannels, err := c.ListChannels(appID)
151+
if err != nil {
152+
return nil, err
153+
}
131154

132-
}
155+
matchingChannels := make([]*types.Channel, 0)
156+
for _, channel := range allChannels {
157+
if channel.ID == name || channel.Name == name {
158+
matchingChannels = append(matchingChannels, &types.Channel{
159+
ID: channel.ID,
160+
Name: channel.Name,
161+
Description: channel.Description,
162+
ReleaseSequence: channel.ReleaseSequence,
163+
ReleaseLabel: channel.ReleaseLabel,
164+
})
165+
}
166+
}
167+
168+
if len(matchingChannels) == 0 {
169+
if create {
170+
channel, err := c.CreateChannel(appID, name, description)
171+
if err != nil {
172+
return nil, errors.Wrapf(err, "create channel %q ", name)
173+
}
174+
return channel, nil
175+
}
176+
177+
return nil, fmt.Errorf("could not find channel %q", name)
178+
}
133179

134-
func ArchiveChannel(appID string, channelID string) error {
135-
return nil
180+
if len(matchingChannels) > 1 {
181+
return nil, fmt.Errorf("channel %q is ambiguous, please use channel ID", name)
182+
}
183+
return matchingChannels[0], nil
136184
}
137185

138186
var getShipChannel = `

0 commit comments

Comments
 (0)