From 5a78644de2c0fddfc928e103ee78ddb9c1bc9050 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 15:41:16 +0300 Subject: [PATCH 1/9] interpolated mutation --- pkg/codefresh/argo_runtime.go | 56 ++++++++++++++++++++++++++++++++--- pkg/codefresh/codefresh.go | 1 + 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index f3131ab..d649539 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -11,6 +11,7 @@ import ( type ( IArgoRuntimeAPI interface { List() ([]model.Runtime, error) + Create(runtimeName string) (*model.RuntimeCreationResponse, error) } argoRuntime struct { codefresh *codefresh @@ -21,19 +22,65 @@ type ( } Errors []graphqlError } + + graphQlRuntimeCreationResponse struct { + Data struct { + Runtime model.RuntimeCreationResponse + } + // TODO: Errors + } ) func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { return &argoRuntime{codefresh: codefresh} } -func (r *argoRuntime) List() ([]model.Runtime, error) { +func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { // TODO: should also return error + type forJsonData interface{} + + // the newlines are necessary + var interpolatedMutation forJsonData = fmt.Sprintf("mutation {\n runtime(name: \"%s\") {\n id\n newAccessToken\n }\n}\n", runtimeName) + + jsonData := map[string]interface{}{ + "query": interpolatedMutation, + } + + response, err := r.codefresh.requestAPI(&requestOptions{ + method: "POST", + path: "/argo/api/graphql", + body: jsonData, + }) + + if err != nil { + fmt.Printf("The HTTP request failed with error %s\n", err) + return nil, err + } + defer response.Body.Close() + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Printf("failed to read from response body") + return nil, err + } + res := graphQlRuntimeCreationResponse{} + + err = json.Unmarshal(data, &res) + if err != nil { + return nil, err + } + + return &res.Data.Runtime, nil +} + +func (r *argoRuntime) List() ([]model.Runtime, error) { jsonData := map[string]interface{}{ "query": ` { - runtimes( + runtimes + ( pagination: {} - project: "") { + project: "" + ) { edges { node { metadata { @@ -55,11 +102,12 @@ func (r *argoRuntime) List() ([]model.Runtime, error) { path: "/argo/api/graphql", body: jsonData, }) - defer response.Body.Close() if err != nil { fmt.Printf("The HTTP request failed with error %s\n", err) return nil, err } + defer response.Body.Close() + data, err := ioutil.ReadAll(response.Body) if err != nil { fmt.Printf("failed to read from response body") diff --git a/pkg/codefresh/codefresh.go b/pkg/codefresh/codefresh.go index 808666f..4d432c3 100644 --- a/pkg/codefresh/codefresh.go +++ b/pkg/codefresh/codefresh.go @@ -108,6 +108,7 @@ func (c *codefresh) requestAPIWithContext(ctx context.Context, opt *requestOptio } request.Header.Set("Authorization", c.token) request.Header.Set("Content-Type", "application/json") + request.Header.Set("origin", c.host) response, err := c.client.Do(request) if err != nil { From 11b0ba6dda9e911494abafe44f54fb4a0d217295 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 15:45:25 +0300 Subject: [PATCH 2/9] bump --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 83b4730..697f087 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.27.1 +0.28.0 From 29152eba505e63d09289b53f8dd09f5b4da7e19f Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 16:14:03 +0300 Subject: [PATCH 3/9] removed comment --- pkg/codefresh/argo_runtime.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index d649539..55a98ef 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -27,7 +27,6 @@ type ( Data struct { Runtime model.RuntimeCreationResponse } - // TODO: Errors } ) @@ -37,7 +36,7 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { // TODO: should also return error type forJsonData interface{} - + // the newlines are necessary var interpolatedMutation forJsonData = fmt.Sprintf("mutation {\n runtime(name: \"%s\") {\n id\n newAccessToken\n }\n}\n", runtimeName) From d3b564cfcacff0a1a7040206cbfd5c20ba1ca384 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 16:39:14 +0300 Subject: [PATCH 4/9] with []graphqlError --- pkg/codefresh/argo_runtime.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index 55a98ef..0913ead 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -27,6 +27,7 @@ type ( Data struct { Runtime model.RuntimeCreationResponse } + Errors []graphqlError } ) @@ -34,7 +35,7 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { return &argoRuntime{codefresh: codefresh} } -func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { // TODO: should also return error +func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { type forJsonData interface{} // the newlines are necessary @@ -61,13 +62,17 @@ func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse fmt.Printf("failed to read from response body") return nil, err } - res := graphQlRuntimeCreationResponse{} + res := graphQlRuntimeCreationResponse{} err = json.Unmarshal(data, &res) if err != nil { return nil, err } + if len(res.Errors) > 0 { + return nil, graphqlErrorResponse{errors: res.Errors} + } + return &res.Data.Runtime, nil } @@ -112,11 +117,14 @@ func (r *argoRuntime) List() ([]model.Runtime, error) { fmt.Printf("failed to read from response body") return nil, err } + res := graphqlRuntimesResponse{} err = json.Unmarshal(data, &res) + if err != nil { return nil, err } + runtimes := make([]model.Runtime, len(res.Data.Runtimes.Edges)) for i := range res.Data.Runtimes.Edges { runtimes[i] = *res.Data.Runtimes.Edges[i].Node @@ -127,5 +135,4 @@ func (r *argoRuntime) List() ([]model.Runtime, error) { } return runtimes, nil - } From 267f0690c3f6bc79353126d0f7aca1accbb82dcf Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 16:43:12 +0300 Subject: [PATCH 5/9] without separate interface{} --- pkg/codefresh/argo_runtime.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index 0913ead..14a63c0 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -36,10 +36,8 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { } func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { - type forJsonData interface{} - // the newlines are necessary - var interpolatedMutation forJsonData = fmt.Sprintf("mutation {\n runtime(name: \"%s\") {\n id\n newAccessToken\n }\n}\n", runtimeName) + var interpolatedMutation interface{} = fmt.Sprintf("mutation {\n runtime(name: \"%s\") {\n id\n newAccessToken\n }\n}\n", runtimeName) jsonData := map[string]interface{}{ "query": interpolatedMutation, From 8a2d883cdc03159f5124ddc86195ff30267695c7 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 17:19:15 +0300 Subject: [PATCH 6/9] better --- pkg/codefresh/argo_runtime.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index 14a63c0..a154c1a 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -36,8 +36,13 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { } func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { - // the newlines are necessary - var interpolatedMutation interface{} = fmt.Sprintf("mutation {\n runtime(name: \"%s\") {\n id\n newAccessToken\n }\n}\n", runtimeName) + interpolatedMutation := fmt.Sprintf(`mutation { + runtime(name: "%s") { + id + newAccessToken + } + } + `, runtimeName) jsonData := map[string]interface{}{ "query": interpolatedMutation, From 347d7fc0f8c37e86904b6191bee4172d6a61ccd6 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 17:22:21 +0300 Subject: [PATCH 7/9] indentation --- pkg/codefresh/argo_runtime.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index a154c1a..9b3f4e3 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -58,6 +58,7 @@ func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse fmt.Printf("The HTTP request failed with error %s\n", err) return nil, err } + defer response.Body.Close() data, err := ioutil.ReadAll(response.Body) From a6f33cffc73bc915ec30e58cc40a0e7562564168 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 17:22:54 +0300 Subject: [PATCH 8/9] n --- pkg/codefresh/argo_runtime.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index 9b3f4e3..1a1e27f 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -36,6 +36,7 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { } func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { + interpolatedMutation := fmt.Sprintf(`mutation { runtime(name: "%s") { id From 3fc03d090a98ce5c8fe2a581c824d978d990b610 Mon Sep 17 00:00:00 2001 From: elad-codefresh Date: Wed, 28 Jul 2021 17:23:15 +0300 Subject: [PATCH 9/9] n --- pkg/codefresh/argo_runtime.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/codefresh/argo_runtime.go b/pkg/codefresh/argo_runtime.go index 1a1e27f..9b3f4e3 100644 --- a/pkg/codefresh/argo_runtime.go +++ b/pkg/codefresh/argo_runtime.go @@ -36,7 +36,6 @@ func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI { } func (r *argoRuntime) Create(runtimeName string) (*model.RuntimeCreationResponse, error) { - interpolatedMutation := fmt.Sprintf(`mutation { runtime(name: "%s") { id