You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo.go 7.9 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
  215. if err != nil {
  216. if repo != nil {
  217. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  218. log.Error(4, "DeleteRepository: %v", errDelete)
  219. }
  220. }
  221. ctx.APIError(500, "MigrateRepository", err)
  222. return
  223. }
  224. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  225. ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
  226. }
  227. func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
  228. owner, err := models.GetUserByName(ctx.Params(":username"))
  229. if err != nil {
  230. if models.IsErrUserNotExist(err) {
  231. ctx.APIError(422, "", err)
  232. } else {
  233. ctx.APIError(500, "GetUserByName", err)
  234. }
  235. return nil, nil
  236. }
  237. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  238. if err != nil {
  239. if models.IsErrRepoNotExist(err) {
  240. ctx.Error(404)
  241. } else {
  242. ctx.APIError(500, "GetRepositoryByName", err)
  243. }
  244. return nil, nil
  245. }
  246. return owner, repo
  247. }
  248. func GetRepo(ctx *middleware.Context) {
  249. owner, repo := parseOwnerAndRepo(ctx)
  250. if ctx.Written() {
  251. return
  252. }
  253. ctx.JSON(200, ToApiRepository(owner, repo, api.Permission{true, true, true}))
  254. }
  255. func DeleteRepo(ctx *middleware.Context) {
  256. owner, repo := parseOwnerAndRepo(ctx)
  257. if ctx.Written() {
  258. return
  259. }
  260. if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.Id) {
  261. ctx.APIError(403, "", "Given user is not owner of organization.")
  262. return
  263. }
  264. if err := models.DeleteRepository(owner.Id, repo.ID); err != nil {
  265. ctx.APIError(500, "DeleteRepository", err)
  266. return
  267. }
  268. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  269. ctx.Status(204)
  270. }