BlueGit

timfa bluegit

Files

bluegit / internal / store / notes.go

notes.go

feat: breid branches en merge requests uit
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 7cb4d08b History
notes.go 3.9 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 ParentID int64
28 CreatedAt time.Time
29}
30
31// AddNote bewaart een algemene reactie en telt hem als bijdrage.
32func (s *Store) AddNote(ctx context.Context, repoID, mrID, authorID int64, body string) (Note, error) {
33 return s.insertNote(ctx, repoID, mrID, authorID, body, "", 0, 0, "", "", 0)
34}
35
36// AddReply bewaart een reactie op een bestaande reactie, één niveau diep.
37func (s *Store) AddReply(ctx context.Context, repoID, mrID, authorID, parentID int64, body string) (Note, error) {
38 var parentMR, parentParent int64
39 err := s.db.QueryRowContext(ctx, `SELECT mr_id, parent_id FROM notes WHERE id = ? AND repo_id = ?`, parentID, repoID).Scan(&parentMR, &parentParent)
40 if err != nil || parentMR != mrID || parentParent != 0 {
41 return Note{}, i18n.E("err.noteParent")
42 }
43 return s.insertNote(ctx, repoID, mrID, authorID, body, "", 0, 0, "", "", parentID)
44}
45
46// AddLineNote bewaart een reactie op regels, optioneel met een suggestie.
47func (s *Store) AddLineNote(ctx context.Context, repoID, mrID, authorID int64, body, path string, start, end int, suggestion, base string) (Note, error) {
48 if path == "" || start < 1 || end < start {
49 return Note{}, i18n.E("err.sugLines")
50 }
51 return s.insertNote(ctx, repoID, mrID, authorID, body, path, start, end, suggestion, base, 0)
52}
53
54func (s *Store) insertNote(ctx context.Context, repoID, mrID, authorID int64, body, path string, start, end int, suggestion, base string, parentID int64) (Note, error) {
55 body = strings.TrimSpace(body)
56 if body == "" {
57 return Note{}, i18n.E("err.noteEmpty")
58 }
59 limit := 4000
60 if path != "" {
61 limit = 20000
62 }
63 body = trim(body, limit)
64 tx, err := s.db.BeginTx(ctx, nil)
65 if err != nil {
66 return Note{}, err
67 }
68 defer func() { _ = tx.Rollback() }()
69 ts := now()
70 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, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
71 repoID, mrID, authorID, body, ts, path, start, end, suggestion, base, parentID)
72 if err != nil {
73 return Note{}, err
74 }
75 id, _ := res.LastInsertId()
76 if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO contributions (user_id, repo_id, kind, ref, occurred_at) VALUES (?, ?, 'comment', ?, ?)`,
77 authorID, repoID, "note:"+strconv.FormatInt(id, 10), ts); err != nil {
78 return Note{}, err
79 }
80 if err := tx.Commit(); err != nil {
81 return Note{}, err
82 }
83 return Note{
84 ID: id, RepoID: repoID, MRID: mrID, AuthorID: authorID, Body: body,
85 FilePath: path, LineStart: start, LineEnd: end, Suggestion: suggestion, BaseText: base,
86 ParentID: parentID, CreatedAt: parseTime(ts),
87 }, nil
88}
89
90// ListNotes geeft de reacties van één merge request, oudste eerst.
91func (s *Store) ListNotes(ctx context.Context, mrID int64) ([]Note, error) {
92 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, n.parent_id
93 FROM notes n JOIN users u ON u.id = n.author_id WHERE n.mr_id = ? ORDER BY n.id`, mrID)
94 if err != nil {
95 return nil, err
96 }
97 defer rows.Close()
98 var out []Note
99 for rows.Next() {
100 var n Note
101 var created string
102 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, &n.ParentID); err != nil {
103 return nil, err
104 }
105 n.CreatedAt = parseTime(created)
106 out = append(out, n)
107 }
108 return out, rows.Err()
109}