|
| 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 | + name string |
| 23 | + repoUrl string |
| 24 | + path string |
| 25 | + } |
| 26 | + Errors []graphqlError |
| 27 | + } |
| 28 | + |
| 29 | + // graphQlRuntimeCreationResponse struct { |
| 30 | + // Data struct { |
| 31 | + // Runtime model.RuntimeCreationResponse |
| 32 | + // } |
| 33 | + // Errors []graphqlError |
| 34 | + // } |
| 35 | +) |
| 36 | + |
| 37 | +func newGitSourceAPI(codefresh *codefresh) IGitSourceAPI { |
| 38 | + return &gitSource{codefresh: codefresh} |
| 39 | +} |
| 40 | + |
| 41 | +func (g *gitSource) List(runtimeName string) ([]model.GitSource, error) { |
| 42 | + jsonData := map[string]interface{}{ |
| 43 | + "query": `GetGitSourcesList(pagination: SlicePaginationArgs!, project: String!, $runtime: String!) { |
| 44 | + gitSources(pagination: {}, project: "", runtime: $runtime) { |
| 45 | + edges { |
| 46 | + node { |
| 47 | + metadata { |
| 48 | + name |
| 49 | + } |
| 50 | + path |
| 51 | + repoURL |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }`, |
| 56 | + "variables": map[string]interface{}{ |
| 57 | + "runtime": runtimeName, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + response, err := g.codefresh.requestAPI(&requestOptions{ |
| 62 | + method: "POST", |
| 63 | + path: "/argo/api/graphql", |
| 64 | + body: jsonData, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + fmt.Printf("The HTTP request failed with error %s\n", err) |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + defer response.Body.Close() |
| 71 | + |
| 72 | + data, err := ioutil.ReadAll(response.Body) |
| 73 | + if err != nil { |
| 74 | + fmt.Printf("failed to read from response body") |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + res := graphQlGitSourcesListResponse{} |
| 79 | + err = json.Unmarshal(data, &res) |
| 80 | + |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + gitSources := make([]model.GitSource, 3) |
| 86 | + // for i := range res.Data.GitSourceEdge { |
| 87 | + // gitSources[i] = *res.Data.GitSourceEdges. |
| 88 | + // } |
| 89 | + |
| 90 | + // if len(res.Errors) > 0 { |
| 91 | + // return nil, graphqlErrorResponse{errors: res.Errors} |
| 92 | + // } |
| 93 | + |
| 94 | + return gitSources, nil |
| 95 | +} |
0 commit comments