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.

312 lines
7.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
  1. // Copyright 2014 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 v1
  5. import (
  6. "net/url"
  7. "path"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. api "github.com/gogits/go-gogs-client"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. // ToApiRepository converts repository to API format.
  18. func ToApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
  19. cl, err := repo.CloneLink()
  20. if err != nil {
  21. log.Error(4, "CloneLink: %v", err)
  22. }
  23. return &api.Repository{
  24. Id: repo.ID,
  25. Owner: *ToApiUser(owner),
  26. FullName: owner.Name + "/" + repo.Name,
  27. Private: repo.IsPrivate,
  28. Fork: repo.IsFork,
  29. HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
  30. CloneUrl: cl.HTTPS,
  31. SshUrl: cl.SSH,
  32. Permissions: permission,
  33. }
  34. }
  35. func SearchRepos(ctx *middleware.Context) {
  36. opt := models.SearchOption{
  37. Keyword: path.Base(ctx.Query("q")),
  38. Uid: com.StrTo(ctx.Query("uid")).MustInt64(),
  39. Limit: com.StrTo(ctx.Query("limit")).MustInt(),
  40. }
  41. if opt.Limit == 0 {
  42. opt.Limit = 10
  43. }
  44. // Check visibility.
  45. if ctx.IsSigned && opt.Uid > 0 {
  46. if ctx.User.Id == opt.Uid {
  47. opt.Private = true
  48. } else {
  49. u, err := models.GetUserByID(opt.Uid)
  50. if err != nil {
  51. ctx.JSON(500, map[string]interface{}{
  52. "ok": false,
  53. "error": err.Error(),
  54. })
  55. return
  56. }
  57. if u.IsOrganization() && u.IsOwnedBy(ctx.User.Id) {
  58. opt.Private = true
  59. }
  60. // FIXME: how about collaborators?
  61. }
  62. }
  63. repos, err := models.SearchRepositoryByName(opt)
  64. if err != nil {
  65. ctx.JSON(500, map[string]interface{}{
  66. "ok": false,
  67. "error": err.Error(),
  68. })
  69. return
  70. }
  71. results := make([]*api.Repository, len(repos))
  72. for i := range repos {
  73. if err = repos[i].GetOwner(); err != nil {
  74. ctx.JSON(500, map[string]interface{}{
  75. "ok": false,
  76. "error": err.Error(),
  77. })
  78. return
  79. }
  80. results[i] = &api.Repository{
  81. Id: repos[i].ID,
  82. FullName: path.Join(repos[i].Owner.Name, repos[i].Name),
  83. }
  84. }
  85. ctx.JSON(200, map[string]interface{}{
  86. "ok": true,
  87. "data": results,
  88. })
  89. }
  90. // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
  91. func ListMyRepos(ctx *middleware.Context) {
  92. ownRepos, err := models.GetRepositories(ctx.User.Id, true)
  93. if err != nil {
  94. ctx.APIError(500, "GetRepositories", err)
  95. return
  96. }
  97. numOwnRepos := len(ownRepos)
  98. accessibleRepos, err := ctx.User.GetAccessibleRepositories()
  99. if err != nil {
  100. ctx.APIError(500, "GetAccessibleRepositories", err)
  101. return
  102. }
  103. repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
  104. for i := range ownRepos {
  105. repos[i] = ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
  106. }
  107. i := numOwnRepos
  108. for repo, access := range accessibleRepos {
  109. repos[i] = ToApiRepository(repo.Owner, repo, api.Permission{
  110. Admin: access >= models.ACCESS_MODE_ADMIN,
  111. Push: access >= models.ACCESS_MODE_WRITE,
  112. Pull: true,
  113. })
  114. i++
  115. }
  116. ctx.JSON(200, &repos)
  117. }
  118. func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) {
  119. repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
  120. Name: opt.Name,
  121. Description: opt.Description,
  122. Gitignores: opt.Gitignores,
  123. License: opt.License,
  124. Readme: opt.Readme,
  125. IsPrivate: opt.Private,
  126. AutoInit: opt.AutoInit,
  127. })
  128. if err != nil {
  129. if models.IsErrRepoAlreadyExist(err) ||
  130. models.IsErrNameReserved(err) ||
  131. models.IsErrNamePatternNotAllowed(err) {
  132. ctx.APIError(422, "", err)
  133. } else {
  134. if repo != nil {
  135. if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil {
  136. log.Error(4, "DeleteRepository: %v", err)
  137. }
  138. }
  139. ctx.APIError(500, "CreateRepository", err)
  140. }
  141. return
  142. }
  143. ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true}))
  144. }
  145. // https://github.com/gogits/go-gogs-client/wiki/Repositories#create
  146. func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
  147. // Shouldn't reach this condition, but just in case.
  148. if ctx.User.IsOrganization() {
  149. ctx.APIError(422, "", "not allowed creating repository for organization")
  150. return
  151. }
  152. createRepo(ctx, ctx.User, opt)
  153. }
  154. func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) {
  155. org, err := models.GetOrgByName(ctx.Params(":org"))
  156. if err != nil {
  157. if models.IsErrUserNotExist(err) {
  158. ctx.APIError(422, "", err)
  159. } else {
  160. ctx.APIError(500, "GetOrgByName", err)
  161. }
  162. return
  163. }
  164. if !org.IsOwnedBy(ctx.User.Id) {
  165. ctx.APIError(403, "", "Given user is not owner of organization.")
  166. return
  167. }
  168. createRepo(ctx, org, opt)
  169. }
  170. func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
  171. ctxUser := ctx.User
  172. // Not equal means current user is an organization.
  173. if form.Uid != ctxUser.Id {
  174. org, err := models.GetUserByID(form.Uid)
  175. if err != nil {
  176. if models.IsErrUserNotExist(err) {
  177. ctx.APIError(422, "", err)
  178. } else {
  179. ctx.APIError(500, "GetUserByID", err)
  180. }
  181. return
  182. }
  183. ctxUser = org
  184. }
  185. if ctx.HasError() {
  186. ctx.APIError(422, "", ctx.GetErrMsg())
  187. return
  188. }
  189. if ctxUser.IsOrganization() {
  190. // Check ownership of organization.
  191. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  192. ctx.APIError(403, "", "Given user is not owner of organization.")
  193. return
  194. }
  195. }
  196. // Remote address can be HTTP/HTTPS/Git URL or local path.
  197. remoteAddr := form.CloneAddr
  198. if strings.HasPrefix(form.CloneAddr, "http://") ||
  199. strings.HasPrefix(form.CloneAddr, "https://") ||
  200. strings.HasPrefix(form.CloneAddr, "git://") {
  201. u, err := url.Parse(form.CloneAddr)
  202. if err != nil {
  203. ctx.APIError(422, "", err)
  204. return
  205. }
  206. if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 {
  207. u.User = url.UserPassword(form.AuthUsername, form.AuthPassword)
  208. }
  209. remoteAddr = u.String()
  210. } else if !com.IsDir(remoteAddr) {
  211. ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.")
  212. return
  213. }
  214. repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{
  215. Name: form.RepoName,
  216. Description: form.Description,
  217. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  218. IsMirror: form.Mirror,
  219. RemoteAddr: remoteAddr,
  220. })
  221. if err != nil {
  222. if repo != nil {
  223. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  224. log.Error(4, "DeleteRepository: %v", errDelete)
  225. }
  226. }
  227. ctx.APIError(500, "MigrateRepository", err)
  228. return
  229. }
  230. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  231. ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
  232. }
  233. func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
  234. owner, err := models.GetUserByName(ctx.Params(":username"))
  235. if err != nil {
  236. if models.IsErrUserNotExist(err) {
  237. ctx.APIError(422, "", err)
  238. } else {
  239. ctx.APIError(500, "GetUserByName", err)
  240. }
  241. return nil, nil
  242. }
  243. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  244. if err != nil {
  245. if models.IsErrRepoNotExist(err) {
  246. ctx.Error(404)
  247. } else {
  248. ctx.APIError(500, "GetRepositoryByName", err)
  249. }
  250. return nil, nil
  251. }
  252. return owner, repo
  253. }
  254. func GetRepo(ctx *middleware.Context) {
  255. owner, repo := parseOwnerAndRepo(ctx)
  256. if ctx.Written() {
  257. return
  258. }
  259. ctx.JSON(200, ToApiRepository(owner, repo, api.Permission{true, true, true}))
  260. }
  261. func DeleteRepo(ctx *middleware.Context) {
  262. owner, repo := parseOwnerAndRepo(ctx)
  263. if ctx.Written() {
  264. return
  265. }
  266. if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.Id) {
  267. ctx.APIError(403, "", "Given user is not owner of organization.")
  268. return
  269. }
  270. if err := models.DeleteRepository(owner.Id, repo.ID); err != nil {
  271. ctx.APIError(500, "DeleteRepository", err)
  272. return
  273. }
  274. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  275. ctx.Status(204)
  276. }