BlueGit

timfa bluegit

Files

bluegit / internal / store / activity.go

activity.go

feat: koppel commits aan wie ze pushte
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 6c3f521b History
activity.go 3.9 KiB Download Raw
1package store
2
3import (
4 "context"
5 "strings"
6 "time"
7)
8
9// Contribution is één getelde bijdrage.
10type Contribution struct {
11 UserID int64
12 RepoID int64
13 Kind string
14 Ref string
15 At time.Time
16}
17
18// AddContributions bewaart bijdragen. Een herhaling van dezelfde ref telt niet opnieuw.
19func (s *Store) AddContributions(ctx context.Context, rows []Contribution) error {
20 if len(rows) == 0 {
21 return nil
22 }
23 tx, err := s.db.BeginTx(ctx, nil)
24 if err != nil {
25 return err
26 }
27 defer func() { _ = tx.Rollback() }()
28 for _, row := range rows {
29 if row.UserID == 0 || row.RepoID == 0 || row.Kind == "" || row.Ref == "" {
30 continue
31 }
32 at := row.At.UTC()
33 if at.IsZero() {
34 at = time.Now().UTC()
35 }
36 if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO contributions (user_id, repo_id, kind, ref, occurred_at) VALUES (?, ?, ?, ?, ?)`,
37 row.UserID, row.RepoID, row.Kind, row.Ref, at.Format(time.RFC3339)); err != nil {
38 return err
39 }
40 }
41 return tx.Commit()
42}
43
44// ContributionTimes geeft tijdstippen binnen de filter.
45// userID 0 is iedereen. Een lege repolijst geeft niets terug.
46func (s *Store) ContributionTimes(ctx context.Context, userID int64, repoIDs []int64, since time.Time) ([]time.Time, error) {
47 if len(repoIDs) == 0 {
48 return nil, nil
49 }
50 holders := make([]string, len(repoIDs))
51 args := make([]any, 0, len(repoIDs)+2)
52 args = append(args, since.UTC().Format(time.RFC3339))
53 for i, id := range repoIDs {
54 holders[i] = "?"
55 args = append(args, id)
56 }
57 q := `SELECT occurred_at FROM contributions WHERE occurred_at >= ? AND repo_id IN (` + strings.Join(holders, ",") + `)`
58 if userID != 0 {
59 q += ` AND user_id = ?`
60 args = append(args, userID)
61 }
62 rows, err := s.db.QueryContext(ctx, q, args...)
63 if err != nil {
64 return nil, err
65 }
66 defer rows.Close()
67 var out []time.Time
68 for rows.Next() {
69 var raw string
70 if err := rows.Scan(&raw); err != nil {
71 return nil, err
72 }
73 out = append(out, parseTime(raw))
74 }
75 return out, rows.Err()
76}
77
78// ContributionSynced meldt of de bestaande commits van een repository al zijn overgenomen.
79func (s *Store) ContributionSynced(ctx context.Context, repoID int64) (bool, error) {
80 var n int
81 err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM contribution_sync WHERE repo_id = ?`, repoID).Scan(&n)
82 return n > 0, err
83}
84
85// MarkContributionSynced onthoudt dat de bestaande commits zijn overgenomen.
86func (s *Store) MarkContributionSynced(ctx context.Context, repoID int64) error {
87 _, err := s.db.ExecContext(ctx, `INSERT OR REPLACE INTO contribution_sync (repo_id, synced_at) VALUES (?, ?)`, repoID, now())
88 return err
89}
90
91// DropContributions wist de telling van één repository, zodat die opnieuw uit git komt.
92func (s *Store) DropContributions(ctx context.Context, repoID int64) error {
93 if _, err := s.db.ExecContext(ctx, `DELETE FROM contributions WHERE repo_id = ?`, repoID); err != nil {
94 return err
95 }
96 _, err := s.db.ExecContext(ctx, `DELETE FROM contribution_sync WHERE repo_id = ?`, repoID)
97 return err
98}
99
100// UserByGitEmail koppelt het lokale deel van een auteur-e-mail aan een gebruikersnaam.
101// Er is geen e-mailkolom: ada@example.com hoort bij de gebruiker ada.
102// Een waarde zonder @ telt niet, en de git-auteurnaam telt niet.
103func (s *Store) UserByGitEmail(ctx context.Context, email string) (User, bool) {
104 local, ok := emailLocal(email)
105 if !ok {
106 return User{}, false
107 }
108 u, err := s.UserByUsername(ctx, local)
109 if err != nil {
110 return User{}, false
111 }
112 return u, true
113}
114
115// UserIDByGitAuthor is UserByGitEmail als alleen het id nodig is.
116func (s *Store) UserIDByGitAuthor(ctx context.Context, email string) (int64, bool) {
117 u, ok := s.UserByGitEmail(ctx, email)
118 if !ok {
119 return 0, false
120 }
121 return u.ID, true
122}
123
124func emailLocal(email string) (string, bool) {
125 email = strings.ToLower(strings.TrimSpace(email))
126 i := strings.LastIndex(email, "@")
127 if i <= 0 || i == len(email)-1 {
128 return "", false
129 }
130 local := email[:i]
131 if local == "" || strings.ContainsAny(local, " @") {
132 return "", false
133 }
134 return local, true
135}