Skip to content

Commit bf4bfc6

Browse files
wip
1 parent 62235d2 commit bf4bfc6

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

pkg/codefresh/argo_runtime.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/codefresh-io/argo-platform/libs/ql/graph/model"
99
)
1010

11-
1211
type (
1312
IArgoRuntimeAPI interface {
1413
List() ([]model.Runtime, error)
@@ -17,20 +16,20 @@ type (
1716
codefresh *codefresh
1817
}
1918
graphqlRuntimesResponse struct {
20-
Data struct {
21-
Runtimes model.RuntimePage `json:"runtimes"`
22-
} `json:"data"`
23-
Errors []byte
19+
Data struct {
20+
Runtimes model.RuntimePage
21+
}
22+
Errors []graphqlError
2423
}
2524
)
2625

2726
func newArgoRuntimeAPI(codefresh *codefresh) IArgoRuntimeAPI {
2827
return &argoRuntime{codefresh: codefresh}
2928
}
30-
func (r *argoRuntime) List() ([]model.Runtime, error) {
29+
func (r *argoRuntime) List() ([]model.Runtime, error) {
3130

3231
jsonData := map[string]interface{}{
33-
"query":`
32+
"query": `
3433
{
3534
runtimes{
3635
edges{
@@ -46,24 +45,23 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
4645
}
4746
}
4847
`,
49-
}
50-
48+
}
5149

5250
response, err := r.codefresh.requestAPI(&requestOptions{
5351
method: "POST",
54-
path: "/argo/api/graphql",
55-
body: jsonData,
52+
path: "/argo/api/graphql",
53+
body: jsonData,
5654
})
57-
defer response.Body.Close()
58-
if err != nil {
59-
fmt.Printf("The HTTP request failed with error %s\n", err)
55+
defer response.Body.Close()
56+
if err != nil {
57+
fmt.Printf("The HTTP request failed with error %s\n", err)
6058
return nil, err
61-
}
62-
data, err := ioutil.ReadAll(response.Body)
59+
}
60+
data, err := ioutil.ReadAll(response.Body)
6361
if err != nil {
64-
fmt.Printf("failed to read from response body")
62+
fmt.Printf("failed to read from response body")
6563
return nil, err
66-
}
64+
}
6765
res := graphqlRuntimesResponse{}
6866
err = json.Unmarshal(data, &res)
6967
if err != nil {
@@ -73,8 +71,11 @@ func (r *argoRuntime) List() ([]model.Runtime, error) {
7371
for _, v := range res.Data.Runtimes.Edges {
7472
runtimes = append(runtimes, *v.Node)
7573
}
76-
74+
75+
if len(res.Errors) > 0 {
76+
return nil, graphqlErrorResponse{errors: res.Errors}
77+
}
78+
7779
return runtimes, nil
78-
79-
80-
}
80+
81+
}

pkg/codefresh/common.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
)
8+
9+
type graphqlError struct {
10+
Message string
11+
Locations [] struct {
12+
Line int
13+
Column int
14+
}
15+
Extensions struct {
16+
Code string
17+
Exception struct {
18+
Stacktrace []string
19+
}
20+
}
21+
}
22+
23+
type graphqlErrorResponse struct {
24+
errors []graphqlError
25+
concatenatedErrors string
26+
}
27+
28+
29+
func (e graphqlErrorResponse) Error() string {
30+
31+
if e.concatenatedErrors != "" {
32+
return e.concatenatedErrors
33+
}
34+
var sb strings.Builder
35+
for _, err := range e.errors {
36+
sb.WriteString(fmt.Sprintln(err.Message))
37+
}
38+
e.concatenatedErrors = sb.String()
39+
return e.concatenatedErrors
40+
}

0 commit comments

Comments
 (0)