this repo has no description
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 ResolvedRepo struct { 22 models.Repo 23} 24 25type RepoResolver struct { 26 config *config.Config 27 enforcer *rbac.Enforcer 28 execer db.Execer 29} 30 31func New(config *config.Config, enforcer *rbac.Enforcer, execer db.Execer) *RepoResolver { 32 return &RepoResolver{config: config, enforcer: enforcer, execer: execer} 33} 34 35// NOTE: this... should not even be here. the entire package will be removed in future refactor 36func GetBaseRepoPath(r *http.Request, repo *models.Repo) string { 37 var ( 38 user = chi.URLParam(r, "user") 39 name = chi.URLParam(r, "repo") 40 ) 41 if user == "" || name == "" { 42 return repo.DidSlashRepo() 43 } 44 return path.Join(user, name) 45} 46 47// TODO: move this out of `RepoResolver` struct 48func (rr *RepoResolver) Resolve(r *http.Request) (*ResolvedRepo, error) { 49 repo, ok := r.Context().Value("repo").(*models.Repo) 50 if !ok { 51 log.Println("malformed middleware: `repo` not exist in context") 52 return nil, fmt.Errorf("malformed middleware") 53 } 54 55 return &ResolvedRepo{ 56 Repo: *repo, 57 }, nil 58} 59 60// 1. [x] replace `RepoInfo` to `reporesolver.GetRepoInfo(r *http.Request, repo, user)` 61// 2. [x] remove `rr`, `CurrentDir`, `Ref` fields from `ResolvedRepo` 62// 3. [ ] remove `ResolvedRepo` 63// 4. [ ] replace reporesolver to reposervice 64func (rr *RepoResolver) GetRepoInfo(r *http.Request, user *oauth.User) repoinfo.RepoInfo { 65 ownerId, ook := r.Context().Value("resolvedId").(identity.Identity) 66 repo, rok := r.Context().Value("repo").(*models.Repo) 67 if !ook || !rok { 68 log.Println("malformed request, failed to get repo from context") 69 } 70 71 // get dir/ref 72 currentDir := path.Dir(extractPathAfterRef(r.URL.EscapedPath())) 73 ref := chi.URLParam(r, "ref") 74 75 repoAt := repo.RepoAt() 76 isStarred := false 77 roles := repoinfo.RolesInRepo{} 78 if user != nil { 79 isStarred = db.GetStarStatus(rr.execer, user.Did, repoAt) 80 roles.Roles = rr.enforcer.GetPermissionsInRepo(user.Did, repo.Knot, repo.DidSlashRepo()) 81 } 82 83 stats := repo.RepoStats 84 if stats == nil { 85 starCount, err := db.GetStarCount(rr.execer, repoAt) 86 if err != nil { 87 log.Println("failed to get star count for ", repoAt) 88 } 89 issueCount, err := db.GetIssueCount(rr.execer, repoAt) 90 if err != nil { 91 log.Println("failed to get issue count for ", repoAt) 92 } 93 pullCount, err := db.GetPullCount(rr.execer, repoAt) 94 if err != nil { 95 log.Println("failed to get pull count for ", repoAt) 96 } 97 stats = &models.RepoStats{ 98 StarCount: starCount, 99 IssueCount: issueCount, 100 PullCount: pullCount, 101 } 102 } 103 104 var sourceRepo *models.Repo 105 var err error 106 if repo.Source != "" { 107 sourceRepo, err = db.GetRepoByAtUri(rr.execer, repo.Source) 108 if err != nil { 109 log.Println("failed to get repo by at uri", err) 110 } 111 } 112 113 repoInfo := repoinfo.RepoInfo{ 114 // this is basically a models.Repo 115 OwnerDid: ownerId.DID.String(), 116 OwnerHandle: ownerId.Handle.String(), 117 Name: repo.Name, 118 Rkey: repo.Rkey, 119 Description: repo.Description, 120 Website: repo.Website, 121 Topics: repo.Topics, 122 Knot: repo.Knot, 123 Spindle: repo.Spindle, 124 Stats: *stats, 125 126 // fork repo upstream 127 Source: sourceRepo, 128 129 // page context 130 CurrentDir: currentDir, 131 Ref: ref, 132 133 // info related to the session 134 IsStarred: isStarred, 135 Roles: roles, 136 } 137 138 return repoInfo 139} 140 141// extractPathAfterRef gets the actual repository path 142// after the ref. for example: 143// 144// /@icyphox.sh/foorepo/blob/main/abc/xyz/ => abc/xyz/ 145func extractPathAfterRef(fullPath string) string { 146 fullPath = strings.TrimPrefix(fullPath, "/") 147 148 // match blob/, tree/, or raw/ followed by any ref and then a slash 149 // 150 // captures everything after the final slash 151 pattern := `(?:blob|tree|raw)/[^/]+/(.*)$` 152 153 re := regexp.MustCompile(pattern) 154 matches := re.FindStringSubmatch(fullPath) 155 156 if len(matches) > 1 { 157 return matches[1] 158 } 159 160 return "" 161}