gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "errors"
  6. "net/http"
  7. "code.gitea.io/gitea/modules/git"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/services/context"
  10. "code.gitea.io/gitea/services/convert"
  11. )
  12. // GetNote Get a note corresponding to a single commit from a repository
  13. func GetNote(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/git/notes/{sha} repository repoGetNote
  15. // ---
  16. // summary: Get a note corresponding to a single commit from a repository
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // - name: sha
  31. // in: path
  32. // description: a git ref or commit sha
  33. // type: string
  34. // required: true
  35. // - name: verification
  36. // in: query
  37. // description: include verification for every commit (disable for speedup, default 'true')
  38. // type: boolean
  39. // - name: files
  40. // in: query
  41. // description: include a list of affected files for every commit (disable for speedup, default 'true')
  42. // type: boolean
  43. // responses:
  44. // "200":
  45. // "$ref": "#/responses/Note"
  46. // "422":
  47. // "$ref": "#/responses/validationError"
  48. // "404":
  49. // "$ref": "#/responses/notFound"
  50. sha := ctx.PathParam("sha")
  51. if !git.IsValidRefPattern(sha) {
  52. ctx.APIError(http.StatusUnprocessableEntity, "no valid ref or sha: "+sha)
  53. return
  54. }
  55. getNote(ctx, sha)
  56. }
  57. func getNote(ctx *context.APIContext, identifier string) {
  58. if ctx.Repo.GitRepo == nil {
  59. ctx.APIErrorInternal(errors.New("no open git repo"))
  60. return
  61. }
  62. commitID, err := ctx.Repo.GitRepo.ConvertToGitID(identifier)
  63. if err != nil {
  64. if git.IsErrNotExist(err) {
  65. ctx.APIErrorNotFound(err)
  66. } else {
  67. ctx.APIErrorInternal(err)
  68. }
  69. return
  70. }
  71. var note git.Note
  72. if err := git.GetNote(ctx, ctx.Repo.GitRepo, commitID.String(), &note); err != nil {
  73. if git.IsErrNotExist(err) {
  74. ctx.APIErrorNotFound("commit doesn't exist: " + identifier)
  75. return
  76. }
  77. ctx.APIErrorInternal(err)
  78. return
  79. }
  80. verification := ctx.FormString("verification") == "" || ctx.FormBool("verification")
  81. files := ctx.FormString("files") == "" || ctx.FormBool("files")
  82. cmt, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, note.Commit, nil,
  83. convert.ToCommitOptions{
  84. Stat: true,
  85. Verification: verification,
  86. Files: files,
  87. })
  88. if err != nil {
  89. ctx.APIErrorInternal(err)
  90. return
  91. }
  92. apiNote := api.Note{Message: string(note.Message), Commit: cmt}
  93. ctx.JSON(http.StatusOK, apiNote)
  94. }