-
Notifications
You must be signed in to change notification settings - Fork 22
/
views.go
154 lines (124 loc) · 3.52 KB
/
views.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package api
import (
"sort"
"strings"
graphql "github.com/cli/shurcooL-graphql"
)
type Views struct {
client *Client
}
type ViewConnection struct {
RepoName string
Filter string
}
type ViewQueryData struct {
Name string
Description string
ViewInfo struct {
Connections []struct {
Repository struct{ Name string }
Filter string
}
} `graphql:"... on View"`
}
type View struct {
Name string
Description string
Connections []ViewConnection
}
func (c *Client) Views() *Views { return &Views{client: c} }
func (c *Views) Get(name string) (*View, error) {
var query struct {
Result ViewQueryData `graphql:"searchDomain(name: $name)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
}
err := c.client.Query(&query, variables)
if err != nil {
return nil, err
}
connections := make([]ViewConnection, len(query.Result.ViewInfo.Connections))
for i, data := range query.Result.ViewInfo.Connections {
connections[i] = ViewConnection{
RepoName: data.Repository.Name,
Filter: data.Filter,
}
}
view := View{
Name: query.Result.Name,
Description: query.Result.Description,
Connections: connections,
}
return &view, nil
}
type ViewListItem struct {
Name string
Typename string `graphql:"__typename"`
}
func (c *Views) List() ([]ViewListItem, error) {
var query struct {
View []ViewListItem `graphql:"searchDomains"`
}
err := c.client.Query(&query, nil)
sort.Slice(query.View, func(i, j int) bool {
return strings.ToLower(query.View[i].Name) < strings.ToLower(query.View[j].Name)
})
return query.View, err
}
type ViewConnectionInput struct {
RepositoryName graphql.String `json:"repositoryName"`
Filter graphql.String `json:"filter"`
}
func (c *Views) Create(name, description string, connections []ViewConnectionInput) error {
var mutation struct {
CreateView struct {
Name string
Description string
} `graphql:"createView(name: $name, description: $description, connections: $connections)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"description": graphql.String(description),
"connections": connections,
}
return c.client.Mutate(&mutation, variables)
}
func (c *Views) Delete(name, reason string) error {
var mutation struct {
DeleteSearchDomain struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"deleteSearchDomain(name: $name, deleteMessage: $reason)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"reason": graphql.String(reason),
}
return c.client.Mutate(&mutation, variables)
}
func (c *Views) UpdateConnections(name string, connections []ViewConnectionInput) error {
var mutation struct {
View struct {
Name string
} `graphql:"updateView(viewName: $viewName, connections: $connections)"`
}
variables := map[string]interface{}{
"viewName": graphql.String(name),
"connections": connections,
}
return c.client.Mutate(&mutation, variables)
}
func (c *Views) UpdateDescription(name string, description string) error {
var mutation struct {
UpdateDescriptionMutation struct {
// We have to make a selection, so just take __typename
Typename graphql.String `graphql:"__typename"`
} `graphql:"updateDescriptionForSearchDomain(name: $name, newDescription: $description)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"description": graphql.String(description),
}
return c.client.Mutate(&mutation, variables)
}