Skip to content

Commit 39e472b

Browse files
Merge pull request #29 from codefresh-io/CR-4371
CR-4371
2 parents 786a5be + aae7b98 commit 39e472b

File tree

6 files changed

+417
-30
lines changed

6 files changed

+417
-30
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.31.1
1+
0.31.2

pkg/codefresh/codefresh.go

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type (
2929
Argo() ArgoAPI
3030
Gitops() GitopsAPI
3131
ArgoRuntime() IArgoRuntimeAPI
32+
GitSource() IGitSourceAPI
3233
}
3334
)
3435

@@ -89,6 +90,10 @@ func (c *codefresh) ArgoRuntime() IArgoRuntimeAPI {
8990
return newArgoRuntimeAPI(c)
9091
}
9192

93+
func (c *codefresh) GitSource() IGitSourceAPI {
94+
return newGitSourceAPI(c)
95+
}
96+
9297
func (c *codefresh) requestAPI(opt *requestOptions) (*http.Response, error) {
9398
return c.requestAPIWithContext(context.Background(), opt)
9499
}

pkg/codefresh/git-source.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package codefresh
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
8+
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
9+
)
10+
11+
type (
12+
IGitSourceAPI interface {
13+
List(runtimeName string) ([]model.GitSource, error)
14+
}
15+
16+
gitSource struct {
17+
codefresh *codefresh
18+
}
19+
20+
graphQlGitSourcesListResponse struct {
21+
Data struct {
22+
GitSources model.GitSourcePage
23+
}
24+
Errors []graphqlError
25+
}
26+
)
27+
28+
func newGitSourceAPI(codefresh *codefresh) IGitSourceAPI {
29+
return &gitSource{codefresh: codefresh}
30+
}
31+
32+
func (g *gitSource) List(runtimeName string) ([]model.GitSource, error) {
33+
jsonData := map[string]interface{}{
34+
"query": `query GitSources($runtime: String) {
35+
gitSources(runtime: $runtime) {
36+
edges {
37+
node {
38+
metadata {
39+
name
40+
}
41+
self {
42+
path
43+
repoURL
44+
status {
45+
syncStatus
46+
healthStatus
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}`,
53+
"variables": map[string]interface{}{
54+
"runtime": runtimeName,
55+
},
56+
}
57+
58+
response, err := g.codefresh.requestAPI(&requestOptions{
59+
method: "POST",
60+
path: "/2.0/api/graphql",
61+
body: jsonData,
62+
})
63+
if err != nil {
64+
fmt.Printf("The HTTP request failed with error %s\n", err)
65+
return nil, err
66+
}
67+
defer response.Body.Close()
68+
69+
data, err := ioutil.ReadAll(response.Body)
70+
if err != nil {
71+
fmt.Printf("failed to read from response body")
72+
return nil, err
73+
}
74+
75+
res := graphQlGitSourcesListResponse{}
76+
err = json.Unmarshal(data, &res)
77+
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
gitSources := make([]model.GitSource, len(res.Data.GitSources.Edges))
83+
for i := range res.Data.GitSources.Edges {
84+
gitSources[i] = *res.Data.GitSources.Edges[i].Node
85+
}
86+
87+
if len(res.Errors) > 0 {
88+
return nil, graphqlErrorResponse{errors: res.Errors}
89+
}
90+
91+
return gitSources, nil
92+
}

pkg/codefresh/gitops.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package codefresh
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
type (
68
GitopsAPI interface {
@@ -103,7 +105,7 @@ type (
103105
ParentApp string `json:"parentApp"`
104106
Namespace string `json:"namespace"`
105107
Server string `json:"server"`
106-
Context *string `json:"context"`
108+
Context *string `json:"context"`
107109
}
108110

109111
EnvironmentActivity struct {
@@ -119,7 +121,7 @@ type (
119121
HistoryId int64 `json:"historyId"`
120122
Revision string `json:"revision, omitempty"`
121123
Resources interface{} `json:"resources"`
122-
Context *string `json:"context"`
124+
Context *string `json:"context"`
123125
}
124126
)
125127

pkg/codefresh/mocks/codefresh.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)