| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 9 | ) |
| 10 | |
| 11 | // SetDraft zet of haalt het conceptvinkje weg. |
| 12 | func (s *Store) SetDraft(ctx context.Context, id int64, draft bool) error { |
| 13 | bit := 0 |
| 14 | if draft { |
| 15 | bit = 1 |
| 16 | } |
| 17 | _, err := s.db.ExecContext(ctx, `UPDATE merge_requests SET draft = ?, updated_at = ? WHERE id = ?`, bit, now(), id) |
| 18 | return err |
| 19 | } |
| 20 | |
| 21 | // UpdateMergeRequest bewaart titel, beschrijving en labels. |
| 22 | func (s *Store) UpdateMergeRequest(ctx context.Context, id int64, title, description, labels string) error { |
| 23 | title = trim(title, 200) |
| 24 | if title == "" { |
| 25 | return i18n.E("err.mrTitle") |
| 26 | } |
| 27 | description = trim(description, 20000) |
| 28 | labels = trim(labels, 200) |
| 29 | _, err := s.db.ExecContext(ctx, `UPDATE merge_requests SET title = ?, description = ?, labels = ?, updated_at = ? WHERE id = ?`, |
| 30 | title, description, labels, now(), id) |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | // RenameOpenMRBranches werkt open verzoeken bij als een branch een nieuwe naam krijgt. |
| 35 | func (s *Store) RenameOpenMRBranches(ctx context.Context, repoID int64, oldName, newName string) error { |
| 36 | ts := now() |
| 37 | if _, err := s.db.ExecContext(ctx, `UPDATE merge_requests SET source_branch = ?, updated_at = ? WHERE repo_id = ? AND source_branch = ? AND status = 'open'`, |
| 38 | newName, ts, repoID, oldName); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | _, err := s.db.ExecContext(ctx, `UPDATE merge_requests SET target_branch = ?, updated_at = ? WHERE repo_id = ? AND target_branch = ? AND status = 'open'`, |
| 42 | newName, ts, repoID, oldName) |
| 43 | return err |
| 44 | } |
| 45 | |
| 46 | // MREvent is één gebeurtenis op de tijdlijn van een merge request. |
| 47 | type MREvent struct { |
| 48 | ID int64 |
| 49 | ActorID int64 |
| 50 | Actor string |
| 51 | Kind string |
| 52 | Body string |
| 53 | CreatedAt time.Time |
| 54 | } |
| 55 | |
| 56 | // AddMREvent bewaart een gebeurtenis. actorID 0 is toegestaan. |
| 57 | func (s *Store) AddMREvent(ctx context.Context, repoID, mrID, actorID int64, kind, body string) error { |
| 58 | kind = strings.TrimSpace(kind) |
| 59 | if kind == "" { |
| 60 | return i18n.E("err.badForm") |
| 61 | } |
| 62 | _, err := s.db.ExecContext(ctx, `INSERT INTO mr_events (repo_id, mr_id, actor_id, kind, body, created_at) VALUES (?, ?, ?, ?, ?, ?)`, |
| 63 | repoID, mrID, actorID, trim(kind, 40), trim(body, 4000), now()) |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | // ListMREvents geeft de gebeurtenissen, oudste eerst. |
| 68 | func (s *Store) ListMREvents(ctx context.Context, mrID int64) ([]MREvent, error) { |
| 69 | rows, err := s.db.QueryContext(ctx, `SELECT e.id, e.actor_id, COALESCE(u.username, ''), e.kind, e.body, e.created_at |
| 70 | FROM mr_events e LEFT JOIN users u ON u.id = e.actor_id |
| 71 | WHERE e.mr_id = ? ORDER BY e.id`, mrID) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | defer rows.Close() |
| 76 | var out []MREvent |
| 77 | for rows.Next() { |
| 78 | var e MREvent |
| 79 | var created string |
| 80 | if err := rows.Scan(&e.ID, &e.ActorID, &e.Actor, &e.Kind, &e.Body, &created); err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | e.CreatedAt = parseTime(created) |
| 84 | out = append(out, e) |
| 85 | } |
| 86 | return out, rows.Err() |
| 87 | } |
| 88 | |
| 89 | // PipelinesForMR geeft de pipelines van deze merge request, oudste eerst. |
| 90 | func (s *Store) PipelinesForMR(ctx context.Context, repoID, mrID int64) ([]Pipeline, error) { |
| 91 | rows, err := s.db.QueryContext(ctx, pipelineCols+` WHERE repo_id = ? AND mr_id = ? ORDER BY id`, repoID, mrID) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | defer rows.Close() |
| 96 | var out []Pipeline |
| 97 | for rows.Next() { |
| 98 | p, err := scanPipeline(rows) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | out = append(out, p) |
| 103 | } |
| 104 | return out, rows.Err() |
| 105 | } |