gitea源码

renderhelper_issueicontitle.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "html/template"
  9. "code.gitea.io/gitea/models/issues"
  10. "code.gitea.io/gitea/models/perm/access"
  11. "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/modules/htmlutil"
  13. "code.gitea.io/gitea/modules/markup"
  14. "code.gitea.io/gitea/modules/util"
  15. gitea_context "code.gitea.io/gitea/services/context"
  16. )
  17. func renderRepoIssueIconTitle(ctx context.Context, opts markup.RenderIssueIconTitleOptions) (_ template.HTML, err error) {
  18. webCtx := gitea_context.GetWebContext(ctx)
  19. if webCtx == nil {
  20. return "", errors.New("context is not a web context")
  21. }
  22. textIssueIndex := fmt.Sprintf("(#%d)", opts.IssueIndex)
  23. dbRepo := webCtx.Repo.Repository
  24. if opts.OwnerName != "" {
  25. dbRepo, err = repo.GetRepositoryByOwnerAndName(ctx, opts.OwnerName, opts.RepoName)
  26. if err != nil {
  27. return "", err
  28. }
  29. textIssueIndex = fmt.Sprintf("(%s/%s#%d)", dbRepo.OwnerName, dbRepo.Name, opts.IssueIndex)
  30. }
  31. if dbRepo == nil {
  32. return "", nil
  33. }
  34. issue, err := issues.GetIssueByIndex(ctx, dbRepo.ID, opts.IssueIndex)
  35. if err != nil {
  36. return "", err
  37. }
  38. if webCtx.Repo.Repository == nil || dbRepo.ID != webCtx.Repo.Repository.ID {
  39. perms, err := access.GetUserRepoPermission(ctx, dbRepo, webCtx.Doer)
  40. if err != nil {
  41. return "", err
  42. }
  43. if !perms.CanReadIssuesOrPulls(issue.IsPull) {
  44. return "", util.ErrPermissionDenied
  45. }
  46. }
  47. if issue.IsPull {
  48. if err = issue.LoadPullRequest(ctx); err != nil {
  49. return "", err
  50. }
  51. }
  52. htmlIcon, err := webCtx.RenderToHTML("shared/issueicon", issue)
  53. if err != nil {
  54. return "", err
  55. }
  56. return htmlutil.HTMLFormat(`<a href="%s">%s %s %s</a>`, opts.LinkHref, htmlIcon, issue.Title, textIssueIndex), nil
  57. }