-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
353 lines (292 loc) · 8.91 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"context"
"database/sql"
_ "embed"
"flag"
"fmt"
"io"
"log/slog"
"net/http"
u "net/url"
"regexp"
"strings"
"time"
"github.com/antchfx/htmlquery"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/net/html"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
var matrixHomeserver = flag.String("matrix.homeserver", "matrix.org", "Matrix homeserver for the bot account")
var matrixUsername = flag.String("matrix.username", "", "Matrix bot username")
var mainURL = flag.String("url", "https://nixpkgs-update-logs.nix-community.org", "Webpage with logs")
var dbPath = flag.String("db", "data.db", "Path to the DB file")
var tickerOpt = flag.Duration("ticker", 24*time.Hour, "How often to check url")
var debug = flag.Bool("debug", false, "Enable debug logging")
var client *mautrix.Client
var db *sql.DB
// TODO: revisit this, in light of default constraint in schema (which must be applied to existing DB somehow)
// Ensure invariant: a subscription can't be added before the corresponding package has had its last_visited column set.
// Otherwise we have cases where Scan fails (because it can't cast NULL to a string), and also we can end up sending notifications for old errors.
//
//go:embed db/schema.sql
var ddl string
// These two regexps are for parsing logs.
// - "error: " is a nix build error
// - "ExitFailure" is a nixpkgs-update error
// - "failed with" is a nixpkgs/maintainers/scripts/update.py error
var erroRE = regexp.MustCompile(`^error:|ExitFailure|failed with`)
var ignoRE = regexp.MustCompile(`^~.*|^\.\.`)
// These two regexps are for matching against user input.
// We want to avoid stuff like the following, because it leads us to spam the nix-community.org server.
// - sub *
// - sub pythonPackages.*
//
// Unsubbing with the same queries is OK, because it it has different semantics and doesn't spam upstream.
var dangerousRE = regexp.MustCompile(`^sub (?:[*?]+|\w+\.\*)$`)
var subUnsubRE = regexp.MustCompile(`^(un)?sub ([\w_?*.-]+)$`)
var hc = &http.Client{}
// These are abstracted so that we can pass a different function in tests.
type handlers struct {
logFetcher func(string) (string, bool)
sender func(string, id.RoomID) (*mautrix.RespSendEvent, error)
}
var h handlers
var helpText = `Welcome to the nixpkgs-update-notifier bot!
These are the available commands:
- **sub foo**: subscribe to package <code>foo</code>
- **unsub foo**: unsubscribe from package <code>foo</code>
- **subs**: list subscriptions
- **help**: show this help message
You can use the <code>*</code> and <code>?</code> globs in queries. Things you can do:
- <code>sub python31?Packages.acme</code>
- <code>sub *.acme</code>
Things you cannot do:
- <code>sub *</code>
- <code>sub ?</code>
- <code>sub foo.*</code>
The code for the bot is [here](https://github.com/asymmetric/nixpkgs-update-notifier).
`
func main() {
ctx := context.Background()
flag.Parse()
setupLogger()
h = handlers{
logFetcher: fetchLastLog,
sender: sendMarkdown,
}
var err error
if err = setupDB(ctx, fmt.Sprintf("file:%s", *dbPath)); err != nil {
panic(err)
}
defer db.Close()
client = setupMatrix()
go func() {
if err := client.Sync(); err != nil {
panic(err)
}
}()
ticker := time.NewTicker(*tickerOpt)
optimizeTicker := time.NewTicker(24 * time.Hour)
slog.Debug("delay set", "value", *tickerOpt)
// - fetch main page, add list of packages to mem
// - fetch last log of subscribed packages
// - enter infinite loop, block on queue
// wake on new item in queue
// item can be:
// - new url to parse
// - if it's a non-log link, visit it and then add all log-links to the queue
// - if it's a log-link, download log, check for errors, insert into db accordingly
// - fetch main page
// - new sub
// - new broken package, send to subbers
slog.Info("initialized", "delay", tickerOpt)
storeAttrPaths(*mainURL)
updateSubs()
for {
select {
case <-ticker.C:
slog.Info("new ticker run")
storeAttrPaths(*mainURL)
updateSubs()
case <-optimizeTicker.C:
slog.Info("optimizing DB")
if _, err := db.Exec("PRAGMA optimize;"); err != nil {
panic(err)
}
}
}
}
// scrapes the main page, saving package names to db
func storeAttrPaths(url string) {
req, err := newReqWithUA(url)
if err != nil {
panic(err)
}
resp, err := hc.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
hrefs := htmlquery.Find(doc, "//a/@href")
slog.Info("storing attr paths", "count", len(hrefs))
for _, href := range hrefs {
attr_path := strings.TrimSuffix(htmlquery.InnerText(href), "/")
if ignoRE.MatchString(attr_path) {
continue
}
if _, err := db.Exec("INSERT OR IGNORE INTO packages(attr_path) VALUES (?)", attr_path); err != nil {
fatal(err)
}
}
}
// Iterates over subscribed-to packages, and fetches their latest log, printing out whether it contained an error.
// It also updates the packages.last_visited column.
func updateSubs() {
rows, err := db.Query("SELECT attr_path FROM subscriptions")
if err != nil {
fatal(err)
}
defer rows.Close()
aps := make([]string, 0)
for rows.Next() {
var ap string
if err := rows.Scan(&ap); err != nil {
fatal(err)
}
aps = append(aps, ap)
}
if err := rows.Err(); err != nil {
fatal(err)
}
for _, ap := range aps {
url := packageURL(ap)
// TODO: make async
// TODO: we could sometimes avoid fetching altogether if we passed last_visited
date, hasError := h.logFetcher(url)
// avoid duplicate notifications by ensuring we haven't already notified for this log
var last string
if err := db.QueryRow("SELECT last_visited FROM packages WHERE attr_path = ?", ap).Scan(&last); err != nil {
fatal(err)
}
if date > last {
if hasError {
slog.Info("new log", "err", true, "url", logURL(ap, date))
notifySubscribers(ap, date)
} else {
slog.Info("new log", "err", false, "url", logURL(ap, date))
}
if _, err := db.Exec("UPDATE packages SET last_visited = ?, error = ? WHERE attr_path = ?", date, hasError, ap); err != nil {
fatal(err)
}
} else {
slog.Info("no new log", "url", url, "date", date)
}
}
}
// fetch package page
// find last log
// fetch last log
func fetchLastLog(url string) (date string, hasError bool) {
req, err := newReqWithUA(url)
if err != nil {
panic(err)
}
resp, err := hc.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
panic(err)
}
n := htmlquery.FindOne(doc, "//a[contains(@href, '.log')][last()]/@href")
href := htmlquery.InnerText(n)
parsedURL, err := u.Parse(url)
if err != nil {
panic(err)
}
fullURL := parsedURL.JoinPath(href)
_, date = getComponents(fullURL.String())
slog.Debug("fetching log", "url", fullURL)
req, err = newReqWithUA(fullURL.String())
if err != nil {
panic(err)
}
resp, err = hc.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
hasError = erroRE.Find(body) != nil
return date, hasError
}
func notifySubscribers(attr_path, date string) {
// - find all subscribers for package
// - send message in respective room
// - if we're not in that room, drop from db of subs?
logPath := fmt.Sprintf("%s/%s/%s.log", *mainURL, attr_path, date)
slog.Debug("lp", "lp", logPath)
rows, err := db.Query("SELECT roomid FROM subscriptions WHERE attr_path = ?", attr_path)
if err != nil {
panic(err)
}
defer rows.Close()
roomIDs := make([]string, 0)
for rows.Next() {
var roomID string
if err := rows.Scan(&roomID); err != nil {
panic(err)
}
roomIDs = append(roomIDs, roomID)
}
if err := rows.Err(); err != nil {
panic(err)
}
for _, roomID := range roomIDs {
slog.Info("notifying subscriber", "roomid", roomID)
s := fmt.Sprintf("New build error for package `%s`: %s", attr_path, logPath)
if _, err := h.sender(s, id.RoomID(roomID)); err != nil {
// TODO check if we're not in room, in that case remove sub
slog.Error(err.Error())
}
}
}
func handleMessage(ctx context.Context, evt *event.Event) {
msg := evt.Content.AsMessage().Body
sender := evt.Sender.String()
slog.Debug("received msg", "msg", msg, "sender", sender)
if sender == fmt.Sprintf("@%s:%s", *matrixUsername, *matrixHomeserver) {
slog.Debug("ignoring our own message", "msg", msg)
return
}
if dangerousRE.MatchString(msg) {
slog.Info("received spammy query", "msg", msg, "sender", sender)
s := `Pattern returns too many results, please use a more specific selector.
Type **help** for a list of allowed/forbidden patterns.`
if _, err := h.sender(s, id.RoomID(evt.RoomID)); err != nil {
slog.Error(err.Error())
}
} else if subUnsubRE.MatchString(msg) {
handleSubUnsub(msg, evt)
} else if msg == "subs" {
handleSubs(evt)
} else {
// anything else, so print help
if _, err := h.sender(helpText, evt.RoomID); err != nil {
slog.Error(err.Error())
}
slog.Debug("received help", "sender", sender)
}
}