forked from buildkite/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpipelines.go
91 lines (83 loc) · 1.81 KB
/
pipelines.go
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
package cli
import (
"fmt"
"github.com/buildkite/cli/v2/graphql"
)
type pipeline struct {
ID string
Org string
Slug string
URL string
RepositoryURL string
}
func listPipelines(client *graphql.Client) ([]pipeline, error) {
resp, err := client.Do(`
query {
viewer {
organizations {
edges {
node {
slug
pipelines(first:500) {
edges {
node {
id
slug
url
repository {
url
}
}
}
}
}
}
}
}
}
`, nil)
if err != nil {
return nil, err
}
var parsedResp struct {
Data struct {
Viewer struct {
Organizations struct {
Edges []struct {
Node struct {
Slug string `json:"slug"`
Pipelines struct {
Edges []struct {
Node struct {
ID string `json:"id"`
Slug string `json:"slug"`
URL string `json:"url"`
Repository struct {
URL string `json:"url"`
} `json:"repository"`
} `json:"node"`
} `json:"edges"`
} `json:"pipelines"`
} `json:"node"`
} `json:"edges"`
} `json:"organizations"`
} `json:"viewer"`
} `json:"data"`
}
if err = resp.DecodeInto(&parsedResp); err != nil {
return nil, fmt.Errorf("Failed to parse GraphQL response: %v", err)
}
var pipelines []pipeline
for _, orgEdge := range parsedResp.Data.Viewer.Organizations.Edges {
for _, pipelineEdge := range orgEdge.Node.Pipelines.Edges {
pipelines = append(pipelines, pipeline{
ID: pipelineEdge.Node.ID,
URL: pipelineEdge.Node.URL,
Org: orgEdge.Node.Slug,
Slug: pipelineEdge.Node.Slug,
RepositoryURL: pipelineEdge.Node.Repository.URL,
})
}
}
return pipelines, nil
}