BlueGit

timfa bluegit

Files

bluegit / internal / server / activity.go

activity.go

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