| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "time" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/access" |
| 10 | ) |
| 11 | |
| 12 | // ApprovalPolicy is de instelling voor verplichte goedkeuringen. |
| 13 | // Configured onderscheidt "nooit bewaard" van een bewuste keuze, |
| 14 | // ook als die keuze de eis uitzet. Een bewaarde groepsregel wint |
| 15 | // van de projectregel. |
| 16 | type ApprovalPolicy struct { |
| 17 | Configured bool |
| 18 | MaintainerRequired bool |
| 19 | RequiredCount int |
| 20 | DevelopersApprove bool |
| 21 | } |
| 22 | |
| 23 | // GetApproval leest de regel. Zonder rij is hij niet ingesteld en is het aantal 1. |
| 24 | func (s *Store) GetApproval(ctx context.Context, scopeKind string, scopeID int64) (ApprovalPolicy, error) { |
| 25 | var configured, required, developers int |
| 26 | var count int |
| 27 | err := s.db.QueryRowContext(ctx, `SELECT configured, maintainer_required, required_count, developers_approve FROM approval_rules WHERE scope_kind = ? AND scope_id = ?`, scopeKind, scopeID).Scan(&configured, &required, &count, &developers) |
| 28 | if errors.Is(err, sql.ErrNoRows) { |
| 29 | return ApprovalPolicy{RequiredCount: 1}, nil |
| 30 | } |
| 31 | if err != nil { |
| 32 | return ApprovalPolicy{}, err |
| 33 | } |
| 34 | if count < 1 { |
| 35 | count = 1 |
| 36 | } |
| 37 | return ApprovalPolicy{ |
| 38 | Configured: configured == 1, MaintainerRequired: required == 1, |
| 39 | RequiredCount: count, DevelopersApprove: developers == 1, |
| 40 | }, nil |
| 41 | } |
| 42 | |
| 43 | // SaveApproval bewaart de regel en markeert hem als ingesteld. |
| 44 | func (s *Store) SaveApproval(ctx context.Context, scopeKind string, scopeID int64, p ApprovalPolicy) error { |
| 45 | if p.RequiredCount < 1 { |
| 46 | p.RequiredCount = 1 |
| 47 | } |
| 48 | if p.RequiredCount > 100 { |
| 49 | p.RequiredCount = 100 |
| 50 | } |
| 51 | _, err := s.db.ExecContext(ctx, `INSERT INTO approval_rules (scope_kind, scope_id, configured, maintainer_required, required_count, developers_approve) |
| 52 | VALUES (?, ?, 1, ?, ?, ?) |
| 53 | ON CONFLICT(scope_kind, scope_id) DO UPDATE SET configured = 1, maintainer_required = excluded.maintainer_required, required_count = excluded.required_count, developers_approve = excluded.developers_approve`, |
| 54 | scopeKind, scopeID, boolInt(p.MaintainerRequired), p.RequiredCount, boolInt(p.DevelopersApprove)) |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | // EffectiveApproval is de regel die voor een repository geldt. |
| 59 | // Een ingestelde groepsregel vervangt de projectregel. |
| 60 | func (s *Store) EffectiveApproval(ctx context.Context, repo Repo) (ApprovalPolicy, string, error) { |
| 61 | if repo.OwnerKind == "group" { |
| 62 | p, err := s.GetApproval(ctx, "group", repo.OwnerID) |
| 63 | if err != nil { |
| 64 | return ApprovalPolicy{}, "", err |
| 65 | } |
| 66 | if p.Configured { |
| 67 | g, err := s.GroupByID(ctx, repo.OwnerID) |
| 68 | if err != nil { |
| 69 | return ApprovalPolicy{}, "", err |
| 70 | } |
| 71 | return p, g.Path, nil |
| 72 | } |
| 73 | } |
| 74 | p, err := s.GetApproval(ctx, "repo", repo.ID) |
| 75 | return p, "", err |
| 76 | } |
| 77 | |
| 78 | // GroupMaintainerCount telt beheerders, inclusief de maker van de groep. |
| 79 | func (s *Store) GroupMaintainerCount(ctx context.Context, groupID int64) (int, error) { |
| 80 | g, err := s.GroupByID(ctx, groupID) |
| 81 | if err != nil { |
| 82 | return 0, err |
| 83 | } |
| 84 | ids := map[int64]bool{g.OwnerID: true} |
| 85 | members, err := s.ListMembers(ctx, "group", groupID) |
| 86 | if err != nil { |
| 87 | return 0, err |
| 88 | } |
| 89 | for _, m := range members { |
| 90 | if !m.Pending && m.Role == access.RoleMaintainer && m.UserID != 0 { |
| 91 | ids[m.UserID] = true |
| 92 | } |
| 93 | } |
| 94 | return len(ids), nil |
| 95 | } |
| 96 | |
| 97 | // ProjectMaintainerCount telt wie op dit project beheerder is. |
| 98 | // Bij een groep horen de groepsbeheerders en de maker daar ook bij. |
| 99 | func (s *Store) ProjectMaintainerCount(ctx context.Context, repo Repo) (int, error) { |
| 100 | ids := map[int64]bool{} |
| 101 | if repo.OwnerKind == "user" { |
| 102 | ids[repo.OwnerID] = true |
| 103 | } |
| 104 | if repo.OwnerKind == "group" { |
| 105 | g, err := s.GroupByID(ctx, repo.OwnerID) |
| 106 | if err != nil { |
| 107 | return 0, err |
| 108 | } |
| 109 | ids[g.OwnerID] = true |
| 110 | members, err := s.ListMembers(ctx, "group", g.ID) |
| 111 | if err != nil { |
| 112 | return 0, err |
| 113 | } |
| 114 | for _, m := range members { |
| 115 | if !m.Pending && m.Role == access.RoleMaintainer && m.UserID != 0 { |
| 116 | ids[m.UserID] = true |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | members, err := s.ListMembers(ctx, "repo", repo.ID) |
| 121 | if err != nil { |
| 122 | return 0, err |
| 123 | } |
| 124 | for _, m := range members { |
| 125 | if !m.Pending && m.Role == access.RoleMaintainer && m.UserID != 0 { |
| 126 | ids[m.UserID] = true |
| 127 | } |
| 128 | } |
| 129 | return len(ids), nil |
| 130 | } |
| 131 | |
| 132 | // Review is de huidige review van één persoon op een merge request. |
| 133 | type Review struct { |
| 134 | UserID int64 |
| 135 | Username string |
| 136 | Status string |
| 137 | UpdatedAt time.Time |
| 138 | } |
| 139 | |
| 140 | // UpsertReview zet de review van deze gebruiker. |
| 141 | // status is approved, changes, rejected of dismissed. |
| 142 | func (s *Store) UpsertReview(ctx context.Context, repoID, mrID, userID int64, status string) error { |
| 143 | _, err := s.db.ExecContext(ctx, `INSERT INTO reviews (repo_id, mr_id, user_id, status, updated_at) VALUES (?, ?, ?, ?, ?) |
| 144 | ON CONFLICT(mr_id, user_id) DO UPDATE SET status = excluded.status, updated_at = excluded.updated_at`, |
| 145 | repoID, mrID, userID, status, now()) |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | // ListReviews geeft elke reviewer, oudste bijwerking eerst. |
| 150 | func (s *Store) ListReviews(ctx context.Context, mrID int64) ([]Review, error) { |
| 151 | rows, err := s.db.QueryContext(ctx, `SELECT r.user_id, u.username, r.status, r.updated_at FROM reviews r JOIN users u ON u.id = r.user_id WHERE r.mr_id = ? ORDER BY r.updated_at, r.user_id`, mrID) |
| 152 | if err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 | defer rows.Close() |
| 156 | var out []Review |
| 157 | for rows.Next() { |
| 158 | var rev Review |
| 159 | var updated string |
| 160 | if err := rows.Scan(&rev.UserID, &rev.Username, &rev.Status, &updated); err != nil { |
| 161 | return nil, err |
| 162 | } |
| 163 | rev.UpdatedAt = parseTime(updated) |
| 164 | out = append(out, rev) |
| 165 | } |
| 166 | return out, rows.Err() |
| 167 | } |