appview: use less OwnerSlashRepo() in handlers #802

merged
opened by boltless.me targeting master from sl/yurolxtlpsmz
Changed files
+47 -19
appview
issues
middleware
pages
repoinfo
pulls
repo
reporesolver
+9 -4
appview/issues/issues.go
···
// notify about the issue closure
rp.notifier.NewIssueState(r.Context(), syntax.DID(user.Did), issue)
-
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", f.OwnerSlashRepo(), issue.IssueId))
+
ownerSlashRepo := rp.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", ownerSlashRepo, issue.IssueId))
return
} else {
l.Error("user is not permitted to close issue")
···
// notify about the issue reopen
rp.notifier.NewIssueState(r.Context(), syntax.DID(user.Did), issue)
-
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", f.OwnerSlashRepo(), issue.IssueId))
+
ownerSlashRepo := rp.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", ownerSlashRepo, issue.IssueId))
return
} else {
l.Error("user is not the owner of the repo")
···
}
rp.notifier.NewIssueComment(r.Context(), &comment, mentions)
-
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d#comment-%d", f.OwnerSlashRepo(), issue.IssueId, commentId))
+
ownerSlashRepo := rp.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d#comment-%d", ownerSlashRepo, issue.IssueId, commentId))
}
func (rp *Issues) IssueComment(w http.ResponseWriter, r *http.Request) {
···
}
}
rp.notifier.NewIssue(r.Context(), issue, mentions)
-
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", f.OwnerSlashRepo(), issue.IssueId))
+
+
ownerSlashRepo := rp.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
rp.pages.HxLocation(w, fmt.Sprintf("/%s/issues/%d", ownerSlashRepo, issue.IssueId))
return
}
}
+2 -2
appview/middleware/middleware.go
···
ok, err := mw.enforcer.E.Enforce(actor.Did, f.Knot, f.DidSlashRepo(), requiredPerm)
if err != nil || !ok {
// we need a logged in user
-
log.Printf("%s does not have perms of a %s in repo %s", actor.Did, requiredPerm, f.OwnerSlashRepo())
+
log.Printf("%s does not have perms of a %s in repo %s", actor.Did, requiredPerm, f.DidSlashRepo())
http.Error(w, "Forbiden", http.StatusUnauthorized)
return
}
···
return
}
-
fullName := f.OwnerHandle() + "/" + f.Name
+
fullName := mw.repoResolver.GetBaseRepoPath(r, &f.Repo)
if r.Header.Get("User-Agent") == "Go-http-client/1.1" {
if r.URL.Query().Get("go-get") == "1" {
+2 -2
appview/pages/repoinfo/repoinfo.go
···
return path.Join(r.owner(), r.Name)
}
-
func (r RepoInfo) OwnerWithoutAt() string {
+
func (r RepoInfo) ownerWithoutAt() string {
if r.OwnerHandle != "" {
return r.OwnerHandle
} else {
···
}
func (r RepoInfo) FullNameWithoutAt() string {
-
return path.Join(r.OwnerWithoutAt(), r.Name)
+
return path.Join(r.ownerWithoutAt(), r.Name)
}
func (r RepoInfo) GetTabs() [][]string {
+16 -8
appview/pulls/pulls.go
···
}
s.notifier.NewPullComment(r.Context(), comment, mentions)
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d#comment-%d", f.OwnerSlashRepo(), pull.PullId, commentId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d#comment-%d", ownerSlashRepo, pull.PullId, commentId))
return
}
}
···
s.notifier.NewPull(r.Context(), pull)
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pullId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pullId))
func (s *Pulls) createStackedPullRequest(
···
return
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls", f.OwnerSlashRepo()))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls", ownerSlashRepo))
func (s *Pulls) ValidatePatch(w http.ResponseWriter, r *http.Request) {
···
return
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pull.PullId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId))
func (s *Pulls) resubmitStackedPullHelper(
···
return
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pull.PullId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId))
func (s *Pulls) MergePull(w http.ResponseWriter, r *http.Request) {
···
s.notifier.NewPullState(r.Context(), syntax.DID(user.Did), p)
-
s.pages.HxLocation(w, fmt.Sprintf("/@%s/%s/pulls/%d", f.OwnerHandle(), f.Name, pull.PullId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId))
func (s *Pulls) ClosePull(w http.ResponseWriter, r *http.Request) {
···
s.notifier.NewPullState(r.Context(), syntax.DID(user.Did), p)
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pull.PullId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId))
func (s *Pulls) ReopenPull(w http.ResponseWriter, r *http.Request) {
···
s.notifier.NewPullState(r.Context(), syntax.DID(user.Did), p)
-
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pull.PullId))
+
ownerSlashRepo := s.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", ownerSlashRepo, pull.PullId))
func newStack(f *reporesolver.ResolvedRepo, user *oauth.User, targetBranch, patch string, pullSource *models.PullSource, stackId string) (models.Stack, error) {
+3 -1
appview/repo/blob.go
···
return
}
+
ownerSlashRepo := rp.repoResolver.GetBaseRepoPath(r, &f.Repo)
+
// Use XRPC response directly instead of converting to internal types
var breadcrumbs [][]string
-
breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", f.OwnerSlashRepo(), url.PathEscape(ref))})
+
breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", ownerSlashRepo, url.PathEscape(ref))})
if filePath != "" {
for idx, elem := range strings.Split(filePath, "/") {
breadcrumbs = append(breadcrumbs, []string{elem, fmt.Sprintf("%s/%s", breadcrumbs[idx][1], url.PathEscape(elem))})
+3 -2
appview/repo/tree.go
···
result.ReadmeFileName = xrpcResp.Readme.Filename
result.Readme = xrpcResp.Readme.Contents
}
+
ownerSlashRepo := rp.repoResolver.GetBaseRepoPath(r, &f.Repo)
// redirects tree paths trying to access a blob; in this case the result.Files is unpopulated,
// so we can safely redirect to the "parent" (which is the same file).
if len(result.Files) == 0 && result.Parent == treePath {
-
redirectTo := fmt.Sprintf("/%s/blob/%s/%s", f.OwnerSlashRepo(), url.PathEscape(ref), result.Parent)
+
redirectTo := fmt.Sprintf("/%s/blob/%s/%s", ownerSlashRepo, url.PathEscape(ref), result.Parent)
http.Redirect(w, r, redirectTo, http.StatusFound)
return
}
user := rp.oauth.GetUser(r)
var breadcrumbs [][]string
-
breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", f.OwnerSlashRepo(), url.PathEscape(ref))})
+
breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", ownerSlashRepo, url.PathEscape(ref))})
if treePath != "" {
for idx, elem := range strings.Split(treePath, "/") {
breadcrumbs = append(breadcrumbs, []string{elem, fmt.Sprintf("%s/%s", breadcrumbs[idx][1], url.PathEscape(elem))})
+12
appview/reporesolver/resolver.go
···
return &RepoResolver{config: config, enforcer: enforcer, idResolver: resolver, execer: execer}
}
+
// NOTE: this... should not even be here. the entire package will be removed in future refactor
+
func (rr *RepoResolver) GetBaseRepoPath(r *http.Request, repo *models.Repo) string {
+
var (
+
user = chi.URLParam(r, "user")
+
name = chi.URLParam(r, "repo")
+
)
+
if user == "" || name == "" {
+
return repo.DidSlashRepo()
+
}
+
return path.Join(user, name)
+
}
+
func (rr *RepoResolver) Resolve(r *http.Request) (*ResolvedRepo, error) {
repo, ok := r.Context().Value("repo").(*models.Repo)
if !ok {