forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathteam.go
215 lines (189 loc) · 4.62 KB
/
team.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package opslevel
import (
"github.com/shurcooL/graphql"
)
type User struct {
Name graphql.String
Email graphql.String
}
type Contact struct {
DisplayName graphql.String
Address graphql.String
}
type ContactInput struct {
Type string `json:"type,omitEmpty"`
DisplayName string `json:"displayName,omitEmpty"`
Address string `json:"address,omitEmpty"`
}
type Team struct {
Alias graphql.String
Contacts []Contact
Id graphql.ID
Manager User
Name graphql.String
Responsibilities graphql.String
}
type TeamCreateInput struct {
Name string `json:"name"`
ManagerEmail string `json:"managerEmail,omitempty"`
Responsibilities string `json:"responsibilities,omitempty"`
Contacts []ContactInput `json:"contacts,omitempty"`
}
type TeamUpdateInput struct {
Id graphql.ID `json:"id,omitempty"`
Alias string `json:"alias,omitempty"`
Name string `json:"name,omitempty"`
ManagerEmail string `json:"managerEmail,omitempty"`
Responsibilities string `json:"responsibilities,omitempty"`
}
type TeamDeleteInput struct {
Id graphql.ID `json:"id,omitempty"`
Alias string `json:"alias,omitempty"`
}
//#region Create
func (client *Client) CreateTeam(input TeamCreateInput) (*Team, error) {
var m struct {
Payload struct {
Team Team
Errors []OpsLevelErrors
} `graphql:"teamCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
if err := client.Mutate(&m, v); err != nil {
return nil, err
}
return &m.Payload.Team, FormatErrors(m.Payload.Errors)
}
//#endregion
//#region Retrieve
func (client *Client) GetTeamWithAlias(alias string) (*Team, error) {
var q struct {
Account struct {
Team Team `graphql:"team(alias: $team)"`
}
}
v := PayloadVariables{
"team": alias,
}
if err := client.Query(&q, v); err != nil {
return nil, err
}
return &q.Account.Team, nil
}
func (client *Client) GetTeamWithId(id graphql.ID) (*Team, error) {
var q struct {
Account struct {
Team Team `graphql:"team(id: $team)"`
}
}
v := PayloadVariables{
"team": id,
}
if err := client.Query(&q, v); err != nil {
return nil, err
}
return &q.Account.Team, nil
}
func (client *Client) GetTeamCount() (int, error) {
var q struct {
Account struct {
Teams struct {
TotalCount graphql.Int
}
}
}
if err := client.Query(&q, nil); err != nil {
return 0, err
}
return int(q.Account.Teams.TotalCount), nil
}
type ListTeamsQuery struct {
Account struct {
Teams struct {
Nodes []Team
PageInfo PageInfo
} `graphql:"teams(after: $after, first: $first)"`
}
}
func (q *ListTeamsQuery) Query(client *Client) error {
var subQ ListTeamsQuery
v := PayloadVariables{
"after": q.Account.Teams.PageInfo.End,
"first": graphql.Int(100),
}
if err := client.Query(&subQ, v); err != nil {
return err
}
if subQ.Account.Teams.PageInfo.HasNextPage {
subQ.Query(client)
}
for _, team := range subQ.Account.Teams.Nodes {
q.Account.Teams.Nodes = append(q.Account.Teams.Nodes, team)
}
return nil
}
func (client *Client) ListTeams() ([]Team, error) {
q := ListTeamsQuery{}
if err := q.Query(client); err != nil {
return []Team{}, err
}
return q.Account.Teams.Nodes, nil
}
//#endregion
//#region Update
func (client *Client) UpdateTeam(input TeamUpdateInput) (*Team, error) {
var m struct {
Payload struct {
Team Team
Errors []OpsLevelErrors
} `graphql:"teamUpdate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
if err := client.Mutate(&m, v); err != nil {
return nil, err
}
return &m.Payload.Team, FormatErrors(m.Payload.Errors)
}
//#endregion
//#region Delete
func (client *Client) DeleteTeamWithAlias(alias string) error {
var m struct {
Payload struct {
Id graphql.ID `graphql:"deletedTeamId"`
Alias graphql.String `graphql:"deletedTeamAlias"`
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"teamDelete(input: $input)"`
}
v := PayloadVariables{
"input": TeamDeleteInput{
Alias: alias,
},
}
if err := client.Mutate(&m, v); err != nil {
return err
}
return FormatErrors(m.Payload.Errors)
}
func (client *Client) DeleteTeamWithId(id graphql.ID) error {
var m struct {
Payload struct {
Id graphql.ID `graphql:"deletedTeamId"`
Alias graphql.String `graphql:"deletedTeamAlias"`
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"teamDelete(input: $input)"`
}
v := PayloadVariables{
"input": TeamDeleteInput{
Id: id,
},
}
if err := client.Mutate(&m, v); err != nil {
return err
}
return FormatErrors(m.Payload.Errors)
}
//#endregion