Skip to content

Commit

Permalink
fix(venona): add missing query param
Browse files Browse the repository at this point in the history
  • Loading branch information
masontikhonov committed Nov 27, 2024
1 parent 7283b9c commit a7d08bb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
26 changes: 18 additions & 8 deletions venona/pkg/codefresh/codefresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
26 changes: 25 additions & 1 deletion venona/pkg/codefresh/codefresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down

0 comments on commit a7d08bb

Please sign in to comment.