forked from
tangled.org/core
Monorepo for Tangled
2.6 kB
119 lines
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/hostutil"
14 "tangled.org/core/types"
15)
16
17func sortFiles(files []types.NiceTree) {
18 sort.Slice(files, func(i, j int) bool {
19 iIsFile := files[i].IsFile()
20 jIsFile := files[j].IsFile()
21 if iIsFile != jIsFile {
22 return !iIsFile
23 }
24 return files[i].Name < files[j].Name
25 })
26}
27
28func sortBranches(branches []types.Branch) {
29 slices.SortFunc(branches, func(a, b types.Branch) int {
30 if a.IsDefault {
31 return -1
32 }
33 if b.IsDefault {
34 return 1
35 }
36 if a.Commit != nil && b.Commit != nil {
37 if a.Commit.Committer.When.Before(b.Commit.Committer.When) {
38 return 1
39 } else {
40 return -1
41 }
42 }
43 return strings.Compare(a.Name, b.Name)
44 })
45}
46
47func uniqueEmails(commits []types.Commit) []string {
48 emails := make(map[string]struct{})
49 for _, commit := range commits {
50 emails[commit.Author.Email] = struct{}{}
51 emails[commit.Committer.Email] = struct{}{}
52 for _, c := range commit.CoAuthors() {
53 emails[c.Email] = struct{}{}
54 }
55 }
56
57 // delete empty emails if any, from the set
58 delete(emails, "")
59
60 return slices.Collect(maps.Keys(emails))
61}
62
63func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) {
64 if commitCount == 0 && tagCount == 0 && branchCount == 0 {
65 return
66 }
67
68 // typically 1 item on right side = 2 files in height
69 availableSpace := fileCount / 2
70
71 // clamp tagcount
72 if tagCount > 0 {
73 tagsTrunc = 1
74 availableSpace -= 1 // an extra subtracted for headers etc.
75 }
76
77 // clamp branchcount
78 if branchCount > 0 {
79 branchesTrunc = min(max(branchCount, 1), 3)
80 availableSpace -= branchesTrunc // an extra subtracted for headers etc.
81 }
82
83 // show
84 if commitCount > 0 {
85 commitsTrunc = max(availableSpace, 3)
86 }
87
88 return
89}
90
91// fetch pipelines from spindle and map by commit sha
92func getPipelineStatuses(
93 ctx context.Context,
94 repo *models.Repo,
95 shas []string,
96) (map[string]types.Pipeline, error) {
97 m := make(map[string]types.Pipeline)
98
99 if len(shas) == 0 {
100 return m, nil
101 }
102
103 if repo.Spindle == "" {
104 return m, nil
105 }
106
107 spindleUrl, err := hostutil.EnsureHttpScheme(repo.Spindle)
108 if err != nil {
109 return m, nil
110 }
111
112 xrpcc := &indigoxrpc.Client{Host: spindleUrl}
113 out, err := tangled.CiQueryPipelines(ctx, xrpcc, shas, "", nil, 0, repo.RepoDid)
114 if err != nil {
115 return nil, err
116 }
117
118 return types.PipelinesByCommit(out.Pipelines), nil
119}