diff --git a/venona/pkg/codefresh/codefresh.go b/venona/pkg/codefresh/codefresh.go index a794ab9b..dc653c01 100644 --- a/venona/pkg/codefresh/codefresh.go +++ b/venona/pkg/codefresh/codefresh.go @@ -80,7 +80,10 @@ func New(opts Options) Codefresh { // Tasks get from Codefresh all latest tasks func (c cf) Tasks(ctx context.Context) (task.Tasks, error) { - res, err := c.doRequest(ctx, "GET", nil, "api", "agent", c.agentID, "tasks") + query := map[string]string{ + "waitForStatusReport": "true", + } + res, err := c.doRequest(ctx, "GET", nil, query, "api", "agent", c.agentID, "tasks") if err != nil { return nil, err } @@ -99,7 +102,7 @@ func (c cf) ReportTaskStatus(ctx context.Context, id task.Id, status task.TaskSt return fmt.Errorf("failed marshalling when reporting task status: %w", err) } - _, err = c.doRequest(ctx, "POST", bytes.NewBuffer(s), "api", "agent", c.agentID, "tasks", string(id), "statuses") + _, err = c.doRequest(ctx, "POST", bytes.NewBuffer(s), nil, "api", "agent", c.agentID, "tasks", string(id), "statuses") if err != nil { return fmt.Errorf("failed sending request when reporting task status: %w", err) } @@ -119,7 +122,7 @@ func (c cf) ReportStatus(ctx context.Context, status AgentStatus) error { return fmt.Errorf("failed marshalling when reporting status: %w", err) } - _, err = c.doRequest(ctx, "PUT", bytes.NewBuffer(s), "api", "agent", c.agentID, "status") + _, err = c.doRequest(ctx, "PUT", bytes.NewBuffer(s), nil, "api", "agent", c.agentID, "status") if err != nil { return fmt.Errorf("failed sending request when reporting status: %w", err) } @@ -134,7 +137,7 @@ func (c cf) buildErrorFromResponse(status int, body []byte) error { } } -func (c cf) prepareURL(paths ...string) (*url.URL, error) { +func (c cf) prepareURL(query map[string]string, paths ...string) (*url.URL, error) { u, err := url.Parse(c.host) if err != nil { return nil, err @@ -150,11 +153,18 @@ func (c cf) prepareURL(paths ...string) (*url.URL, error) { u.Path = path.Join(accPath...) u.RawPath = path.Join(accRawPath...) + + rawQuery := url.Values{} + for k, v := range query { + rawQuery.Set(k, v) + } + u.RawQuery = rawQuery.Encode() + return u, nil } -func (c cf) prepareRequest(method string, data io.Reader, apis ...string) (*http.Request, error) { - u, err := c.prepareURL(apis...) +func (c cf) prepareRequest(method string, data io.Reader, query map[string]string, apis ...string) (*http.Request, error) { + u, err := c.prepareURL(query, apis...) if err != nil { return nil, err } @@ -173,8 +183,8 @@ func (c cf) prepareRequest(method string, data io.Reader, apis ...string) (*http return req, nil } -func (c cf) doRequest(ctx context.Context, method string, body io.Reader, apis ...string) ([]byte, error) { - req, err := c.prepareRequest(method, body, apis...) +func (c cf) doRequest(ctx context.Context, method string, body io.Reader, query map[string]string, apis ...string) ([]byte, error) { + req, err := c.prepareRequest(method, body, query, apis...) if err != nil { return nil, err } diff --git a/venona/pkg/codefresh/codefresh_test.go b/venona/pkg/codefresh/codefresh_test.go index b841d146..ed5672d4 100644 --- a/venona/pkg/codefresh/codefresh_test.go +++ b/venona/pkg/codefresh/codefresh_test.go @@ -69,6 +69,7 @@ func Test_cf_prepareURL(t *testing.T) { tests := map[string]struct { fields args paths []string + query map[string]string want *url.URL wantErr bool }{ @@ -94,6 +95,29 @@ func Test_cf_prepareURL(t *testing.T) { wantErr: false, want: mustURL("http://url/docker:desktop%2Fserver"), }, + "Append query": { + query: map[string]string{ + "key": "value", + "keyTwo": "valueTwo", + }, + paths: []string{"docker:desktop/server"}, + fields: args{ + host: "http://url", + }, + wantErr: false, + want: mustURL("http://url/docker:desktop%2Fserver?key=value&keyTwo=valueTwo"), + }, + "Escape query": { + query: map[string]string{ + "ke+y": "va+lu=e", + }, + paths: []string{"docker:desktop/server"}, + fields: args{ + host: "http://url", + }, + wantErr: false, + want: mustURL("http://url/docker:desktop%2Fserver?ke%2By=va%2Blu%3De"), + }, } for name, tt := range tests { t.Run(name, func(t *testing.T) { @@ -103,7 +127,7 @@ func Test_cf_prepareURL(t *testing.T) { agentID: tt.fields.agentID, httpClient: tt.fields.httpClient, } - url, err := c.prepareURL(tt.paths...) + url, err := c.prepareURL(tt.query, tt.paths...) if tt.wantErr { assert.Error(t, err) }