|
|
|
// Copyright 2018-2022 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddParent adds new parents
|
|
|
|
func AddParent(ctx *context.Context) {
|
|
|
|
issueIndex := ctx.ParamsInt64("index")
|
|
|
|
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, issueIndex)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetIssueByIndex", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the Repo is allowed to have parents
|
|
|
|
if !ctx.Repo.CanCreateIssueParents(ctx.User, issue.IsPull) {
|
|
|
|
ctx.Error(http.StatusForbidden, "CanCreateIssueParents")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parentID := ctx.FormInt64("newParent")
|
|
|
|
|
|
|
|
if err = issue.LoadRepo(); err != nil {
|
|
|
|
ctx.ServerError("LoadRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect
|
|
|
|
defer ctx.Redirect(issue.HTMLURL(), http.StatusSeeOther)
|
|
|
|
|
|
|
|
// Parent
|
|
|
|
parent, err := models.GetIssueByID(parentID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.parent.add_error_dep_issue_not_exist"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if both issues are in the same repo if cross repository parents is not enabled
|
|
|
|
if issue.RepoID != parent.RepoID && !setting.Service.AllowCrossRepositoryParents {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.parent.add_error_dep_not_same_repo"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if issue and parent is the same
|
|
|
|
if parent.ID == issue.ID {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.parent.add_error_same_issue"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = models.CreateIssueParent(ctx.User, issue, parent)
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrParentExists(err) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.parent.add_error_dep_exists"))
|
|
|
|
return
|
|
|
|
} else if models.IsErrCircularParent(err) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.parent.add_error_cannot_create_circular"))
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("CreateOrUpdateIssueParent", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveParent removes the parent
|
|
|
|
func RemoveParent(ctx *context.Context) {
|
|
|
|
issueIndex := ctx.ParamsInt64("index")
|
|
|
|
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, issueIndex)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetIssueByIndex", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the Repo is allowed to have dependencies
|
|
|
|
if !ctx.Repo.CanCreateIssueParents(ctx.User, issue.IsPull) {
|
|
|
|
ctx.Error(http.StatusForbidden, "CanCreateIssueParents")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parentID := ctx.FormInt64("removeParentID")
|
|
|
|
|
|
|
|
if err = issue.LoadRepo(); err != nil {
|
|
|
|
ctx.ServerError("LoadRepo", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent Type
|
|
|
|
parentTypeStr := ctx.Req.PostForm.Get("parentType")
|
|
|
|
|
|
|
|
var parentType models.ParentType
|
|
|
|
|
|
|
|
switch parentTypeStr {
|
|
|
|
case "father":
|
|
|
|
parentType = models.ParentTypeFather
|
|
|
|
case "child":
|
|
|
|
parentType = models.ParentTypeChild
|
|
|
|
default:
|
|
|
|
ctx.Error(http.StatusBadRequest, "GetParentType")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent
|
|
|
|
parent, err := models.GetIssueByID(parentID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetIssueByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = models.RemoveIssueParent(ctx.User, issue, parent, parentType); err != nil {
|
|
|
|
if models.IsErrParentNotExists(err) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.issues.parent.add_error_dep_not_exist"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("RemoveIssueParent", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect
|
|
|
|
ctx.Redirect(issue.HTMLURL(), http.StatusSeeOther)
|
|
|
|
}
|