-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-source.go
More file actions
105 lines (89 loc) · 2.2 KB
/
git-source.go
File metadata and controls
105 lines (89 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package codefresh
import (
"context"
"fmt"
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"
)
type (
IGitSourceAPI interface {
List(ctx context.Context, runtimeName string) ([]model.GitSource, error)
Delete(ctx context.Context, runtimeName string, name string) error
}
gitSource struct {
codefresh *codefresh
}
graphQlGitSourcesListResponse struct {
Data struct {
GitSources model.GitSourceSlice
}
Errors []graphqlError
}
graphQlDeleteGitSourceResponse struct {
Errors []graphqlError
}
)
func newGitSourceAPI(codefresh *codefresh) IGitSourceAPI {
return &gitSource{codefresh: codefresh}
}
func (g *gitSource) List(ctx context.Context, runtimeName string) ([]model.GitSource, error) {
jsonData := map[string]interface{}{
"query": `
query GitSources($runtime: String) {
gitSources(runtime: $runtime) {
edges {
node {
metadata {
name
}
self {
path
repoURL
status {
syncStatus
healthStatus
}
}
}
}
}
}`,
"variables": map[string]interface{}{
"runtime": runtimeName,
},
}
res := &graphQlGitSourcesListResponse{}
err := g.codefresh.graphqlAPI(ctx, jsonData, res)
if err != nil {
return nil, fmt.Errorf("failed getting git-source list: %w", err)
}
gitSources := make([]model.GitSource, len(res.Data.GitSources.Edges))
for i := range res.Data.GitSources.Edges {
gitSources[i] = *res.Data.GitSources.Edges[i].Node
}
if len(res.Errors) > 0 {
return nil, graphqlErrorResponse{errors: res.Errors}
}
return gitSources, nil
}
func (g *gitSource) Delete(ctx context.Context, runtimeName string, name string) error {
jsonData := map[string]interface{}{
"query": `
mutation RemoveGitSource($runtime: String, $name: String) {
removeGitSource(runtime: $runtime, name: $name)
}
`,
"variables": map[string]interface{}{
"runtime": runtimeName,
"name": name,
},
}
res := graphQlDeleteGitSourceResponse{}
err := g.codefresh.graphqlAPI(ctx, jsonData, res)
if err != nil {
return fmt.Errorf("failed making a graphql API call to removeGitSource: %w", err)
}
if len(res.Errors) > 0 {
return graphqlErrorResponse{errors: res.Errors}
}
return nil
}