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.

locks.go 9.7KB

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