Monorepo for Tangled
at 74318eac9fdd72cf69e916276814351931ed0dcb 137 lines 4.4 kB view raw
1package repo 2 3import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "strings" 8 "time" 9 10 "tangled.org/core/api/tangled" 11 "tangled.org/core/appview/db" 12 "tangled.org/core/appview/pages" 13 "tangled.org/core/appview/reporesolver" 14 xrpcclient "tangled.org/core/appview/xrpcclient" 15 "tangled.org/core/types" 16 17 indigoxrpc "github.com/bluesky-social/indigo/xrpc" 18 "github.com/go-chi/chi/v5" 19 "github.com/go-git/go-git/v5/plumbing" 20) 21 22func (rp *Repo) Tree(w http.ResponseWriter, r *http.Request) { 23 l := rp.logger.With("handler", "RepoTree") 24 f, err := rp.repoResolver.Resolve(r) 25 if err != nil { 26 l.Error("failed to fully resolve repo", "err", err) 27 return 28 } 29 ref := chi.URLParam(r, "ref") 30 ref, _ = url.PathUnescape(ref) 31 // if the tree path has a trailing slash, let's strip it 32 // so we don't 404 33 treePath := chi.URLParam(r, "*") 34 treePath, _ = url.PathUnescape(treePath) 35 treePath = strings.TrimSuffix(treePath, "/") 36 scheme := "http" 37 if !rp.config.Core.Dev { 38 scheme = "https" 39 } 40 host := fmt.Sprintf("%s://%s", scheme, f.Knot) 41 xrpcc := &indigoxrpc.Client{ 42 Host: host, 43 } 44 repo := fmt.Sprintf("%s/%s", f.Did, f.Name) 45 xrpcResp, err := tangled.RepoTree(r.Context(), xrpcc, treePath, ref, repo) 46 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 47 l.Error("failed to call XRPC repo.tree", "err", xrpcerr) 48 rp.pages.Error503(w) 49 return 50 } 51 // Convert XRPC response to internal types.RepoTreeResponse 52 files := make([]types.NiceTree, len(xrpcResp.Files)) 53 for i, xrpcFile := range xrpcResp.Files { 54 file := types.NiceTree{ 55 Name: xrpcFile.Name, 56 Mode: xrpcFile.Mode, 57 Size: int64(xrpcFile.Size), 58 } 59 // Convert last commit info if present 60 if xrpcFile.Last_commit != nil { 61 commitWhen, _ := time.Parse(time.RFC3339, xrpcFile.Last_commit.When) 62 file.LastCommit = &types.LastCommitInfo{ 63 Hash: plumbing.NewHash(xrpcFile.Last_commit.Hash), 64 Message: xrpcFile.Last_commit.Message, 65 When: commitWhen, 66 } 67 } 68 files[i] = file 69 } 70 result := types.RepoTreeResponse{ 71 Ref: xrpcResp.Ref, 72 Files: files, 73 } 74 if xrpcResp.Parent != nil { 75 result.Parent = *xrpcResp.Parent 76 } 77 if xrpcResp.Dotdot != nil { 78 result.DotDot = *xrpcResp.Dotdot 79 } 80 if xrpcResp.Readme != nil { 81 result.ReadmeFileName = xrpcResp.Readme.Filename 82 result.Readme = xrpcResp.Readme.Contents 83 } 84 ownerSlashRepo := reporesolver.GetBaseRepoPath(r, f) 85 // redirects tree paths trying to access a blob; in this case the result.Files is unpopulated, 86 // so we can safely redirect to the "parent" (which is the same file). 87 if len(result.Files) == 0 && result.Parent == treePath { 88 redirectTo := fmt.Sprintf("/%s/blob/%s/%s", ownerSlashRepo, url.PathEscape(ref), result.Parent) 89 http.Redirect(w, r, redirectTo, http.StatusFound) 90 return 91 } 92 user := rp.oauth.GetMultiAccountUser(r) 93 var breadcrumbs [][]string 94 breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", ownerSlashRepo, url.PathEscape(ref))}) 95 if treePath != "" { 96 for idx, elem := range strings.Split(treePath, "/") { 97 breadcrumbs = append(breadcrumbs, []string{elem, fmt.Sprintf("%s/%s", breadcrumbs[idx][1], url.PathEscape(elem))}) 98 } 99 } 100 sortFiles(result.Files) 101 102 // Get email to DID mapping for commit author 103 var emails []string 104 if xrpcResp.LastCommit != nil && xrpcResp.LastCommit.Author != nil { 105 emails = append(emails, xrpcResp.LastCommit.Author.Email) 106 } 107 emailToDidMap, err := db.GetEmailToDid(rp.db, emails, true) 108 if err != nil { 109 l.Error("failed to get email to did mapping", "err", err) 110 emailToDidMap = make(map[string]string) 111 } 112 113 var lastCommitInfo *types.LastCommitInfo 114 if xrpcResp.LastCommit != nil { 115 when, _ := time.Parse(time.RFC3339, xrpcResp.LastCommit.When) 116 lastCommitInfo = &types.LastCommitInfo{ 117 Hash: plumbing.NewHash(xrpcResp.LastCommit.Hash), 118 Message: xrpcResp.LastCommit.Message, 119 When: when, 120 } 121 if xrpcResp.LastCommit.Author != nil { 122 lastCommitInfo.Author.Name = xrpcResp.LastCommit.Author.Name 123 lastCommitInfo.Author.Email = xrpcResp.LastCommit.Author.Email 124 lastCommitInfo.Author.When, _ = time.Parse(time.RFC3339, xrpcResp.LastCommit.Author.When) 125 } 126 } 127 128 rp.pages.RepoTree(w, pages.RepoTreeParams{ 129 LoggedInUser: user, 130 BreadCrumbs: breadcrumbs, 131 TreePath: treePath, 132 RepoInfo: rp.repoResolver.GetRepoInfo(r, user), 133 EmailToDid: emailToDidMap, 134 LastCommitInfo: lastCommitInfo, 135 RepoTreeResponse: result, 136 }) 137}