Skip to content

Commit a68cf0e

Browse files
committed
WKS-2076 - Add base64 encoding support for source code
1 parent 984d60a commit a68cf0e

File tree

5 files changed

+110
-17
lines changed

5 files changed

+110
-17
lines changed

commands/common/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ type OptionsMetadata struct {
1313
IsTutorialAvailable bool `json:"isTutorialAvailable"`
1414
IsFeedbackEnabled bool `json:"isFeedbackEnabled"`
1515
IsHistoryEnabled bool `json:"isHistoryEnabled"`
16+
ShouldEncodeSourceCodeInBase64 bool `json:"shouldEncodeSourceCodeInBase64"`
1617
}

commands/common/test_worker_server.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ func NewServerStub(t *testing.T) *ServerStub {
109109
}
110110

111111
type ServerStub struct {
112-
test *testing.T
113-
waitFor time.Duration
114-
token string
115-
projectKey *queryParamStub
116-
workers map[string]*model.WorkerDetails
117-
executionHistory map[string]ExecutionHistoryStub
118-
endpoints []mockhttp.ServerEndpoint
119-
queryParams map[string]queryParamStub
112+
test *testing.T
113+
waitFor time.Duration
114+
token string
115+
projectKey *queryParamStub
116+
workers map[string]*model.WorkerDetails
117+
executionHistory map[string]ExecutionHistoryStub
118+
endpoints []mockhttp.ServerEndpoint
119+
queryParams map[string]queryParamStub
120+
optionsForceBase64 bool
120121
}
121122

122123
func (s *ServerStub) WithT(t *testing.T) *ServerStub {
@@ -293,6 +294,11 @@ func (s *ServerStub) WithOptionsEndpoint() *ServerStub {
293294
return s
294295
}
295296

297+
func (s *ServerStub) WithBase64OptionsEndpoint() *ServerStub {
298+
s.optionsForceBase64 = true
299+
return s.WithOptionsEndpoint()
300+
}
301+
296302
func (s *ServerStub) handleGetAll(res http.ResponseWriter, req *http.Request) {
297303
s.applyDelay()
298304

@@ -492,6 +498,9 @@ func (s *ServerStub) handleGetOptions(res http.ResponseWriter, req *http.Request
492498
res.WriteHeader(http.StatusOK)
493499

494500
options := LoadSampleOptions(s.test)
501+
if s.optionsForceBase64 {
502+
options.ShouldEncodeSourceCodeInBase64 = true
503+
}
495504
_, err := res.Write([]byte(MustJsonMarshal(s.test, options)))
496505
require.NoError(s.test, err)
497506
}

commands/deploy_cmd.go

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

33
import (
4+
"encoding/base64"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -39,6 +40,7 @@ func GetDeployCommand() components.Command {
3940
model.GetChangesVersionFlag(),
4041
model.GetChangesDescriptionFlag(),
4142
model.GetChangesCommitShaFlag(),
43+
model.GetBase64Flag(),
4244
},
4345
Action: func(c *components.Context) error {
4446
server, err := model.GetServerDetails(c)
@@ -80,27 +82,30 @@ func GetDeployCommand() components.Command {
8082
Description: c.GetStringFlagValue(model.FlagChangesDescription),
8183
CommitSha: c.GetStringFlagValue(model.FlagChangesCommitSha),
8284
}
85+
86+
options, err := common.FetchOptions(c, server.GetUrl(), server.GetAccessToken())
87+
if err != nil {
88+
return err
89+
}
90+
8391
if !version.IsEmpty() {
84-
options, err := common.FetchOptions(c, server.GetUrl(), server.GetAccessToken())
85-
if err != nil {
86-
return err
87-
}
8892
if err = common.ValidateVersion(version, options); err != nil {
8993
return err
9094
}
9195
}
92-
return runDeployCommand(c, manifest, actionMeta, version, server.GetUrl(), server.GetAccessToken())
96+
97+
return runDeployCommand(c, manifest, actionMeta, version, server.GetUrl(), server.GetAccessToken(), options.ShouldEncodeSourceCodeInBase64)
9398
},
9499
}
95100
}
96101

97-
func runDeployCommand(ctx *components.Context, manifest *model.Manifest, actionMeta *model.ActionMetadata, version *model.Version, serverURL string, token string) error {
102+
func runDeployCommand(ctx *components.Context, manifest *model.Manifest, actionMeta *model.ActionMetadata, version *model.Version, serverURL string, token string, forceBase64 bool) error {
98103
existingWorker, err := common.FetchWorkerDetails(ctx, serverURL, token, manifest.Name, manifest.ProjectKey)
99104
if err != nil {
100105
return err
101106
}
102107

103-
body, err := prepareDeployRequest(ctx, manifest, actionMeta, version, existingWorker)
108+
body, err := prepareDeployRequest(ctx, manifest, actionMeta, version, existingWorker, forceBase64)
104109
if err != nil {
105110
return err
106111
}
@@ -144,13 +149,17 @@ func runDeployCommand(ctx *components.Context, manifest *model.Manifest, actionM
144149
return err
145150
}
146151

147-
func prepareDeployRequest(ctx *components.Context, manifest *model.Manifest, actionMeta *model.ActionMetadata, version *model.Version, existingWorker *model.WorkerDetails) (*deployRequest, error) {
152+
func prepareDeployRequest(ctx *components.Context, manifest *model.Manifest, actionMeta *model.ActionMetadata, version *model.Version, existingWorker *model.WorkerDetails, forceBase64 bool) (*deployRequest, error) {
148153
sourceCode, err := common.ReadSourceCode(manifest)
149154
if err != nil {
150155
return nil, err
151156
}
152157
sourceCode = common.CleanImports(sourceCode)
153158

159+
if forceBase64 || ctx.GetBoolFlagValue(model.FlagBase64) {
160+
sourceCode = "base64:" + base64.StdEncoding.EncodeToString([]byte(sourceCode))
161+
}
162+
154163
var secrets []*model.Secret
155164

156165
if !ctx.GetBoolFlagValue(model.FlagNoSecrets) {

commands/deploy_cmd_test.go

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package commands
55

66
import (
7+
"encoding/base64"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -37,6 +38,7 @@ func TestDeployCommand(t *testing.T) {
3738
workerName: "wk-0",
3839
serverBehavior: common.NewServerStub(t).
3940
WithGetOneEndpoint().
41+
WithOptionsEndpoint().
4042
WithCreateEndpoint(
4143
expectDeployRequest(
4244
actionsMeta,
@@ -60,6 +62,7 @@ func TestDeployCommand(t *testing.T) {
6062
workerName: "wk-1",
6163
serverBehavior: common.NewServerStub(t).
6264
WithGetOneEndpoint().
65+
WithOptionsEndpoint().
6366
WithUpdateEndpoint(
6467
expectDeployRequest(actionsMeta, "wk-1", "GENERIC_EVENT", ""),
6568
).
@@ -73,6 +76,7 @@ func TestDeployCommand(t *testing.T) {
7376
workerName: "wk-2",
7477
serverBehavior: common.NewServerStub(t).
7578
WithGetOneEndpoint().
79+
WithOptionsEndpoint().
7680
WithUpdateEndpoint(
7781
expectDeployRequest(
7882
actionsMeta,
@@ -102,6 +106,7 @@ func TestDeployCommand(t *testing.T) {
102106
workerName: "wk-1",
103107
serverBehavior: common.NewServerStub(t).
104108
WithGetOneEndpoint().
109+
WithOptionsEndpoint().
105110
WithCreateEndpoint(
106111
expectDeployRequest(actionsMeta, "wk-1", "GENERIC_EVENT", "proj-1"),
107112
),
@@ -113,7 +118,7 @@ func TestDeployCommand(t *testing.T) {
113118
name: "should validate schedule",
114119
workerAction: "SCHEDULED_EVENT",
115120
workerName: "wk-3",
116-
serverBehavior: common.NewServerStub(t).WithGetOneEndpoint(),
121+
serverBehavior: common.NewServerStub(t).WithGetOneEndpoint().WithOptionsEndpoint(),
117122
patchManifest: func(mf *model.Manifest) {
118123
mf.FilterCriteria = &model.FilterCriteria{
119124
Schedule: &model.ScheduleFilterCriteria{
@@ -129,6 +134,7 @@ func TestDeployCommand(t *testing.T) {
129134
commandArgs: []string{"--" + model.FlagTimeout, "500"},
130135
serverBehavior: common.NewServerStub(t).
131136
WithDelay(1 * time.Second).
137+
WithOptionsEndpoint().
132138
WithCreateEndpoint(nil),
133139
wantErr: errors.New("request timed out after 500ms"),
134140
},
@@ -155,6 +161,58 @@ func TestDeployCommand(t *testing.T) {
155161
),
156162
commandArgs: []string{"--" + model.FlagChangesVersion, "version number", "--" + model.FlagChangesDescription, "version description", "--" + model.FlagChangesCommitSha, "version commitsha"},
157163
},
164+
{
165+
name: "create with base64 encoding",
166+
workerAction: "BEFORE_UPLOAD",
167+
workerName: "wk-0",
168+
commandArgs: []string{"--" + model.FlagBase64},
169+
serverBehavior: common.NewServerStub(t).
170+
WithGetOneEndpoint().
171+
WithOptionsEndpoint().
172+
WithCreateEndpoint(
173+
expectDeployRequestBase64(actionsMeta, "wk-0", "BEFORE_UPLOAD", ""),
174+
),
175+
},
176+
{
177+
name: "update with base64 encoding",
178+
workerAction: "GENERIC_EVENT",
179+
workerName: "wk-1",
180+
commandArgs: []string{"--" + model.FlagBase64},
181+
serverBehavior: common.NewServerStub(t).
182+
WithGetOneEndpoint().
183+
WithOptionsEndpoint().
184+
WithUpdateEndpoint(
185+
expectDeployRequestBase64(actionsMeta, "wk-1", "GENERIC_EVENT", ""),
186+
).
187+
WithWorkers(&model.WorkerDetails{
188+
Key: "wk-1",
189+
}),
190+
},
191+
{
192+
name: "create with base64 from options",
193+
workerAction: "BEFORE_UPLOAD",
194+
workerName: "wk-0",
195+
serverBehavior: common.NewServerStub(t).
196+
WithGetOneEndpoint().
197+
WithBase64OptionsEndpoint().
198+
WithCreateEndpoint(
199+
expectDeployRequestBase64(actionsMeta, "wk-0", "BEFORE_UPLOAD", ""),
200+
),
201+
},
202+
{
203+
name: "update with base64 from options",
204+
workerAction: "GENERIC_EVENT",
205+
workerName: "wk-1",
206+
serverBehavior: common.NewServerStub(t).
207+
WithGetOneEndpoint().
208+
WithBase64OptionsEndpoint().
209+
WithUpdateEndpoint(
210+
expectDeployRequestBase64(actionsMeta, "wk-1", "GENERIC_EVENT", ""),
211+
).
212+
WithWorkers(&model.WorkerDetails{
213+
Key: "wk-1",
214+
}),
215+
},
158216
{
159217
name: "fails when version invalid",
160218
serverBehavior: common.NewServerStub(t).
@@ -239,6 +297,17 @@ func expectDeployRequest(actionsMeta common.ActionsMetadata, workerName, actionN
239297
}
240298
}
241299

300+
func expectDeployRequestBase64(actionsMeta common.ActionsMetadata, workerName, actionName, projectKey string, secrets ...*model.Secret) common.BodyValidator {
301+
return func(t require.TestingT, body []byte) {
302+
want := getExpectedDeployRequestForAction(t, actionsMeta, workerName, actionName, projectKey, secrets...)
303+
want.SourceCode = "base64:" + base64.StdEncoding.EncodeToString([]byte(want.SourceCode))
304+
got := &deployRequest{}
305+
err := json.Unmarshal(body, got)
306+
require.NoError(t, err)
307+
assertDeployRequestEquals(t, want, got)
308+
}
309+
}
310+
242311
func getExpectedDeployRequestForAction(
243312
t require.TestingT,
244313
actionsMeta common.ActionsMetadata,

model/flags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
FlagChangesVersion = "changes-version"
2626
FlagChangesDescription = "changes-description"
2727
FlagChangesCommitSha = "changes-commitsha"
28+
FlagBase64 = "base64"
2829
defaultTimeoutMillis = 5000
2930
)
3031

@@ -119,6 +120,10 @@ func GetChangesCommitShaFlag() components.StringFlag {
119120
return components.NewStringFlag(FlagChangesCommitSha, "Commit identifier or your change in your VCS.", components.WithStrDefaultValue(""))
120121
}
121122

123+
func GetBase64Flag() components.BoolFlag {
124+
return components.NewBoolFlag(FlagBase64, "Encode the worker source code in base64 before sending.", components.WithBoolDefaultValue(false))
125+
}
126+
122127
func GetServerDetails(c *components.Context) (*config.ServerDetails, error) {
123128
serverURLFromEnv, envHasServerURL := os.LookupEnv(EnvKeyServerURL)
124129
accessTokenFromEnv, envHasAccessToken := os.LookupEnv(EnvKeyAccessToken)

0 commit comments

Comments
 (0)