Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

api.go 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 total 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. // ReferencesGitRepo injects the GitRepo into the Context
  68. func ReferencesGitRepo() macaron.Handler {
  69. return func(ctx *APIContext) {
  70. // Empty repository does not have reference information.
  71. if ctx.Repo.Repository.IsBare {
  72. return
  73. }
  74. // For API calls.
  75. if ctx.Repo.GitRepo == nil {
  76. repoPath := models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  77. gitRepo, err := git.OpenRepository(repoPath)
  78. if err != nil {
  79. ctx.Error(500, "RepoRef Invalid repo "+repoPath, err)
  80. return
  81. }
  82. ctx.Repo.GitRepo = gitRepo
  83. }
  84. }
  85. }