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.

103 lines
2.5 KiB

  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 repo
  5. import (
  6. "fmt"
  7. "net/http"
  8. "github.com/martini-contrib/render"
  9. "github.com/martini-contrib/sessions"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/log"
  14. )
  15. func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
  16. data["Title"] = "Create repository"
  17. if req.Method == "GET" {
  18. r.HTML(200, "repo/create", data)
  19. return
  20. }
  21. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  22. r.HTML(200, "repo/create", data)
  23. return
  24. }
  25. // TODO: access check
  26. user, err := models.GetUserById(form.UserId)
  27. if err != nil {
  28. if err.Error() == models.ErrUserNotExist.Error() {
  29. data["HasError"] = true
  30. data["ErrorMsg"] = "User does not exist"
  31. auth.AssignForm(form, data)
  32. r.HTML(200, "repo/create", data)
  33. return
  34. }
  35. }
  36. fmt.Println(models.RepoPath(user.Name, form.RepoName))
  37. if err == nil {
  38. if repo, err := models.CreateRepository(user,
  39. form.RepoName, form.Description, form.Visibility == "private"); err == nil {
  40. err = models.InitRepository(repo, form.InitReadme == "true", form.Language)
  41. if err == nil {
  42. data["RepoName"] = user.Name + "/" + form.RepoName
  43. r.HTML(200, "repo/created", data)
  44. return
  45. }
  46. }
  47. }
  48. if err.Error() == models.ErrRepoAlreadyExist.Error() {
  49. data["HasError"] = true
  50. data["ErrorMsg"] = "Repository name has already been used"
  51. auth.AssignForm(form, data)
  52. r.HTML(200, "repo/create", data)
  53. return
  54. }
  55. data["ErrorMsg"] = err
  56. log.Error("repo.Create: %v", err)
  57. r.HTML(200, "base/error", data)
  58. }
  59. func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
  60. data["Title"] = "Delete repository"
  61. if req.Method == "GET" {
  62. r.HTML(200, "repo/delete", data)
  63. return
  64. }
  65. u := &models.User{}
  66. err := models.DeleteRepository(u, "")
  67. if err != nil {
  68. data["ErrorMsg"] = err
  69. log.Error("repo.Delete: %v", err)
  70. r.HTML(200, "base/error", data)
  71. }
  72. }
  73. func List(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
  74. data["Title"] = "Repositories"
  75. u := auth.SignedInUser(session)
  76. repos, err := models.GetRepositories(u)
  77. fmt.Println("repos", repos)
  78. if err != nil {
  79. data["ErrorMsg"] = err
  80. log.Error("repo.List: %v", err)
  81. r.HTML(200, "base/error", data)
  82. return
  83. }
  84. data["Repos"] = repos
  85. r.HTML(200, "repo/list", data)
  86. }