repos.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 admin
  5. import (
  6. "github.com/Unknwon/paginater"
  7. log "gopkg.in/clog.v1"
  8. "github.com/gogits/gogs/models"
  9. "github.com/gogits/gogs/modules/base"
  10. "github.com/gogits/gogs/modules/context"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. const (
  14. REPOS base.TplName = "admin/repo/list"
  15. )
  16. func Repos(ctx *context.Context) {
  17. ctx.Data["Title"] = ctx.Tr("admin.repositories")
  18. ctx.Data["PageIsAdmin"] = true
  19. ctx.Data["PageIsAdminRepositories"] = true
  20. page := ctx.QueryInt("page")
  21. if page <= 0 {
  22. page = 1
  23. }
  24. var (
  25. repos []*models.Repository
  26. count int64
  27. err error
  28. )
  29. keyword := ctx.Query("q")
  30. if len(keyword) == 0 {
  31. repos, err = models.Repositories(page, setting.UI.Admin.RepoPagingNum)
  32. if err != nil {
  33. ctx.Handle(500, "Repositories", err)
  34. return
  35. }
  36. count = models.CountRepositories(true)
  37. } else {
  38. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  39. Keyword: keyword,
  40. OrderBy: "id ASC",
  41. Private: true,
  42. Page: page,
  43. PageSize: setting.UI.Admin.RepoPagingNum,
  44. })
  45. if err != nil {
  46. ctx.Handle(500, "SearchRepositoryByName", err)
  47. return
  48. }
  49. }
  50. ctx.Data["Keyword"] = keyword
  51. ctx.Data["Total"] = count
  52. ctx.Data["Page"] = paginater.New(int(count), setting.UI.Admin.RepoPagingNum, page, 5)
  53. if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
  54. ctx.Handle(500, "LoadAttributes", err)
  55. return
  56. }
  57. ctx.Data["Repos"] = repos
  58. ctx.HTML(200, REPOS)
  59. }
  60. func DeleteRepo(ctx *context.Context) {
  61. repo, err := models.GetRepositoryByID(ctx.QueryInt64("id"))
  62. if err != nil {
  63. ctx.Handle(500, "GetRepositoryByID", err)
  64. return
  65. }
  66. if err := models.DeleteRepository(repo.MustOwner().ID, repo.ID); err != nil {
  67. ctx.Handle(500, "DeleteRepository", err)
  68. return
  69. }
  70. log.Trace("Repository deleted: %s/%s", repo.MustOwner().Name, repo.Name)
  71. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  72. ctx.JSON(200, map[string]interface{}{
  73. "redirect": setting.AppSubUrl + "/admin/repos?page=" + ctx.Query("page"),
  74. })
  75. }