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.

119 lines
3.1 KiB

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