forked from
tangled.org/core
Monorepo for Tangled — https://tangled.org
1package repoinfo
2
3import (
4 "fmt"
5 "path"
6 "slices"
7
8 "github.com/bluesky-social/indigo/atproto/syntax"
9 "tangled.org/core/api/tangled"
10 "tangled.org/core/appview/models"
11 "tangled.org/core/appview/state/userutil"
12)
13
14func (r RepoInfo) owner() string {
15 if r.OwnerHandle != "" {
16 return r.OwnerHandle
17 } else {
18 return r.OwnerDid
19 }
20}
21
22func (r RepoInfo) FullName() string {
23 return path.Join(r.owner(), r.Name)
24}
25
26func (r RepoInfo) ownerWithoutAt() string {
27 if r.OwnerHandle != "" {
28 return r.OwnerHandle
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
53func (r RepoInfo) RepoAt() syntax.ATURI {
54 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.OwnerDid, tangled.RepoNSID, r.Rkey))
55}
56
57type RepoInfo struct {
58 Name string
59 Rkey string
60 OwnerDid string
61 OwnerHandle string
62 Description string
63 Website string
64 Topics []string
65 Knot string
66 Spindle string
67 IsStarred bool
68 Stats models.RepoStats
69 Roles RolesInRepo
70 Source *models.Repo
71 Ref string
72 CurrentDir string
73}
74
75// each tab on a repo could have some metadata:
76//
77// issues -> number of open issues etc.
78// settings -> a warning icon to setup branch protection? idk
79//
80// we gather these bits of info here, because go templates
81// are difficult to program in
82func (r RepoInfo) TabMetadata() map[string]any {
83 meta := make(map[string]any)
84
85 meta["pulls"] = r.Stats.PullCount.Open
86 meta["issues"] = r.Stats.IssueCount.Open
87
88 // more stuff?
89
90 return meta
91}
92
93type RolesInRepo struct {
94 Roles []string
95}
96
97func (r RolesInRepo) SettingsAllowed() bool {
98 return slices.Contains(r.Roles, "repo:settings")
99}
100
101func (r RolesInRepo) CollaboratorInviteAllowed() bool {
102 return slices.Contains(r.Roles, "repo:invite")
103}
104
105func (r RolesInRepo) RepoDeleteAllowed() bool {
106 return slices.Contains(r.Roles, "repo:delete")
107}
108
109func (r RolesInRepo) IsOwner() bool {
110 return slices.Contains(r.Roles, "repo:owner")
111}
112
113func (r RolesInRepo) IsCollaborator() bool {
114 return slices.Contains(r.Roles, "repo:collaborator")
115}
116
117func (r RolesInRepo) IsPushAllowed() bool {
118 return slices.Contains(r.Roles, "repo:push")
119}