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