You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/Unknwon/paginater"
  14. macaron "gopkg.in/macaron.v1"
  15. )
  16. // APIContext is a specific macaron context for API service
  17. type APIContext struct {
  18. *Context
  19. Org *APIOrganization
  20. }
  21. // Error responses error message to client with given message.
  22. // If status is 500, also it prints error to log.
  23. func (ctx *APIContext) Error(status int, title string, obj interface{}) {
  24. var message string
  25. if err, ok := obj.(error); ok {
  26. message = err.Error()
  27. } else {
  28. message = obj.(string)
  29. }
  30. if status == 500 {
  31. log.Error(4, "%s: %s", title, message)
  32. }
  33. ctx.JSON(status, map[string]string{
  34. "message": message,
  35. "url": base.DocURL,
  36. })
  37. }
  38. // SetLinkHeader sets pagination link header by given totol number and page size.
  39. func (ctx *APIContext) SetLinkHeader(total, pageSize int) {
  40. page := paginater.New(total, pageSize, ctx.QueryInt("page"), 0)
  41. links := make([]string, 0, 4)
  42. if page.HasNext() {
  43. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"next\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Next()))
  44. }
  45. if !page.IsLast() {
  46. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"last\"", setting.AppURL, ctx.Req.URL.Path[1:], page.TotalPages()))
  47. }
  48. if !page.IsFirst() {
  49. links = append(links, fmt.Sprintf("<%s%s?page=1>; rel=\"first\"", setting.AppURL, ctx.Req.URL.Path[1:]))
  50. }
  51. if page.HasPrevious() {
  52. links = append(links, fmt.Sprintf("<%s%s?page=%d>; rel=\"prev\"", setting.AppURL, ctx.Req.URL.Path[1:], page.Previous()))
  53. }
  54. if len(links) > 0 {
  55. ctx.Header().Set("Link", strings.Join(links, ","))
  56. }
  57. }
  58. // APIContexter returns apicontext as macaron middleware
  59. func APIContexter() macaron.Handler {
  60. return func(c *Context) {
  61. ctx := &APIContext{
  62. Context: c,
  63. }
  64. c.Map(ctx)
  65. }
  66. }
  67. // ExtractOwnerAndRepo returns a handler that populates the `Repo.Owner` and
  68. // `Repo.Repository` fields of an APIContext
  69. func ExtractOwnerAndRepo() macaron.Handler {
  70. return func(ctx *APIContext) {
  71. owner, err := models.GetUserByName(ctx.Params(":username"))
  72. if err != nil {
  73. if models.IsErrUserNotExist(err) {
  74. ctx.Error(422, "", err)
  75. } else {
  76. ctx.Error(500, "GetUserByName", err)
  77. }
  78. return
  79. }
  80. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  81. if err != nil {
  82. if models.IsErrRepoNotExist(err) {
  83. ctx.Status(404)
  84. } else {
  85. ctx.Error(500, "GetRepositoryByName", err)
  86. }
  87. return
  88. }
  89. ctx.Repo.Owner = owner
  90. ctx.Data["Owner"] = owner
  91. ctx.Repo.Repository = repo
  92. ctx.Data["Repository"] = repo
  93. }
  94. }
  95. // ReferencesGitRepo injects the GitRepo into the Context
  96. func ReferencesGitRepo() macaron.Handler {
  97. return func(ctx *APIContext) {
  98. // Empty repository does not have reference information.
  99. if ctx.Repo.Repository.IsBare {
  100. return
  101. }
  102. // For API calls.
  103. if ctx.Repo.GitRepo == nil {
  104. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  105. gitRepo, err := git.OpenRepository(repoPath)
  106. if err != nil {
  107. ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
  108. return
  109. }
  110. ctx.Repo.GitRepo = gitRepo
  111. }
  112. }
  113. }