-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscorecards.go
164 lines (145 loc) · 5.12 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
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
package opslevel
import (
"errors"
"fmt"
"github.com/hasura/go-graphql-client"
)
type ScorecardConnection struct {
Nodes []Scorecard `graphql:"nodes"`
PageInfo PageInfo `graphql:"pageInfo"`
TotalCount int `graphql:"totalCount"`
}
type ScorecardCategoryConnection struct {
Nodes []Category `graphql:"nodes"`
PageInfo PageInfo `graphql:"pageInfo"`
TotalCount int `graphql:"totalCount"`
}
func (scorecard *ScorecardId) ResourceId() ID {
return scorecard.Id
}
func (scorecard *ScorecardId) AliasableType() AliasOwnerTypeEnum {
return AliasOwnerTypeEnumScorecard
}
func (scorecard *ScorecardId) GetAliases() []string {
return scorecard.Aliases
}
func (scorecard *ScorecardId) ReconcileAliases(client *Client, aliasesWanted []string) error {
aliasesToCreate, aliasesToDelete := extractAliases(scorecard.Aliases, aliasesWanted)
// reconcile wanted aliases with actual aliases
deleteErr := client.DeleteAliases(AliasOwnerTypeEnumScorecard, aliasesToDelete)
_, createErr := client.CreateAliases(scorecard.Id, aliasesToCreate)
// update scorecard to reflect API updates
updatedScorecard, getErr := client.GetScorecard(string(scorecard.Id))
if updatedScorecard != nil {
scorecard.Aliases = updatedScorecard.Aliases
}
return errors.Join(deleteErr, createErr, getErr)
}
func (scorecard *Scorecard) ListCategories(client *Client, variables *PayloadVariables) (*ScorecardCategoryConnection, error) {
if scorecard.Id == "" {
return nil, fmt.Errorf("unable to get categories, invalid scorecard id: '%s'", scorecard.Id)
}
var q struct {
Account struct {
Scorecard struct {
Categories ScorecardCategoryConnection `graphql:"categories(after: $after, first: $first)"`
} `graphql:"scorecard(input: $scorecard)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
(*variables)["scorecard"] = *NewIdentifier(string(scorecard.Id))
if err := client.Query(&q, *variables, WithName("ScorecardCategoryList")); err != nil {
return nil, err
}
for q.Account.Scorecard.Categories.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Scorecard.Categories.PageInfo.End
resp, err := scorecard.ListCategories(client, variables)
if err != nil {
return nil, err
}
q.Account.Scorecard.Categories.Nodes = append(q.Account.Scorecard.Categories.Nodes, resp.Nodes...)
q.Account.Scorecard.Categories.PageInfo = resp.PageInfo
q.Account.Scorecard.Categories.TotalCount += resp.TotalCount
}
return &q.Account.Scorecard.Categories, nil
}
func (client *Client) CreateScorecard(input ScorecardInput) (*Scorecard, error) {
var m struct {
Payload ScorecardPayload `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(input string) (*Scorecard, error) {
var q struct {
Account struct {
Scorecard Scorecard `graphql:"scorecard(input: $input)"`
}
}
v := PayloadVariables{
"input": *NewIdentifier(input),
}
err := client.Query(&q, v, WithName("ScorecardGet"))
if q.Account.Scorecard.Id == "" {
err = graphql.Errors{graphql.Error{
Message: fmt.Sprintf("scorecard with ID or Alias matching '%s' not found", input),
Path: []any{"account", "scorecard"},
}}
}
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 nil, err
}
for q.Account.Scorecards.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Scorecards.PageInfo.End
resp, err := client.ListScorecards(variables)
if err != nil {
return nil, 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 ScorecardPayload `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 { // TODO: need to fix this
DeletedScorecardId ID `graphql:"deletedScorecardId"`
Errors []Error `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)
}