Monorepo for Tangled
1package reporesolver
2
3import (
4 "fmt"
5 "log"
6 "net/http"
7 "path"
8 "regexp"
9 "strings"
10
11 "github.com/bluesky-social/indigo/atproto/identity"
12 "github.com/go-chi/chi/v5"
13 "tangled.org/core/appview/config"
14 "tangled.org/core/appview/db"
15 "tangled.org/core/appview/models"
16 "tangled.org/core/appview/oauth"
17 "tangled.org/core/appview/pages/repoinfo"
18 "tangled.org/core/rbac"
19)
20
21type RepoResolver struct {
22 config *config.Config
23 enforcer *rbac.Enforcer
24 execer db.Execer
25}
26
27func New(config *config.Config, enforcer *rbac.Enforcer, execer db.Execer) *RepoResolver {
28 return &RepoResolver{config: config, enforcer: enforcer, execer: execer}
29}
30
31// NOTE: this... should not even be here. the entire package will be removed in future refactor
32func GetBaseRepoPath(r *http.Request, repo *models.Repo) string {
33 if repo.RepoDid != "" {
34 return repo.RepoDid
35 }
36 var (
37 user = chi.URLParam(r, "user")
38 name = chi.URLParam(r, "repo")
39 )
40 if user == "" || name == "" {
41 return repo.RepoIdentifier()
42 }
43 return path.Join(user, name)
44}
45
46// TODO: move this out of `RepoResolver` struct
47func (rr *RepoResolver) Resolve(r *http.Request) (*models.Repo, error) {
48 repo, ok := r.Context().Value("repo").(*models.Repo)
49 if !ok {
50 log.Println("malformed middleware: `repo` not exist in context")
51 return nil, fmt.Errorf("malformed middleware")
52 }
53
54 return repo, nil
55}
56
57// 1. [x] replace `RepoInfo` to `reporesolver.GetRepoInfo(r *http.Request, repo, user)`
58// 2. [x] remove `rr`, `CurrentDir`, `Ref` fields from `ResolvedRepo`
59// 3. [x] remove `ResolvedRepo`
60// 4. [ ] replace reporesolver to reposervice
61func (rr *RepoResolver) GetRepoInfo(r *http.Request, user *oauth.MultiAccountUser) repoinfo.RepoInfo {
62 ownerId, ook := r.Context().Value("resolvedId").(identity.Identity)
63 repo, rok := r.Context().Value("repo").(*models.Repo)
64 if !ook || !rok {
65 log.Println("malformed request, failed to get repo from context")
66 }
67
68 // get dir/ref
69 currentDir := extractCurrentDir(r.URL.EscapedPath())
70 ref := chi.URLParam(r, "ref")
71
72 repoAt := repo.RepoAt()
73 isStarred := false
74 roles := repoinfo.RolesInRepo{}
75 if user != nil && user.Active != nil {
76 isStarred = db.GetStarStatus(rr.execer, user.Active.Did, repoAt)
77 roles.Roles = rr.enforcer.GetPermissionsInRepo(user.Active.Did, repo.Knot, repo.RepoIdentifier())
78 }
79
80 stats := repo.RepoStats
81 if stats == nil {
82 var starCount int
83 var starErr error
84 if repo.RepoDid != "" {
85 starCount, starErr = db.GetStarCountByRepoDid(rr.execer, repo.RepoDid, repoAt)
86 } else {
87 starCount, starErr = db.GetStarCount(rr.execer, repoAt)
88 }
89 if starErr != nil {
90 log.Println("failed to get star count for ", repoAt)
91 }
92 issueCount, err := db.GetIssueCount(rr.execer, repoAt)
93 if err != nil {
94 log.Println("failed to get issue count for ", repoAt)
95 }
96 pullCount, err := db.GetPullCount(rr.execer, repoAt)
97 if err != nil {
98 log.Println("failed to get pull count for ", repoAt)
99 }
100 stats = &models.RepoStats{
101 StarCount: starCount,
102 IssueCount: issueCount,
103 PullCount: pullCount,
104 }
105 }
106
107 var sourceRepo *models.Repo
108 var err error
109 if repo.Source != "" {
110 if strings.HasPrefix(repo.Source, "did:") {
111 sourceRepo, err = db.GetRepoByDid(rr.execer, repo.Source)
112 } else {
113 sourceRepo, err = db.GetRepoByAtUri(rr.execer, repo.Source)
114 }
115 if err != nil {
116 log.Println("failed to get source repo", err)
117 }
118 }
119
120 repoInfo := repoinfo.RepoInfo{
121 // this is basically a models.Repo
122 OwnerDid: ownerId.DID.String(),
123 OwnerHandle: ownerId.Handle.String(),
124 RepoDid: repo.RepoDid,
125 Name: repo.Name,
126 Rkey: repo.Rkey,
127 Description: repo.Description,
128 Website: repo.Website,
129 Topics: repo.Topics,
130 Knot: repo.Knot,
131 Spindle: repo.Spindle,
132 Stats: *stats,
133
134 // fork repo upstream
135 Source: sourceRepo,
136
137 // page context
138 CurrentDir: currentDir,
139 Ref: ref,
140
141 // info related to the session
142 IsStarred: isStarred,
143 Roles: roles,
144 }
145
146 return repoInfo
147}
148
149// extractCurrentDir gets the current directory for markdown link resolution.
150// for blob paths, returns the parent dir. for tree paths, returns the path itself.
151//
152// /@user/repo/blob/main/docs/README.md => docs
153// /@user/repo/tree/main/docs => docs
154func extractCurrentDir(fullPath string) string {
155 fullPath = strings.TrimPrefix(fullPath, "/")
156
157 blobPattern := regexp.MustCompile(`blob/[^/]+/(.*)$`)
158 if matches := blobPattern.FindStringSubmatch(fullPath); len(matches) > 1 {
159 return path.Dir(matches[1])
160 }
161
162 treePattern := regexp.MustCompile(`tree/[^/]+/(.*)$`)
163 if matches := treePattern.FindStringSubmatch(fullPath); len(matches) > 1 {
164 dir := strings.TrimSuffix(matches[1], "/")
165 if dir == "" {
166 return "."
167 }
168 return dir
169 }
170
171 return "."
172}
173
174// extractPathAfterRef gets the actual repository path
175// after the ref. for example:
176//
177// /@icyphox.sh/foorepo/blob/main/abc/xyz/ => abc/xyz/
178func extractPathAfterRef(fullPath string) string {
179 fullPath = strings.TrimPrefix(fullPath, "/")
180
181 // match blob/, tree/, or raw/ followed by any ref and then a slash
182 //
183 // captures everything after the final slash
184 pattern := `(?:blob|tree|raw)/[^/]+/(.*)$`
185
186 re := regexp.MustCompile(pattern)
187 matches := re.FindStringSubmatch(fullPath)
188
189 if len(matches) > 1 {
190 return matches[1]
191 }
192
193 return ""
194}