1package repoinfo
2
3import (
4 "fmt"
5 "path"
6 "slices"
7 "strings"
8
9 "github.com/bluesky-social/indigo/atproto/syntax"
10 "tangled.org/core/appview/models"
11 "tangled.org/core/appview/state/userutil"
12)
13
14func (r RepoInfo) OwnerWithAt() string {
15 if r.OwnerHandle != "" {
16 return fmt.Sprintf("@%s", r.OwnerHandle)
17 } else {
18 return r.OwnerDid
19 }
20}
21
22func (r RepoInfo) FullName() string {
23 return path.Join(r.OwnerWithAt(), r.Name)
24}
25
26func (r RepoInfo) OwnerWithoutAt() string {
27 if after, ok := strings.CutPrefix(r.OwnerWithAt(), "@"); ok {
28 return after
29 } else {
30 return userutil.FlattenDid(r.OwnerDid)
31 }
32}
33
34func (r RepoInfo) FullNameWithoutAt() string {
35 return path.Join(r.OwnerWithoutAt(), r.Name)
36}
37
38func (r RepoInfo) GetTabs() [][]string {
39 tabs := [][]string{
40 {"overview", "/", "square-chart-gantt"},
41 {"issues", "/issues", "circle-dot"},
42 {"pulls", "/pulls", "git-pull-request"},
43 {"pipelines", "/pipelines", "layers-2"},
44 }
45
46 if r.Roles.SettingsAllowed() {
47 tabs = append(tabs, []string{"settings", "/settings", "cog"})
48 }
49
50 return tabs
51}
52
53type RepoInfo struct {
54 Name string
55 Rkey string
56 OwnerDid string
57 OwnerHandle string
58 Description string
59 Knot string
60 Spindle string
61 RepoAt syntax.ATURI
62 IsStarred bool
63 Stats models.RepoStats
64 Roles RolesInRepo
65 Source *models.Repo
66 SourceHandle string
67 Ref string
68 DisableFork bool
69 CurrentDir string
70}
71
72// each tab on a repo could have some metadata:
73//
74// issues -> number of open issues etc.
75// settings -> a warning icon to setup branch protection? idk
76//
77// we gather these bits of info here, because go templates
78// are difficult to program in
79func (r RepoInfo) TabMetadata() map[string]any {
80 meta := make(map[string]any)
81
82 meta["pulls"] = r.Stats.PullCount.Open
83 meta["issues"] = r.Stats.IssueCount.Open
84
85 // more stuff?
86
87 return meta
88}
89
90type RolesInRepo struct {
91 Roles []string
92}
93
94func (r RolesInRepo) SettingsAllowed() bool {
95 return slices.Contains(r.Roles, "repo:settings")
96}
97
98func (r RolesInRepo) CollaboratorInviteAllowed() bool {
99 return slices.Contains(r.Roles, "repo:invite")
100}
101
102func (r RolesInRepo) RepoDeleteAllowed() bool {
103 return slices.Contains(r.Roles, "repo:delete")
104}
105
106func (r RolesInRepo) IsOwner() bool {
107 return slices.Contains(r.Roles, "repo:owner")
108}
109
110func (r RolesInRepo) IsCollaborator() bool {
111 return slices.Contains(r.Roles, "repo:collaborator")
112}
113
114func (r RolesInRepo) IsPushAllowed() bool {
115 return slices.Contains(r.Roles, "repo:push")
116}