BlueGit

timfa bluegit

Files

bluegit / internal / store / notes.go

notes.go

feat: suggesties op merge requests
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 3902d232 History
notes.go 3.3 KiB Download Raw
1package store
2
3import (
4 "context"
5 "strconv"
6
7 "gitlab.com/Timfa/bluegit/internal/i18n"
8 "strings"
9 "time"
10)
11
12// Note is een reactie op een merge request.
13// FilePath leeg is een algemene reactie. Suggestion is de vervangende tekst,
14// BaseText de regels zoals ze bij het plaatsen in het bestand stonden.
15type Note struct {
16 ID int64
17 RepoID int64
18 MRID int64
19 AuthorID int64
20 Author string
21 Body string
22 FilePath string
23 LineStart int
24 LineEnd int
25 Suggestion string
26 BaseText string
27 CreatedAt time.Time
28}
29
30// AddNote bewaart een algemene reactie en telt hem als bijdrage.
31func (s *Store) AddNote(ctx context.Context, repoID, mrID, authorID int64, body string) (Note, error) {
32 return s.insertNote(ctx, repoID, mrID, authorID, body, "", 0, 0, "", "")
33}
34
35// AddLineNote bewaart een reactie op regels, optioneel met een suggestie.
36func (s *Store) AddLineNote(ctx context.Context, repoID, mrID, authorID int64, body, path string, start, end int, suggestion, base string) (Note, error) {
37 if path == "" || start < 1 || end < start {
38 return Note{}, i18n.E("err.sugLines")
39 }
40 return s.insertNote(ctx, repoID, mrID, authorID, body, path, start, end, suggestion, base)
41}
42
43func (s *Store) insertNote(ctx context.Context, repoID, mrID, authorID int64, body, path string, start, end int, suggestion, base string) (Note, error) {
44 body = strings.TrimSpace(body)
45 if body == "" {
46 return Note{}, i18n.E("err.noteEmpty")
47 }
48 limit := 4000
49 if path != "" {
50 limit = 20000
51 }
52 body = trim(body, limit)
53 tx, err := s.db.BeginTx(ctx, nil)
54 if err != nil {
55 return Note{}, err
56 }
57 defer func() { _ = tx.Rollback() }()
58 ts := now()
59 res, err := tx.ExecContext(ctx, `INSERT INTO notes (repo_id, mr_id, author_id, body, created_at, file_path, line_start, line_end, suggestion, base_text) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
60 repoID, mrID, authorID, body, ts, path, start, end, suggestion, base)
61 if err != nil {
62 return Note{}, err
63 }
64 id, _ := res.LastInsertId()
65 if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO contributions (user_id, repo_id, kind, ref, occurred_at) VALUES (?, ?, 'comment', ?, ?)`,
66 authorID, repoID, "note:"+strconv.FormatInt(id, 10), ts); err != nil {
67 return Note{}, err
68 }
69 if err := tx.Commit(); err != nil {
70 return Note{}, err
71 }
72 return Note{
73 ID: id, RepoID: repoID, MRID: mrID, AuthorID: authorID, Body: body,
74 FilePath: path, LineStart: start, LineEnd: end, Suggestion: suggestion, BaseText: base,
75 CreatedAt: parseTime(ts),
76 }, nil
77}
78
79// ListNotes geeft de reacties van één merge request, oudste eerst.
80func (s *Store) ListNotes(ctx context.Context, mrID int64) ([]Note, error) {
81 rows, err := s.db.QueryContext(ctx, `SELECT n.id, n.repo_id, n.mr_id, n.author_id, u.username, n.body, n.created_at, n.file_path, n.line_start, n.line_end, n.suggestion, n.base_text
82 FROM notes n JOIN users u ON u.id = n.author_id WHERE n.mr_id = ? ORDER BY n.id`, mrID)
83 if err != nil {
84 return nil, err
85 }
86 defer rows.Close()
87 var out []Note
88 for rows.Next() {
89 var n Note
90 var created string
91 if err := rows.Scan(&n.ID, &n.RepoID, &n.MRID, &n.AuthorID, &n.Author, &n.Body, &created, &n.FilePath, &n.LineStart, &n.LineEnd, &n.Suggestion, &n.BaseText); err != nil {
92 return nil, err
93 }
94 n.CreatedAt = parseTime(created)
95 out = append(out, n)
96 }
97 return out, rows.Err()
98}