forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_monitor_webhook.go
More file actions
212 lines (186 loc) · 4.57 KB
/
code_monitor_webhook.go
File metadata and controls
212 lines (186 loc) · 4.57 KB
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
package database
import (
"context"
"database/sql"
"time"
"github.com/keegancsmith/sqlf"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/database/dbutil"
)
type WebhookAction struct {
ID int64
Monitor int64
Enabled bool
URL string
IncludeResults bool
CreatedBy int32
CreatedAt time.Time
ChangedBy int32
ChangedAt time.Time
}
const updateWebhookActionQuery = `
UPDATE cm_webhooks
SET enabled = %s,
include_results = %s,
url = %s,
changed_by = %s,
changed_at = %s
WHERE
id = %s
AND EXISTS (
SELECT 1 FROM cm_monitors
WHERE cm_monitors.id = cm_webhooks.monitor
AND %s
)
RETURNING %s;
`
func (s *codeMonitorStore) UpdateWebhookAction(ctx context.Context, id int64, enabled, includeResults bool, url string) (*WebhookAction, error) {
a := actor.FromContext(ctx)
user, err := a.User(ctx, s.userStore)
if err != nil {
return nil, err
}
q := sqlf.Sprintf(
updateWebhookActionQuery,
enabled,
includeResults,
url,
a.UID,
s.Now(),
id,
namespaceScopeQuery(user),
sqlf.Join(webhookActionColumns, ","),
)
row := s.QueryRow(ctx, q)
return scanWebhookAction(row)
}
const createWebhookActionQuery = `
INSERT INTO cm_webhooks
(monitor, enabled, include_results, url, created_by, created_at, changed_by, changed_at)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s)
RETURNING %s;
`
func (s *codeMonitorStore) CreateWebhookAction(ctx context.Context, monitorID int64, enabled, includeResults bool, url string) (*WebhookAction, error) {
now := s.Now()
a := actor.FromContext(ctx)
q := sqlf.Sprintf(
createWebhookActionQuery,
monitorID,
enabled,
includeResults,
url,
a.UID,
now,
a.UID,
now,
sqlf.Join(webhookActionColumns, ","),
)
row := s.QueryRow(ctx, q)
return scanWebhookAction(row)
}
const deleteWebhookActionQuery = `
DELETE FROM cm_webhooks
WHERE id in (%s)
AND MONITOR = %s
`
func (s *codeMonitorStore) DeleteWebhookActions(ctx context.Context, monitorID int64, webhookIDs ...int64) error {
if len(webhookIDs) == 0 {
return nil
}
deleteIDs := make([]*sqlf.Query, 0, len(webhookIDs))
for _, ids := range webhookIDs {
deleteIDs = append(deleteIDs, sqlf.Sprintf("%d", ids))
}
q := sqlf.Sprintf(
deleteWebhookActionQuery,
sqlf.Join(deleteIDs, ","),
monitorID,
)
return s.Exec(ctx, q)
}
const countWebhookActionsQuery = `
SELECT COUNT(*)
FROM cm_webhooks
WHERE monitor = %s;
`
func (s *codeMonitorStore) CountWebhookActions(ctx context.Context, monitorID int64) (int, error) {
var count int
err := s.QueryRow(ctx, sqlf.Sprintf(countWebhookActionsQuery, monitorID)).Scan(&count)
return count, err
}
const getWebhookActionQuery = `
SELECT %s -- WebhookActionColumns
FROM cm_webhooks
WHERE id = %s
`
func (s *codeMonitorStore) GetWebhookAction(ctx context.Context, webhookID int64) (*WebhookAction, error) {
q := sqlf.Sprintf(
getWebhookActionQuery,
sqlf.Join(webhookActionColumns, ","),
webhookID,
)
row := s.QueryRow(ctx, q)
return scanWebhookAction(row)
}
const listWebhookActionsQuery = `
SELECT %s -- WebhookActionColumns
FROM cm_webhooks
WHERE %s
ORDER BY id ASC
LIMIT %s;
`
func (s *codeMonitorStore) ListWebhookActions(ctx context.Context, opts ListActionsOpts) ([]*WebhookAction, error) {
q := sqlf.Sprintf(
listWebhookActionsQuery,
sqlf.Join(webhookActionColumns, ","),
opts.Conds(),
opts.Limit(),
)
rows, err := s.Query(ctx, q)
if err != nil {
return nil, err
}
defer rows.Close()
return scanWebhookActions(rows)
}
// webhookActionColumns is the set of columns in the cm_webhooks table
// This must be kept in sync with scanWebhook
var webhookActionColumns = []*sqlf.Query{
sqlf.Sprintf("cm_webhooks.id"),
sqlf.Sprintf("cm_webhooks.monitor"),
sqlf.Sprintf("cm_webhooks.enabled"),
sqlf.Sprintf("cm_webhooks.url"),
sqlf.Sprintf("cm_webhooks.include_results"),
sqlf.Sprintf("cm_webhooks.created_by"),
sqlf.Sprintf("cm_webhooks.created_at"),
sqlf.Sprintf("cm_webhooks.changed_by"),
sqlf.Sprintf("cm_webhooks.changed_at"),
}
func scanWebhookActions(rows *sql.Rows) ([]*WebhookAction, error) {
var ws []*WebhookAction
for rows.Next() {
w, err := scanWebhookAction(rows)
if err != nil {
return nil, err
}
ws = append(ws, w)
}
return ws, rows.Err()
}
// scanWebhookAction scans a WebhookAction from a *sql.Row or *sql.Rows.
// It must be kept in sync with webhookActionColumns.
func scanWebhookAction(scanner dbutil.Scanner) (*WebhookAction, error) {
var w WebhookAction
err := scanner.Scan(
&w.ID,
&w.Monitor,
&w.Enabled,
&w.URL,
&w.IncludeResults,
&w.CreatedBy,
&w.CreatedAt,
&w.ChangedBy,
&w.ChangedAt,
)
return &w, err
}