This repository has no description
1package repo
2
3import (
4 "context"
5 "maps"
6 "slices"
7 "sort"
8 "strings"
9
10 indigoxrpc "github.com/bluesky-social/indigo/xrpc"
11 "tangled.org/core/api/tangled"
12 "tangled.org/core/appview/models"
13 "tangled.org/core/types"
14)
15
16func sortFiles(files []types.NiceTree) {
17 sort.Slice(files, func(i, j int) bool {
18 iIsFile := files[i].IsFile()
19 jIsFile := files[j].IsFile()
20 if iIsFile != jIsFile {
21 return !iIsFile
22 }
23 return files[i].Name < files[j].Name
24 })
25}
26
27func sortBranches(branches []types.Branch) {
28 slices.SortFunc(branches, func(a, b types.Branch) int {
29 if a.IsDefault {
30 return -1
31 }
32 if b.IsDefault {
33 return 1
34 }
35 if a.Commit != nil && b.Commit != nil {
36 if a.Commit.Committer.When.Before(b.Commit.Committer.When) {
37 return 1
38 } else {
39 return -1
40 }
41 }
42 return strings.Compare(a.Name, b.Name)
43 })
44}
45
46func uniqueEmails(commits []types.Commit) []string {
47 emails := make(map[string]struct{})
48 for _, commit := range commits {
49 emails[commit.Author.Email] = struct{}{}
50 emails[commit.Committer.Email] = struct{}{}
51 for _, c := range commit.CoAuthors() {
52 emails[c.Email] = struct{}{}
53 }
54 }
55
56 // delete empty emails if any, from the set
57 delete(emails, "")
58
59 return slices.Collect(maps.Keys(emails))
60}
61
62func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) {
63 if commitCount == 0 && tagCount == 0 && branchCount == 0 {
64 return
65 }
66
67 // typically 1 item on right side = 2 files in height
68 availableSpace := fileCount / 2
69
70 // clamp tagcount
71 if tagCount > 0 {
72 tagsTrunc = 1
73 availableSpace -= 1 // an extra subtracted for headers etc.
74 }
75
76 // clamp branchcount
77 if branchCount > 0 {
78 branchesTrunc = min(max(branchCount, 1), 3)
79 availableSpace -= branchesTrunc // an extra subtracted for headers etc.
80 }
81
82 // show
83 if commitCount > 0 {
84 commitsTrunc = max(availableSpace, 3)
85 }
86
87 return
88}
89
90// grab pipelines from DB and munge that into a hashmap with commit sha as key
91//
92// golang is so blessed that it requires 35 lines of imperative code for this
93func getPipelineStatuses(
94 ctx context.Context,
95 repo *models.Repo,
96 shas []string,
97) (map[string]*tangled.CiDefs_Pipeline, error) {
98 m := make(map[string]*tangled.CiDefs_Pipeline)
99
100 if len(shas) == 0 {
101 return m, nil
102 }
103
104 xrpcc := &indigoxrpc.Client{Host: repo.Spindle}
105 out, err := tangled.CiQueryPipelines(ctx, xrpcc, shas, "", 0, repo.RepoDid)
106 if err != nil {
107 return nil, err
108 }
109
110 for _, p := range out.Pipelines {
111 m[p.Commit] = p
112 }
113
114 return m, nil
115}