gitea源码

locks.go 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package lfs
  4. import (
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. git_model "code.gitea.io/gitea/models/git"
  10. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/modules/json"
  12. lfs_module "code.gitea.io/gitea/modules/lfs"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/services/context"
  17. "code.gitea.io/gitea/services/convert"
  18. )
  19. func handleLockListOut(ctx *context.Context, repo *repo_model.Repository, lock *git_model.LFSLock, err error) {
  20. if err != nil {
  21. if git_model.IsErrLFSLockNotExist(err) {
  22. ctx.JSON(http.StatusOK, api.LFSLockList{
  23. Locks: []*api.LFSLock{},
  24. })
  25. return
  26. }
  27. ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
  28. Message: "unable to list locks : Internal Server Error",
  29. })
  30. return
  31. }
  32. if repo.ID != lock.RepoID {
  33. ctx.JSON(http.StatusOK, api.LFSLockList{
  34. Locks: []*api.LFSLock{},
  35. })
  36. return
  37. }
  38. ctx.JSON(http.StatusOK, api.LFSLockList{
  39. Locks: []*api.LFSLock{convert.ToLFSLock(ctx, lock)},
  40. })
  41. }
  42. // GetListLockHandler list locks
  43. func GetListLockHandler(ctx *context.Context) {
  44. rv := getRequestContext(ctx)
  45. repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, rv.User, rv.Repo)
  46. if err != nil {
  47. log.Debug("Could not find repository: %s/%s - %s", rv.User, rv.Repo, err)
  48. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  49. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  50. Message: "You must have pull access to list locks",
  51. })
  52. return
  53. }
  54. repository.MustOwner(ctx)
  55. context.CheckRepoScopedToken(ctx, repository, auth_model.Read)
  56. if ctx.Written() {
  57. return
  58. }
  59. authenticated := authenticate(ctx, repository, rv.Authorization, true, false)
  60. if !authenticated {
  61. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  62. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  63. Message: "You must have pull access to list locks",
  64. })
  65. return
  66. }
  67. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  68. cursor := max(ctx.FormInt("cursor"), 0)
  69. limit := ctx.FormInt("limit")
  70. if limit > setting.LFS.LocksPagingNum && setting.LFS.LocksPagingNum > 0 {
  71. limit = setting.LFS.LocksPagingNum
  72. } else if limit < 0 {
  73. limit = 0
  74. }
  75. id := ctx.FormString("id")
  76. if id != "" { // Case where we request a specific id
  77. v, err := strconv.ParseInt(id, 10, 64)
  78. if err != nil {
  79. ctx.JSON(http.StatusBadRequest, api.LFSLockError{
  80. Message: "bad request : " + err.Error(),
  81. })
  82. return
  83. }
  84. lock, err := git_model.GetLFSLockByID(ctx, v)
  85. if err != nil && !git_model.IsErrLFSLockNotExist(err) {
  86. log.Error("Unable to get lock with ID[%s]: Error: %v", v, err)
  87. }
  88. handleLockListOut(ctx, repository, lock, err)
  89. return
  90. }
  91. path := ctx.FormString("path")
  92. if path != "" { // Case where we request a specific id
  93. lock, err := git_model.GetLFSLock(ctx, repository, path)
  94. if err != nil && !git_model.IsErrLFSLockNotExist(err) {
  95. log.Error("Unable to get lock for repository %-v with path %s: Error: %v", repository, path, err)
  96. }
  97. handleLockListOut(ctx, repository, lock, err)
  98. return
  99. }
  100. // If no query params path or id
  101. lockList, err := git_model.GetLFSLockByRepoID(ctx, repository.ID, cursor, limit)
  102. if err != nil {
  103. log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
  104. ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
  105. Message: "unable to list locks : Internal Server Error",
  106. })
  107. return
  108. }
  109. lockListAPI := make([]*api.LFSLock, len(lockList))
  110. next := ""
  111. for i, l := range lockList {
  112. lockListAPI[i] = convert.ToLFSLock(ctx, l)
  113. }
  114. if limit > 0 && len(lockList) == limit {
  115. next = strconv.Itoa(cursor + 1)
  116. }
  117. ctx.JSON(http.StatusOK, api.LFSLockList{
  118. Locks: lockListAPI,
  119. Next: next,
  120. })
  121. }
  122. // PostLockHandler create lock
  123. func PostLockHandler(ctx *context.Context) {
  124. userName := ctx.PathParam("username")
  125. repoName := strings.TrimSuffix(ctx.PathParam("reponame"), ".git")
  126. authorization := ctx.Req.Header.Get("Authorization")
  127. repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName)
  128. if err != nil {
  129. log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
  130. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  131. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  132. Message: "You must have push access to create locks",
  133. })
  134. return
  135. }
  136. repository.MustOwner(ctx)
  137. context.CheckRepoScopedToken(ctx, repository, auth_model.Write)
  138. if ctx.Written() {
  139. return
  140. }
  141. authenticated := authenticate(ctx, repository, authorization, true, true)
  142. if !authenticated {
  143. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  144. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  145. Message: "You must have push access to create locks",
  146. })
  147. return
  148. }
  149. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  150. var req api.LFSLockRequest
  151. bodyReader := ctx.Req.Body
  152. defer bodyReader.Close()
  153. dec := json.NewDecoder(bodyReader)
  154. if err := dec.Decode(&req); err != nil {
  155. log.Warn("Failed to decode lock request as json. Error: %v", err)
  156. writeStatus(ctx, http.StatusBadRequest)
  157. return
  158. }
  159. lock, err := git_model.CreateLFSLock(ctx, repository, &git_model.LFSLock{
  160. Path: req.Path,
  161. OwnerID: ctx.Doer.ID,
  162. })
  163. if err != nil {
  164. if git_model.IsErrLFSLockAlreadyExist(err) {
  165. ctx.JSON(http.StatusConflict, api.LFSLockError{
  166. Lock: convert.ToLFSLock(ctx, lock),
  167. Message: "already created lock",
  168. })
  169. return
  170. }
  171. if git_model.IsErrLFSUnauthorizedAction(err) {
  172. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  173. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  174. Message: "You must have push access to create locks : " + err.Error(),
  175. })
  176. return
  177. }
  178. log.Error("Unable to CreateLFSLock in repository %-v at %s for user %-v: Error: %v", repository, req.Path, ctx.Doer, err)
  179. ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
  180. Message: "internal server error : Internal Server Error",
  181. })
  182. return
  183. }
  184. ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(ctx, lock)})
  185. }
  186. // VerifyLockHandler list locks for verification
  187. func VerifyLockHandler(ctx *context.Context) {
  188. userName := ctx.PathParam("username")
  189. repoName := strings.TrimSuffix(ctx.PathParam("reponame"), ".git")
  190. authorization := ctx.Req.Header.Get("Authorization")
  191. repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName)
  192. if err != nil {
  193. log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
  194. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  195. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  196. Message: "You must have push access to verify locks",
  197. })
  198. return
  199. }
  200. repository.MustOwner(ctx)
  201. context.CheckRepoScopedToken(ctx, repository, auth_model.Read)
  202. if ctx.Written() {
  203. return
  204. }
  205. authenticated := authenticate(ctx, repository, authorization, true, true)
  206. if !authenticated {
  207. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  208. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  209. Message: "You must have push access to verify locks",
  210. })
  211. return
  212. }
  213. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  214. cursor := max(ctx.FormInt("cursor"), 0)
  215. limit := ctx.FormInt("limit")
  216. if limit > setting.LFS.LocksPagingNum && setting.LFS.LocksPagingNum > 0 {
  217. limit = setting.LFS.LocksPagingNum
  218. } else if limit < 0 {
  219. limit = 0
  220. }
  221. lockList, err := git_model.GetLFSLockByRepoID(ctx, repository.ID, cursor, limit)
  222. if err != nil {
  223. log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
  224. ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
  225. Message: "unable to list locks : Internal Server Error",
  226. })
  227. return
  228. }
  229. next := ""
  230. if limit > 0 && len(lockList) == limit {
  231. next = strconv.Itoa(cursor + 1)
  232. }
  233. lockOursListAPI := make([]*api.LFSLock, 0, len(lockList))
  234. lockTheirsListAPI := make([]*api.LFSLock, 0, len(lockList))
  235. for _, l := range lockList {
  236. if l.OwnerID == ctx.Doer.ID {
  237. lockOursListAPI = append(lockOursListAPI, convert.ToLFSLock(ctx, l))
  238. } else {
  239. lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(ctx, l))
  240. }
  241. }
  242. ctx.JSON(http.StatusOK, api.LFSLockListVerify{
  243. Ours: lockOursListAPI,
  244. Theirs: lockTheirsListAPI,
  245. Next: next,
  246. })
  247. }
  248. // UnLockHandler delete locks
  249. func UnLockHandler(ctx *context.Context) {
  250. userName := ctx.PathParam("username")
  251. repoName := strings.TrimSuffix(ctx.PathParam("reponame"), ".git")
  252. authorization := ctx.Req.Header.Get("Authorization")
  253. repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName)
  254. if err != nil {
  255. log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
  256. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  257. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  258. Message: "You must have push access to delete locks",
  259. })
  260. return
  261. }
  262. repository.MustOwner(ctx)
  263. context.CheckRepoScopedToken(ctx, repository, auth_model.Write)
  264. if ctx.Written() {
  265. return
  266. }
  267. authenticated := authenticate(ctx, repository, authorization, true, true)
  268. if !authenticated {
  269. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  270. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  271. Message: "You must have push access to delete locks",
  272. })
  273. return
  274. }
  275. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  276. var req api.LFSLockDeleteRequest
  277. bodyReader := ctx.Req.Body
  278. defer bodyReader.Close()
  279. dec := json.NewDecoder(bodyReader)
  280. if err := dec.Decode(&req); err != nil {
  281. log.Warn("Failed to decode lock request as json. Error: %v", err)
  282. writeStatus(ctx, http.StatusBadRequest)
  283. return
  284. }
  285. lock, err := git_model.DeleteLFSLockByID(ctx, ctx.PathParamInt64("lid"), repository, ctx.Doer, req.Force)
  286. if err != nil {
  287. if git_model.IsErrLFSUnauthorizedAction(err) {
  288. ctx.Resp.Header().Set("WWW-Authenticate", `Basic realm="gitea-lfs"`)
  289. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  290. Message: "You must have push access to delete locks : " + err.Error(),
  291. })
  292. return
  293. }
  294. log.Error("Unable to DeleteLFSLockByID[%d] by user %-v with force %t: Error: %v", ctx.PathParamInt64("lid"), ctx.Doer, req.Force, err)
  295. ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
  296. Message: "unable to delete lock : Internal Server Error",
  297. })
  298. return
  299. }
  300. ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(ctx, lock)})
  301. }