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.

issue_dependency.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 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 repo
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // AddDependency adds new dependencies
  12. func AddDependency(ctx *context.Context) {
  13. issueIndex := ctx.ParamsInt64("index")
  14. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, issueIndex)
  15. if err != nil {
  16. ctx.ServerError("GetIssueByIndex", err)
  17. return
  18. }
  19. // Check if the Repo is allowed to have dependencies
  20. if !ctx.Repo.CanCreateIssueDependencies(ctx.User, issue.IsPull) {
  21. ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies")
  22. return
  23. }
  24. depID := ctx.QueryInt64("newDependency")
  25. if err = issue.LoadRepo(); err != nil {
  26. ctx.ServerError("LoadRepo", err)
  27. return
  28. }
  29. // Redirect
  30. defer ctx.Redirect(issue.HTMLURL(), http.StatusSeeOther)
  31. // Dependency
  32. dep, err := models.GetIssueByID(depID)
  33. if err != nil {
  34. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_issue_not_exist"))
  35. return
  36. }
  37. // Check if both issues are in the same repo if cross repository dependencies is not enabled
  38. if issue.RepoID != dep.RepoID && !setting.Service.AllowCrossRepositoryDependencies {
  39. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_not_same_repo"))
  40. return
  41. }
  42. // Check if issue and dependency is the same
  43. if dep.ID == issue.ID {
  44. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_same_issue"))
  45. return
  46. }
  47. err = models.CreateIssueDependency(ctx.User, issue, dep)
  48. if err != nil {
  49. if models.IsErrDependencyExists(err) {
  50. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_exists"))
  51. return
  52. } else if models.IsErrCircularDependency(err) {
  53. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_cannot_create_circular"))
  54. return
  55. } else {
  56. ctx.ServerError("CreateOrUpdateIssueDependency", err)
  57. return
  58. }
  59. }
  60. }
  61. // RemoveDependency removes the dependency
  62. func RemoveDependency(ctx *context.Context) {
  63. issueIndex := ctx.ParamsInt64("index")
  64. issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, issueIndex)
  65. if err != nil {
  66. ctx.ServerError("GetIssueByIndex", err)
  67. return
  68. }
  69. // Check if the Repo is allowed to have dependencies
  70. if !ctx.Repo.CanCreateIssueDependencies(ctx.User, issue.IsPull) {
  71. ctx.Error(http.StatusForbidden, "CanCreateIssueDependencies")
  72. return
  73. }
  74. depID := ctx.QueryInt64("removeDependencyID")
  75. if err = issue.LoadRepo(); err != nil {
  76. ctx.ServerError("LoadRepo", err)
  77. return
  78. }
  79. // Dependency Type
  80. depTypeStr := ctx.Req.PostForm.Get("dependencyType")
  81. var depType models.DependencyType
  82. switch depTypeStr {
  83. case "blockedBy":
  84. depType = models.DependencyTypeBlockedBy
  85. case "blocking":
  86. depType = models.DependencyTypeBlocking
  87. default:
  88. ctx.Error(http.StatusBadRequest, "GetDependecyType")
  89. return
  90. }
  91. // Dependency
  92. dep, err := models.GetIssueByID(depID)
  93. if err != nil {
  94. ctx.ServerError("GetIssueByID", err)
  95. return
  96. }
  97. if err = models.RemoveIssueDependency(ctx.User, issue, dep, depType); err != nil {
  98. if models.IsErrDependencyNotExists(err) {
  99. ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_not_exist"))
  100. return
  101. }
  102. ctx.ServerError("RemoveIssueDependency", err)
  103. return
  104. }
  105. // Redirect
  106. ctx.Redirect(issue.HTMLURL(), http.StatusSeeOther)
  107. }