BlueGit

timfa bluegit

Files

bluegit / internal / server / activity.go

activity.go

feat: nieuw-formulier, groepsoverzicht, logo's en talen
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed d1203d3b History
activity.go 4.0 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 var rows []store.Contribution
77 seen := map[string]bool{}
78 for ref, sha := range refs {
79 if _, ok := gitrepo.BranchOfRef(ref); !ok {
80 continue
81 }
82 list, err := gitrepo.CommitsSince(ctx, s.Git, dir, "", sha)
83 if err != nil {
84 return err
85 }
86 for _, c := range list {
87 if seen[c.SHA] {
88 continue
89 }
90 seen[c.SHA] = true
91 uid, ok := s.Store.UserIDByGitAuthor(ctx, c.Email, c.Name)
92 if !ok {
93 continue
94 }
95 when := time.Unix(c.Unix, 0).UTC()
96 if c.Unix <= 0 {
97 when = time.Now().UTC()
98 }
99 rows = append(rows, store.Contribution{
100 UserID: uid, RepoID: repo.ID, Kind: "commit", Ref: c.SHA, At: when,
101 })
102 }
103 }
104 return s.Store.AddContributions(ctx, rows)
105}
106
107func (s *Server) recordPushContributions(repo store.Repo, user store.User, before, after map[string]string) {
108 if s.Store == nil || user.ID == 0 || s.StoragePath() == "" || after == nil {
109 return
110 }
111 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
112 ctx := context.Background()
113 var rows []store.Contribution
114 for ref, sha := range after {
115 if before[ref] == sha {
116 continue
117 }
118 if _, ok := gitrepo.BranchOfRef(ref); !ok {
119 continue
120 }
121 list, err := gitrepo.CommitsSince(ctx, s.Git, dir, before[ref], sha)
122 if err != nil {
123 if s.Log != nil {
124 s.Log.Error("bijdragen", "err", err)
125 }
126 continue
127 }
128 for _, c := range list {
129 uid := user.ID
130 if id, ok := s.Store.UserIDByGitAuthor(ctx, c.Email, c.Name); ok {
131 uid = id
132 }
133 when := time.Unix(c.Unix, 0).UTC()
134 if c.Unix <= 0 {
135 when = time.Now().UTC()
136 }
137 rows = append(rows, store.Contribution{
138 UserID: uid, RepoID: repo.ID, Kind: "commit", Ref: c.SHA, At: when,
139 })
140 }
141 }
142 if err := s.Store.AddContributions(ctx, rows); err != nil && s.Log != nil {
143 s.Log.Error("bijdragen", "err", err)
144 }
145}
146
147func (s *Server) recordContribution(userID, repoID int64, kind, ref string, at time.Time) {
148 if s.Store == nil || userID == 0 || repoID == 0 || ref == "" {
149 return
150 }
151 if at.IsZero() {
152 at = time.Now().UTC()
153 }
154 if err := s.Store.AddContributions(context.Background(), []store.Contribution{{
155 UserID: userID, RepoID: repoID, Kind: kind, Ref: ref, At: at,
156 }}); err != nil && s.Log != nil {
157 s.Log.Error("bijdragen", "err", err)
158 }
159}
160
161func mrRef(id int64) string { return "mr:" + strconv.FormatInt(id, 10) }