forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscorecards.go
124 lines (113 loc) · 4.14 KB
/
scorecards.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
package opslevel
import (
"fmt"
)
type ScorecardId struct {
Aliases []string `graphql:"aliases"`
Id ID `graphql:"id"`
}
type Scorecard struct {
ScorecardId
AffectsOverallServiceLevels bool `graphql:"affectsOverallServiceLevels"`
Description string `graphql:"description"` // optional
Filter Filter `graphql:"filter"` // optional
Name string `graphql:"name"`
Owner EntityOwner `graphql:"owner"`
PassingChecks int `graphql:"passingChecks"`
ServiceCount int `graphql:"serviceCount"`
ChecksCount int `graphql:"totalChecks"`
}
type ScorecardConnection struct {
Nodes []Scorecard `graphql:"nodes"`
PageInfo PageInfo `graphql:"pageInfo"`
TotalCount int `graphql:"totalCount"`
}
type ScorecardInput struct {
AffectsOverallServiceLevels *bool `graphql:"affectsOverallServiceLevels" json:"affectsOverallServiceLevels,omitempty"`
Name string `graphql:"name" json:"name"`
Description *string `graphql:"description" json:"description,omitempty"`
OwnerId ID `graphql:"ownerId" json:"ownerId"`
FilterId *ID `graphql:"filterId" json:"filterId,omitempty"`
}
func (client *Client) CreateScorecard(input ScorecardInput) (*Scorecard, error) {
var m struct {
Payload struct {
Scorecard Scorecard `graphql:"scorecard"`
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"scorecardCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("ScorecardCreate"))
return &m.Payload.Scorecard, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetScorecard(identifier string) (*Scorecard, error) {
var q struct {
Account struct {
Scorecard Scorecard `graphql:"scorecard(input: $input)"`
}
}
input := *NewIdentifier(identifier)
v := PayloadVariables{
"input": input,
}
err := client.Query(&q, v, WithName("ScorecardGet"))
if q.Account.Scorecard.Id == "" {
err = fmt.Errorf("Scorecard with ID '%s' or Alias '%s' not found", input.Id, input.Alias)
}
return &q.Account.Scorecard, HandleErrors(err, nil)
}
func (client *Client) ListScorecards(variables *PayloadVariables) (ScorecardConnection, error) {
var q struct {
Account struct {
Scorecards ScorecardConnection `graphql:"scorecards(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("ScorecardsList")); err != nil {
return ScorecardConnection{}, err
}
for q.Account.Scorecards.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Scorecards.PageInfo.End
resp, err := client.ListScorecards(variables)
if err != nil {
return ScorecardConnection{}, err
}
q.Account.Scorecards.Nodes = append(q.Account.Scorecards.Nodes, resp.Nodes...)
q.Account.Scorecards.PageInfo = resp.PageInfo
q.Account.Scorecards.TotalCount = len(q.Account.Scorecards.Nodes)
}
return q.Account.Scorecards, nil
}
func (client *Client) UpdateScorecard(identifier string, input ScorecardInput) (*Scorecard, error) {
var m struct {
Payload struct {
Scorecard Scorecard `graphql:"scorecard"`
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"scorecardUpdate(scorecard: $scorecard, input: $input)"`
}
scorecard := *NewIdentifier(identifier)
v := PayloadVariables{
"scorecard": scorecard,
"input": input,
}
err := client.Mutate(&m, v, WithName("ScorecardUpdate"))
return &m.Payload.Scorecard, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) DeleteScorecard(identifier string) (ID, error) {
var m struct {
Payload struct {
DeletedScorecardId ID `graphql:"deletedScorecardId"`
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"scorecardDelete(input: $input)"`
}
input := *NewIdentifier(identifier)
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("ScorecardDelete"))
return m.Payload.DeletedScorecardId, HandleErrors(err, m.Payload.Errors)
}