BlueGit

timfa bluegit

Files

bluegit / internal / server / 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 4.4 KiB Download Raw
1package server
2
3import (
4 "context"
5 "strconv"
6 "time"
7
8 "gitlab.com/Timfa/bluegit/internal/activity"
9 "gitlab.com/Timfa/bluegit/internal/gitrepo"
10 "gitlab.com/Timfa/bluegit/internal/i18n"
11 "gitlab.com/Timfa/bluegit/internal/store"
12)
13
14func (s *Server) heatmap(ctx context.Context, userID int64, repoIDs []int64, lang i18n.Lang) *activity.Calendar {
15 s.syncRepos(ctx, repoIDs)
16 loc := activity.Location()
17 since := time.Now().In(loc).AddDate(-1, 0, -3)
18 times, err := s.Store.ContributionTimes(ctx, userID, repoIDs, since)
19 counts := map[string]int{}
20 if err == nil {
21 for _, ts := range times {
22 counts[ts.In(loc).Format("2006-01-02")]++
23 }
24 }
25 cal := activity.Build(time.Now(), counts, lang)
26 return &cal
27}
28
29func (s *Server) visibleIDs(ctx context.Context, userID int64, repos []store.Repo) []int64 {
30 allowed, err := s.Store.ListRepoIDs(ctx, userID)
31 if err != nil {
32 return nil
33 }
34 set := map[int64]bool{}
35 for _, id := range allowed {
36 set[id] = true
37 }
38 var ids []int64
39 for _, repo := range repos {
40 if set[repo.ID] {
41 ids = append(ids, repo.ID)
42 }
43 }
44 return ids
45}
46
47func (s *Server) syncRepos(ctx context.Context, repoIDs []int64) {
48 if s.StoragePath() == "" {
49 return
50 }
51 for _, id := range repoIDs {
52 done, err := s.Store.ContributionSynced(ctx, id)
53 if err != nil || done {
54 continue
55 }
56 repo, err := s.Store.RepoByID(ctx, id)
57 if err != nil {
58 continue
59 }
60 if err := s.backfillRepo(ctx, repo); err != nil {
61 if s.Log != nil {
62 s.Log.Error("bijdragen", "err", err)
63 }
64 continue
65 }
66 _ = s.Store.MarkContributionSynced(ctx, id)
67 }
68}
69
70func (s *Server) backfillRepo(ctx context.Context, repo store.Repo) error {
71 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
72 refs, err := gitrepo.Refs(ctx, s.Git, dir)
73 if err != nil {
74 return err
75 }
76 pushers, err := s.Store.PushersByRepo(ctx, repo.ID)
77 if err != nil {
78 return err
79 }
80 var rows []store.Contribution
81 seen := map[string]bool{}
82 for ref, sha := range refs {
83 if _, ok := gitrepo.BranchOfRef(ref); !ok {
84 continue
85 }
86 list, err := gitrepo.CommitsSince(ctx, s.Git, dir, "", sha)
87 if err != nil {
88 return err
89 }
90 for _, c := range list {
91 if seen[c.SHA] {
92 continue
93 }
94 seen[c.SHA] = true
95 uid, ok := s.Store.UserIDByGitAuthor(ctx, c.Email)
96 if !ok {
97 if p, found := pushers[c.SHA]; found && p.UserID != 0 {
98 uid = p.UserID
99 ok = true
100 }
101 }
102 if !ok {
103 continue
104 }
105 when := time.Unix(c.Unix, 0).UTC()
106 if c.Unix <= 0 {
107 when = time.Now().UTC()
108 }
109 rows = append(rows, store.Contribution{
110 UserID: uid, RepoID: repo.ID, Kind: "commit", Ref: c.SHA, At: when,
111 })
112 }
113 }
114 return s.Store.AddContributions(ctx, rows)
115}
116
117func (s *Server) recordPushContributions(repo store.Repo, user store.User, before, after map[string]string) {
118 if s.Store == nil || user.ID == 0 || s.StoragePath() == "" || after == nil {
119 return
120 }
121 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
122 ctx := context.Background()
123 var rows []store.Contribution
124 var authors []store.PushAuthor
125 for ref, sha := range after {
126 if before[ref] == sha {
127 continue
128 }
129 if _, ok := gitrepo.BranchOfRef(ref); !ok {
130 continue
131 }
132 list, err := gitrepo.CommitsSince(ctx, s.Git, dir, before[ref], sha)
133 if err != nil {
134 if s.Log != nil {
135 s.Log.Error("bijdragen", "err", err)
136 }
137 continue
138 }
139 for _, c := range list {
140 authors = append(authors, store.PushAuthor{SHA: c.SHA, Name: c.Name, Email: c.Email})
141 uid := user.ID
142 if id, ok := s.Store.UserIDByGitAuthor(ctx, c.Email); ok {
143 uid = id
144 }
145 when := time.Unix(c.Unix, 0).UTC()
146 if c.Unix <= 0 {
147 when = time.Now().UTC()
148 }
149 rows = append(rows, store.Contribution{
150 UserID: uid, RepoID: repo.ID, Kind: "commit", Ref: c.SHA, At: when,
151 })
152 }
153 }
154 if err := s.Store.RememberPushers(ctx, repo.ID, user.ID, authors); err != nil && s.Log != nil {
155 s.Log.Error("pusher", "err", err)
156 }
157 if err := s.Store.AddContributions(ctx, rows); err != nil && s.Log != nil {
158 s.Log.Error("bijdragen", "err", err)
159 }
160}
161
162func (s *Server) recordContribution(userID, repoID int64, kind, ref string, at time.Time) {
163 if s.Store == nil || userID == 0 || repoID == 0 || ref == "" {
164 return
165 }
166 if at.IsZero() {
167 at = time.Now().UTC()
168 }
169 if err := s.Store.AddContributions(context.Background(), []store.Contribution{{
170 UserID: userID, RepoID: repoID, Kind: kind, Ref: ref, At: at,
171 }}); err != nil && s.Log != nil {
172 s.Log.Error("bijdragen", "err", err)
173 }
174}
175
176func mrRef(id int64) string { return "mr:" + strconv.FormatInt(id, 10) }