Skip to content

Commit

Permalink
fix #5699: fix parisng of tasks env vars in go
Browse files Browse the repository at this point in the history
  • Loading branch information
akosyakov authored and roboquat committed Sep 17, 2021
1 parent 724e72c commit afd2cb3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
16 changes: 8 additions & 8 deletions components/gitpod-protocol/go/gitpod-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1827,14 +1827,14 @@ type ResolvePluginsParams struct {

// TaskConfig is the TaskConfig message type
type TaskConfig struct {
Before string `json:"before,omitempty"`
Command string `json:"command,omitempty"`
Env map[string]string `json:"env,omitempty"`
Init string `json:"init,omitempty"`
Name string `json:"name,omitempty"`
OpenIn string `json:"openIn,omitempty"`
OpenMode string `json:"openMode,omitempty"`
Prebuild string `json:"prebuild,omitempty"`
Before string `json:"before,omitempty"`
Command string `json:"command,omitempty"`
Env map[string]interface{} `json:"env,omitempty"`
Init string `json:"init,omitempty"`
Name string `json:"name,omitempty"`
OpenIn string `json:"openIn,omitempty"`
OpenMode string `json:"openMode,omitempty"`
Prebuild string `json:"prebuild,omitempty"`
}

// VSCodeConfig is the VSCodeConfig message type
Expand Down
2 changes: 1 addition & 1 deletion components/gitpod-protocol/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ export interface TaskConfig {
init?: string;
prebuild?: string;
command?: string;
env?: { [env: string]: string };
env?: { [env: string]: any };
openIn?: 'bottom' | 'main' | 'left' | 'right';
openMode?: 'split-top' | 'split-left' | 'split-right' | 'split-bottom' | 'tab-before' | 'tab-after';
}
Expand Down
16 changes: 8 additions & 8 deletions components/supervisor/pkg/supervisor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,14 @@ type WorkspaceGitpodToken struct {

// TaskConfig defines gitpod task shape
type TaskConfig struct {
Name *string `json:"name,omitempty"`
Before *string `json:"before,omitempty"`
Init *string `json:"init,omitempty"`
Prebuild *string `json:"prebuild,omitempty"`
Command *string `json:"command,omitempty"`
Env *map[string]string `json:"env,omitempty"`
OpenIn *string `json:"openIn,omitempty"`
OpenMode *string `json:"openMode,omitempty"`
Name *string `json:"name,omitempty"`
Before *string `json:"before,omitempty"`
Init *string `json:"init,omitempty"`
Prebuild *string `json:"prebuild,omitempty"`
Command *string `json:"command,omitempty"`
Env *map[string]interface{} `json:"env,omitempty"`
OpenIn *string `json:"openIn,omitempty"`
OpenMode *string `json:"openMode,omitempty"`
}

// Validate validates this configuration
Expand Down
11 changes: 10 additions & 1 deletion components/supervisor/pkg/supervisor/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package supervisor
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -239,7 +240,15 @@ func (tm *tasksManager) Run(ctx context.Context, wg *sync.WaitGroup, successChan
taskLog.Info("starting a task terminal...")
openRequest := &api.OpenTerminalRequest{}
if t.config.Env != nil {
openRequest.Env = *t.config.Env
openRequest.Env = make(map[string]string, len(*t.config.Env))
for key, value := range *t.config.Env {
v, err := json.Marshal(value)
if err != nil {
taskLog.WithError(err).WithField("key", key).Error("cannot marshal env var")
} else {
openRequest.Env[key] = string(v)
}
}
}
var readTimeout time.Duration
if !tm.config.isHeadless() {
Expand Down
16 changes: 16 additions & 0 deletions components/supervisor/pkg/supervisor/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import (
var skipCommand = "echo \"skip\""
var failCommand = "exit 1"

var testEnv = &map[string]interface{}{
"object": map[string]interface{}{"baz": 3},
}
var testEnvCommand = `test $object == "{\"baz\":3}"`

func TestTaskManager(t *testing.T) {
log.Log.Logger.SetLevel(logrus.FatalLevel)
tests := []struct {
Expand Down Expand Up @@ -99,6 +104,17 @@ func TestTaskManager(t *testing.T) {
Success: false,
},
},
{
Desc: "env var parsing",
Headless: true,
Source: csapi.WorkspaceInitFromOther,
GitpodTasks: &[]TaskConfig{{Init: &testEnvCommand, Env: testEnv}},

ExpectedReporter: testHeadlessTaskProgressReporter{
Done: true,
Success: true,
},
},
}
for _, test := range tests {
t.Run(test.Desc, func(t *testing.T) {
Expand Down

0 comments on commit afd2cb3

Please sign in to comment.