/
label.go
165 lines (150 loc) · 3.74 KB
/
label.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
package graphqlapp
import (
context "context"
"database/sql"
"github.com/target/goalert/config"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/label"
"github.com/target/goalert/search"
"github.com/target/goalert/validation"
)
func (q *Query) LabelKeys(ctx context.Context, input *graphql2.LabelKeySearchOptions) (conn *graphql2.StringConnection, err error) {
if input == nil {
input = &graphql2.LabelKeySearchOptions{}
}
var opts label.KeySearchOptions
if input.Search != nil {
opts.Search = *input.Search
}
opts.Omit = input.Omit
if input.After != nil && *input.After != "" {
err = search.ParseCursor(*input.After, &opts)
if err != nil {
return conn, err
}
}
if input.First != nil {
opts.Limit = *input.First
}
if opts.Limit == 0 {
opts.Limit = 15
}
opts.Limit++
labelKeys, err := q.LabelStore.SearchKeys(ctx, &opts)
if err != nil {
return nil, err
}
conn = new(graphql2.StringConnection)
conn.PageInfo = &graphql2.PageInfo{}
if len(labelKeys) == opts.Limit {
labelKeys = labelKeys[:len(labelKeys)-1]
conn.PageInfo.HasNextPage = true
}
if len(labelKeys) > 0 {
last := labelKeys[len(labelKeys)-1]
opts.After = last
cur, err := search.Cursor(opts)
if err != nil {
return nil, err
}
conn.PageInfo.EndCursor = &cur
}
conn.Nodes = labelKeys
return conn, err
}
func (q *Query) LabelValues(ctx context.Context, input *graphql2.LabelValueSearchOptions) (conn *graphql2.StringConnection, err error) {
if input == nil {
input = &graphql2.LabelValueSearchOptions{}
}
var opts label.ValueSearchOptions
if input.Search != nil {
opts.Search = *input.Search
}
opts.Omit = input.Omit
opts.Key = input.Key
if input.After != nil && *input.After != "" {
err = search.ParseCursor(*input.After, &opts)
if err != nil {
return conn, err
}
}
if input.First != nil {
opts.Limit = *input.First
}
if opts.Limit == 0 {
opts.Limit = 15
}
opts.Limit++
values, err := q.LabelStore.SearchValues(ctx, &opts)
if err != nil {
return nil, err
}
conn = new(graphql2.StringConnection)
conn.PageInfo = &graphql2.PageInfo{}
if len(values) == opts.Limit {
values = values[:len(values)-1]
conn.PageInfo.HasNextPage = true
}
if len(values) > 0 {
last := values[len(values)-1]
opts.After = last
cur, err := search.Cursor(opts)
if err != nil {
return nil, err
}
conn.PageInfo.EndCursor = &cur
}
conn.Nodes = values
return conn, err
}
func (q *Query) Labels(ctx context.Context, input *graphql2.LabelSearchOptions) (conn *graphql2.LabelConnection, err error) {
if input == nil {
input = &graphql2.LabelSearchOptions{}
}
keyConn, err := q.LabelKeys(ctx, &graphql2.LabelKeySearchOptions{
Search: input.Search,
After: input.After,
Omit: input.Omit,
First: input.First,
})
if err != nil {
return nil, err
}
conn = new(graphql2.LabelConnection)
conn.PageInfo = keyConn.PageInfo
conn.Nodes = make([]label.Label, len(keyConn.Nodes))
for i, k := range keyConn.Nodes {
conn.Nodes[i].Key = k
}
return conn, nil
}
func (m *Mutation) SetLabel(ctx context.Context, input graphql2.SetLabelInput) (bool, error) {
err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
cfg := config.FromContext(ctx)
if cfg.General.DisableLabelCreation {
allLabels, err := m.LabelStore.UniqueKeysTx(ctx, tx)
if err != nil {
return err
}
var keyExists bool
for _, l := range allLabels {
if input.Key == l {
keyExists = true
break
}
}
if !keyExists {
return validation.NewFieldError("Key", "Creating new labels is currently disabled.")
}
}
return m.LabelStore.SetTx(ctx, tx, &label.Label{
Key: input.Key,
Value: input.Value,
Target: input.Target,
})
})
if err != nil {
return false, err
}
return true, nil
}