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.8KB

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. repo_model "code.gitea.io/gitea/models/repo"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/convert"
  13. "code.gitea.io/gitea/modules/json"
  14. lfs_module "code.gitea.io/gitea/modules/lfs"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. api "code.gitea.io/gitea/modules/structs"
  18. )
  19. func handleLockListOut(ctx *context.Context, repo *repo_model.Repository, lock *models.LFSLock, err error) {
  20. if err != nil {
  21. if models.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(lock)},
  40. })
  41. }
  42. // GetListLockHandler list locks
  43. func GetListLockHandler(ctx *context.Context) {
  44. rv := getRequestContext(ctx)
  45. repository, err := repo_model.GetRepositoryByOwnerAndName(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(401, api.LFSLockError{
  50. Message: "You must have pull access to list locks",
  51. })
  52. return
  53. }
  54. repository.MustOwner()
  55. authenticated := authenticate(ctx, repository, rv.Authorization, true, false)
  56. if !authenticated {
  57. ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
  58. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  59. Message: "You must have pull access to list locks",
  60. })
  61. return
  62. }
  63. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  64. cursor := ctx.FormInt("cursor")
  65. if cursor < 0 {
  66. cursor = 0
  67. }
  68. limit := ctx.FormInt("limit")
  69. if limit > setting.LFS.LocksPagingNum && setting.LFS.LocksPagingNum > 0 {
  70. limit = setting.LFS.LocksPagingNum
  71. } else if limit < 0 {
  72. limit = 0
  73. }
  74. id := ctx.FormString("id")
  75. if id != "" { //Case where we request a specific id
  76. v, err := strconv.ParseInt(id, 10, 64)
  77. if err != nil {
  78. ctx.JSON(http.StatusBadRequest, api.LFSLockError{
  79. Message: "bad request : " + err.Error(),
  80. })
  81. return
  82. }
  83. lock, err := models.GetLFSLockByID(v)
  84. if err != nil && !models.IsErrLFSLockNotExist(err) {
  85. log.Error("Unable to get lock with ID[%s]: Error: %v", v, err)
  86. }
  87. handleLockListOut(ctx, repository, lock, err)
  88. return
  89. }
  90. path := ctx.FormString("path")
  91. if path != "" { //Case where we request a specific id
  92. lock, err := models.GetLFSLock(repository, path)
  93. if err != nil && !models.IsErrLFSLockNotExist(err) {
  94. log.Error("Unable to get lock for repository %-v with path %s: Error: %v", repository, path, err)
  95. }
  96. handleLockListOut(ctx, repository, lock, err)
  97. return
  98. }
  99. //If no query params path or id
  100. lockList, err := models.GetLFSLockByRepoID(repository.ID, cursor, limit)
  101. if err != nil {
  102. log.Error("Unable to list locks for repository ID[%d]: Error: %v", repository.ID, err)
  103. ctx.JSON(http.StatusInternalServerError, api.LFSLockError{
  104. Message: "unable to list locks : Internal Server Error",
  105. })
  106. return
  107. }
  108. lockListAPI := make([]*api.LFSLock, len(lockList))
  109. next := ""
  110. for i, l := range lockList {
  111. lockListAPI[i] = convert.ToLFSLock(l)
  112. }
  113. if limit > 0 && len(lockList) == limit {
  114. next = strconv.Itoa(cursor + 1)
  115. }
  116. ctx.JSON(http.StatusOK, api.LFSLockList{
  117. Locks: lockListAPI,
  118. Next: next,
  119. })
  120. }
  121. // PostLockHandler create lock
  122. func PostLockHandler(ctx *context.Context) {
  123. userName := ctx.Params("username")
  124. repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git")
  125. authorization := ctx.Req.Header.Get("Authorization")
  126. repository, err := repo_model.GetRepositoryByOwnerAndName(userName, repoName)
  127. if err != nil {
  128. log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err)
  129. ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
  130. ctx.JSON(401, api.LFSLockError{
  131. Message: "You must have push access to create locks",
  132. })
  133. return
  134. }
  135. repository.MustOwner()
  136. authenticated := authenticate(ctx, repository, authorization, true, true)
  137. if !authenticated {
  138. ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs")
  139. ctx.JSON(http.StatusUnauthorized, api.LFSLockError{
  140. Message: "You must have push access to create locks",
  141. })
  142. return
  143. }
  144. ctx.Resp.Header().Set("Content-Type", lfs_module.MediaType)
  145. var req api.LFSLockRequest
  146. bodyReader := ctx.Req.Body
  147. defer bodyReader.Close()
  148. dec := json.NewDecoder(bodyReader)
  149. if err := dec.Decode(&req); err != nil {
  150. log.Warn("Failed to decode lock request as json. Error: %v", err)
  151. writeStatus(ctx, 400)
  152. return
  153. }
  154. lock, err := models.CreateLFSLock(repository, &models.LFSLock{
  155. Path: req.Path,
  156. OwnerID: ctx.User.ID,
  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 := repo_model.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.OwnerID == 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 := repo_model.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"), repository, 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. }