this repo has no description
1package repo
2
3import (
4 "context"
5 "crypto/rand"
6 "fmt"
7 "math/big"
8
9 "tangled.sh/tangled.sh/core/appview/db"
10 "tangled.sh/tangled.sh/core/appview/pages/repoinfo"
11
12 "github.com/go-git/go-git/v5/plumbing"
13 "github.com/go-git/go-git/v5/plumbing/object"
14)
15
16func uniqueEmails(commits []*object.Commit) []string {
17 emails := make(map[string]struct{})
18 for _, commit := range commits {
19 if commit.Author.Email != "" {
20 emails[commit.Author.Email] = struct{}{}
21 }
22 if commit.Committer.Email != "" {
23 emails[commit.Committer.Email] = struct{}{}
24 }
25 }
26 var uniqueEmails []string
27 for email := range emails {
28 uniqueEmails = append(uniqueEmails, email)
29 }
30 return uniqueEmails
31}
32
33func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) {
34 if commitCount == 0 && tagCount == 0 && branchCount == 0 {
35 return
36 }
37
38 // typically 1 item on right side = 2 files in height
39 availableSpace := fileCount / 2
40
41 // clamp tagcount
42 if tagCount > 0 {
43 tagsTrunc = 1
44 availableSpace -= 1 // an extra subtracted for headers etc.
45 }
46
47 // clamp branchcount
48 if branchCount > 0 {
49 branchesTrunc = min(max(branchCount, 1), 3)
50 availableSpace -= branchesTrunc // an extra subtracted for headers etc.
51 }
52
53 // show
54 if commitCount > 0 {
55 commitsTrunc = max(availableSpace, 3)
56 }
57
58 return
59}
60
61// emailToDidOrHandle takes an emailToDidMap from db.GetEmailToDid
62// and resolves all dids to handles and returns a new map[string]string
63func emailToDidOrHandle(r *Repo, emailToDidMap map[string]string) map[string]string {
64 if emailToDidMap == nil {
65 return nil
66 }
67
68 var dids []string
69 for _, v := range emailToDidMap {
70 dids = append(dids, v)
71 }
72 resolvedIdents := r.idResolver.ResolveIdents(context.Background(), dids)
73
74 didHandleMap := make(map[string]string)
75 for _, identity := range resolvedIdents {
76 if !identity.Handle.IsInvalidHandle() {
77 didHandleMap[identity.DID.String()] = fmt.Sprintf("@%s", identity.Handle.String())
78 } else {
79 didHandleMap[identity.DID.String()] = identity.DID.String()
80 }
81 }
82
83 // Create map of email to didOrHandle for commit display
84 emailToDidOrHandle := make(map[string]string)
85 for email, did := range emailToDidMap {
86 if didOrHandle, ok := didHandleMap[did]; ok {
87 emailToDidOrHandle[email] = didOrHandle
88 }
89 }
90
91 return emailToDidOrHandle
92}
93
94func randomString(n int) string {
95 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
96 result := make([]byte, n)
97
98 for i := 0; i < n; i++ {
99 n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
100 result[i] = letters[n.Int64()]
101 }
102
103 return string(result)
104}
105
106// grab pipelines from DB and munge that into a hashmap with commit sha as key
107//
108// golang is so blessed that it requires 35 lines of imperative code for this
109func (rp *Repo) getPipelineStatuses(
110 repoInfo repoinfo.RepoInfo,
111 commits []*object.Commit,
112) (map[plumbing.Hash]db.Pipeline, error) {
113 m := make(map[plumbing.Hash]db.Pipeline)
114
115 if len(commits) == 0 {
116 return m, nil
117 }
118
119 shas := make([]string, len(commits))
120 for _, c := range commits {
121 shas = append(shas, c.Hash.String())
122 }
123
124 ps, err := db.GetPipelineStatuses(
125 rp.db,
126 db.FilterEq("repo_owner", repoInfo.OwnerDid),
127 db.FilterEq("repo_name", repoInfo.Name),
128 db.FilterEq("knot", repoInfo.Knot),
129 db.FilterIn("sha", shas),
130 )
131 if err != nil {
132 return nil, err
133 }
134
135 for _, p := range ps {
136 hash := plumbing.NewHash(p.Sha)
137 m[hash] = p
138 }
139
140 return m, nil
141}