fix(repo): prevent double-write redirect collisions on dependency errors, fix ui (#38627)

1. fix `AddDependency` and ` RemoveDependency` to respond correctly
2. refactor the form to use "form-fetch-action"
3. fix the issue dependency icon layout regression

---------

Signed-off-by: Sudhanshu Singh <sudhanshuwriterblc@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Shudhanshu Singh
2026-07-25 20:39:36 +08:00
committed by GitHub
co-authored by wxiaoguang
parent 39cc4db4ba
commit fadaf36e95
4 changed files with 48 additions and 54 deletions
+22 -29
View File
@@ -4,8 +4,6 @@
package repo package repo
import ( import (
"net/http"
issues_model "gitea.dev/models/issues" issues_model "gitea.dev/models/issues"
access_model "gitea.dev/models/perm/access" access_model "gitea.dev/models/perm/access"
"gitea.dev/modules/setting" "gitea.dev/modules/setting"
@@ -23,7 +21,7 @@ func AddDependency(ctx *context.Context) {
// Check if the Repo is allowed to have dependencies // Check if the Repo is allowed to have dependencies
if !ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) { if !ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) {
ctx.HTTPError(http.StatusForbidden, "CanCreateIssueDependencies") ctx.JSONError(ctx.Locale.TrString("error.permission_denied"))
return return
} }
@@ -34,20 +32,17 @@ func AddDependency(ctx *context.Context) {
return return
} }
// Redirect
defer ctx.Redirect(issue.Link())
// Dependency // Dependency
dep, err := issues_model.GetIssueByID(ctx, depID) dep, err := issues_model.GetIssueByID(ctx, depID)
if err != nil { if err != nil {
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_issue_not_exist")) ctx.JSONError(ctx.Tr("repo.issues.dependency.add_error_dep_issue_not_exist"))
return return
} }
// Check if both issues are in the same repo if cross repository dependencies is not enabled // Check if both issues are in the same repo if cross repository dependencies is not enabled
if issue.RepoID != dep.RepoID { if issue.RepoID != dep.RepoID {
if !setting.Service.AllowCrossRepositoryDependencies { if !setting.Service.AllowCrossRepositoryDependencies {
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_not_same_repo")) ctx.JSONError(ctx.Tr("repo.issues.dependency.add_error_dep_not_same_repo"))
return return
} }
if err := dep.LoadRepo(ctx); err != nil { if err := dep.LoadRepo(ctx); err != nil {
@@ -61,29 +56,29 @@ func AddDependency(ctx *context.Context) {
return return
} }
if !depRepoPerm.CanReadIssuesOrPulls(dep.IsPull) { if !depRepoPerm.CanReadIssuesOrPulls(dep.IsPull) {
// you can't see this dependency ctx.JSONError(ctx.Locale.TrString("error.permission_denied"))
return return
} }
} }
// Check if issue and dependency is the same // Check if issue and dependency is the same
if dep.ID == issue.ID { if dep.ID == issue.ID {
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_same_issue")) ctx.JSONError(ctx.Tr("repo.issues.dependency.add_error_same_issue"))
return return
} }
err = issues_model.CreateIssueDependency(ctx, ctx.Doer, issue, dep) err = issues_model.CreateIssueDependency(ctx, ctx.Doer, issue, dep)
if err != nil { if issues_model.IsErrDependencyExists(err) {
if issues_model.IsErrDependencyExists(err) { ctx.JSONError(ctx.Tr("repo.issues.dependency.add_error_dep_exists"))
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_exists")) return
return } else if issues_model.IsErrCircularDependency(err) {
} else if issues_model.IsErrCircularDependency(err) { ctx.JSONError(ctx.Tr("repo.issues.dependency.add_error_cannot_create_circular"))
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_cannot_create_circular")) return
return } else if err != nil {
}
ctx.ServerError("CreateOrUpdateIssueDependency", err) ctx.ServerError("CreateOrUpdateIssueDependency", err)
return return
} }
ctx.JSONOK()
} }
// RemoveDependency removes the dependency // RemoveDependency removes the dependency
@@ -97,7 +92,7 @@ func RemoveDependency(ctx *context.Context) {
// Check if the Repo is allowed to have dependencies // Check if the Repo is allowed to have dependencies
if !ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) { if !ctx.Repo.CanCreateIssueDependencies(ctx, ctx.Doer, issue.IsPull) {
ctx.HTTPError(http.StatusForbidden, "CanCreateIssueDependencies") ctx.JSONError(ctx.Locale.TrString("error.permission_denied"))
return return
} }
@@ -119,7 +114,7 @@ func RemoveDependency(ctx *context.Context) {
case "blocking": case "blocking":
depType = issues_model.DependencyTypeBlocking depType = issues_model.DependencyTypeBlocking
default: default:
ctx.HTTPError(http.StatusBadRequest, "GetDependencyType") ctx.JSONError("invalid dependency type")
return return
} }
@@ -144,20 +139,18 @@ func RemoveDependency(ctx *context.Context) {
return return
} }
if !depRepoPerm.CanReadIssuesOrPulls(dep.IsPull) { if !depRepoPerm.CanReadIssuesOrPulls(dep.IsPull) {
ctx.Redirect(issue.Link()) ctx.JSONError(ctx.Locale.TrString("error.permission_denied"))
return return
} }
} }
if err = issues_model.RemoveIssueDependency(ctx, ctx.Doer, issue, dep, depType); err != nil { err = issues_model.RemoveIssueDependency(ctx, ctx.Doer, issue, dep, depType)
if issues_model.IsErrDependencyNotExists(err) { if issues_model.IsErrDependencyNotExists(err) {
ctx.Flash.Error(ctx.Tr("repo.issues.dependency.add_error_dep_not_exist")) ctx.JSONError(ctx.Tr("repo.issues.dependency.add_error_dep_not_exist"))
return return
} } else if err != nil {
ctx.ServerError("RemoveIssueDependency", err) ctx.ServerError("RemoveIssueDependency", err)
return return
} }
ctx.JSONOK()
// Redirect
ctx.Redirect(issue.Link())
} }
@@ -18,10 +18,10 @@
<span class="text" data-tooltip-content="{{if .Issue.IsPull}}{{ctx.Locale.Tr "repo.issues.dependency.pr_close_blocks"}}{{else}}{{ctx.Locale.Tr "repo.issues.dependency.issue_close_blocks"}}{{end}}"> <span class="text" data-tooltip-content="{{if .Issue.IsPull}}{{ctx.Locale.Tr "repo.issues.dependency.pr_close_blocks"}}{{else}}{{ctx.Locale.Tr "repo.issues.dependency.issue_close_blocks"}}{{end}}">
<strong>{{ctx.Locale.Tr "repo.issues.dependency.blocks_short"}}</strong> <strong>{{ctx.Locale.Tr "repo.issues.dependency.blocks_short"}}</strong>
</span> </span>
<div class="ui divided list"> <div class="flex-divided-list">
{{range .BlockingDependencies}} {{range .BlockingDependencies}}
<div class="item dependency{{if .Issue.IsClosed}} is-closed{{end}} flex-left-right"> <div class="item {{if .Issue.IsClosed}}is-closed{{end}} flex-left-right">
<div class="item-left tw-flex tw-justify-center tw-flex-col tw-flex-1 gt-ellipsis"> <div class="item-left">
<a class="muted issue-dependency-title gt-ellipsis" href="{{.Issue.Link}}" data-tooltip-content="#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}"> <a class="muted issue-dependency-title gt-ellipsis" href="{{.Issue.Link}}" data-tooltip-content="#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}">
#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}} #{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}
</a> </a>
@@ -29,7 +29,7 @@
{{.Repository.OwnerName}}/{{.Repository.Name}} {{.Repository.OwnerName}}/{{.Repository.Name}}
</div> </div>
</div> </div>
<div class="item-right tw-flex tw-items-center tw-m-1"> <div class="item-right">
{{if and $.CanCreateIssueDependencies (not $.Repository.IsArchived)}} {{if and $.CanCreateIssueDependencies (not $.Repository.IsArchived)}}
<a class="muted show-modal" data-modal="#issue-remove-dependency-confirm" <a class="muted show-modal" data-modal="#issue-remove-dependency-confirm"
data-modal-remove-dependency-id="{{.Issue.ID}}" data-modal-dependency-type="blocking" data-modal-remove-dependency-id="{{.Issue.ID}}" data-modal-dependency-type="blocking"
@@ -52,10 +52,10 @@
<span class="text" data-tooltip-content="{{if .Issue.IsPull}}{{ctx.Locale.Tr "repo.issues.dependency.pr_closing_blockedby"}}{{else}}{{ctx.Locale.Tr "repo.issues.dependency.issue_closing_blockedby"}}{{end}}"> <span class="text" data-tooltip-content="{{if .Issue.IsPull}}{{ctx.Locale.Tr "repo.issues.dependency.pr_closing_blockedby"}}{{else}}{{ctx.Locale.Tr "repo.issues.dependency.issue_closing_blockedby"}}{{end}}">
<strong>{{ctx.Locale.Tr "repo.issues.dependency.blocked_by_short"}}</strong> <strong>{{ctx.Locale.Tr "repo.issues.dependency.blocked_by_short"}}</strong>
</span> </span>
<div class="ui divided list"> <div class="flex-divided-list">
{{range .BlockedByDependencies}} {{range .BlockedByDependencies}}
<div class="item dependency{{if .Issue.IsClosed}} is-closed{{end}} flex-left-right"> <div class="item {{if .Issue.IsClosed}}is-closed{{end}} flex-left-right">
<div class="item-left tw-flex tw-justify-center tw-flex-col tw-flex-1 gt-ellipsis"> <div class="item-left">
<a class="muted issue-dependency-title gt-ellipsis" href="{{.Issue.Link}}" data-tooltip-content="#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}"> <a class="muted issue-dependency-title gt-ellipsis" href="{{.Issue.Link}}" data-tooltip-content="#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}">
#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}} #{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}
</a> </a>
@@ -63,7 +63,7 @@
{{.Repository.OwnerName}}/{{.Repository.Name}} {{.Repository.OwnerName}}/{{.Repository.Name}}
</div> </div>
</div> </div>
<div class="item-right tw-flex tw-items-center tw-m-1"> <div class="item-right">
{{if and $.CanCreateIssueDependencies (not $.Repository.IsArchived)}} {{if and $.CanCreateIssueDependencies (not $.Repository.IsArchived)}}
<a class="muted show-modal" data-modal="#issue-remove-dependency-confirm" <a class="muted show-modal" data-modal="#issue-remove-dependency-confirm"
data-modal-remove-dependency-id="{{.Issue.ID}}" data-modal-dependency-type="blockedBy" data-modal-remove-dependency-id="{{.Issue.ID}}" data-modal-dependency-type="blockedBy"
@@ -76,8 +76,8 @@
{{end}} {{end}}
{{if $.CanCreateIssueDependencies}} {{if $.CanCreateIssueDependencies}}
{{range .BlockedByDependenciesNotPermitted}} {{range .BlockedByDependenciesNotPermitted}}
<div class="item dependency{{if .Issue.IsClosed}} is-closed{{end}} flex-left-right"> <div class="item {{if .Issue.IsClosed}}is-closed{{end}} flex-left-right">
<div class="item-left tw-flex tw-justify-center tw-flex-col tw-flex-1 gt-ellipsis"> <div class="item-left">
<div class="gt-ellipsis"> <div class="gt-ellipsis">
<span data-tooltip-content="{{ctx.Locale.Tr "repo.issues.dependency.no_permission.can_remove"}}">{{svg "octicon-lock" 16}}</span> <span data-tooltip-content="{{ctx.Locale.Tr "repo.issues.dependency.no_permission.can_remove"}}">{{svg "octicon-lock" 16}}</span>
<span class="gt-ellipsis issue-dependency-title" data-tooltip-content="#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}"> <span class="gt-ellipsis issue-dependency-title" data-tooltip-content="#{{.Issue.Index}} {{.Issue.Title | ctx.RenderUtils.RenderEmoji}}">
@@ -88,7 +88,7 @@
{{.Repository.OwnerName}}/{{.Repository.Name}} {{.Repository.OwnerName}}/{{.Repository.Name}}
</div> </div>
</div> </div>
<div class="item-right tw-flex tw-items-center tw-m-1"> <div class="item-right">
{{if and $.CanCreateIssueDependencies (not $.Repository.IsArchived)}} {{if and $.CanCreateIssueDependencies (not $.Repository.IsArchived)}}
<a class="muted show-modal" data-modal="#issue-remove-dependency-confirm" <a class="muted show-modal" data-modal="#issue-remove-dependency-confirm"
data-modal-remove-dependency-id="{{.Issue.ID}}" data-modal-dependency-type="blocking" data-modal-remove-dependency-id="{{.Issue.ID}}" data-modal-dependency-type="blocking"
@@ -109,7 +109,7 @@
{{if and .CanCreateIssueDependencies (not .Repository.IsArchived)}} {{if and .CanCreateIssueDependencies (not .Repository.IsArchived)}}
<div> <div>
<form method="post" action="{{.Issue.Link}}/dependency/add" id="addDependencyForm"> <form method="post" action="{{.Issue.Link}}/dependency/add" id="addDependencyForm" class="form-fetch-action">
<div class="ui fluid action input"> <div class="ui fluid action input">
<div class="ui search selection dropdown" id="new-dependency-drop-list" data-issue-id="{{.Issue.ID}}" data-issue-cross-repo-search="{{.AllowCrossRepositoryDependencies}}"> <div class="ui search selection dropdown" id="new-dependency-drop-list" data-issue-id="{{.Issue.ID}}" data-issue-cross-repo-search="{{.AllowCrossRepositoryDependencies}}">
<input name="newDependency" type="hidden"> <input name="newDependency" type="hidden">
@@ -127,7 +127,7 @@
</div> </div>
{{if and .CanCreateIssueDependencies (not .Repository.IsArchived)}} {{if and .CanCreateIssueDependencies (not .Repository.IsArchived)}}
<form id="issue-remove-dependency-confirm" class="ui g-modal-confirm modal" method="post" action="{{.Issue.Link}}/dependency/delete"> <form id="issue-remove-dependency-confirm" class="ui g-modal-confirm modal form-fetch-action" method="post" action="{{.Issue.Link}}/dependency/delete">
<div class="header">{{svg "octicon-trash"}} {{ctx.Locale.Tr "repo.issues.dependency.remove_header"}}</div> <div class="header">{{svg "octicon-trash"}} {{ctx.Locale.Tr "repo.issues.dependency.remove_header"}}</div>
<div class="content"> <div class="content">
<input type="hidden" value="" name="removeDependencyID" class="remove-dependency-id"> <input type="hidden" value="" name="removeDependencyID" class="remove-dependency-id">
+6 -12
View File
@@ -19,6 +19,7 @@ import (
"gitea.dev/models/unittest" "gitea.dev/models/unittest"
user_model "gitea.dev/models/user" user_model "gitea.dev/models/user"
api "gitea.dev/modules/structs" api "gitea.dev/modules/structs"
"gitea.dev/modules/test"
repo_service "gitea.dev/services/repository" repo_service "gitea.dev/services/repository"
"gitea.dev/tests" "gitea.dev/tests"
@@ -175,12 +176,9 @@ func TestWebDeleteIssueDependencyCrossRepoPermission(t *testing.T) {
"removeDependencyID": strconv.FormatInt(dependencyIssue.ID, 10), "removeDependencyID": strconv.FormatInt(dependencyIssue.ID, 10),
"dependencyType": "blockedBy", "dependencyType": "blockedBy",
}) })
session.MakeRequest(t, req, http.StatusSeeOther) resp := session.MakeRequest(t, req, http.StatusBadRequest)
assert.Equal(t, "Permission denied.", test.ParseJSONError(resp.Body.Bytes()).ErrorMessage)
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueDependency{ unittest.AssertExistsAndLoadBean(t, &issues_model.IssueDependency{IssueID: targetIssue.ID, DependencyID: dependencyIssue.ID})
IssueID: targetIssue.ID,
DependencyID: dependencyIssue.ID,
})
assert.NoError(t, repo_service.AddOrUpdateCollaborator(t.Context(), dependencyRepo, user40, perm.AccessModeRead)) assert.NoError(t, repo_service.AddOrUpdateCollaborator(t.Context(), dependencyRepo, user40, perm.AccessModeRead))
@@ -188,10 +186,6 @@ func TestWebDeleteIssueDependencyCrossRepoPermission(t *testing.T) {
"removeDependencyID": strconv.FormatInt(dependencyIssue.ID, 10), "removeDependencyID": strconv.FormatInt(dependencyIssue.ID, 10),
"dependencyType": "blockedBy", "dependencyType": "blockedBy",
}) })
session.MakeRequest(t, req, http.StatusSeeOther) session.MakeRequest(t, req, http.StatusOK)
unittest.AssertNotExistsBean(t, &issues_model.IssueDependency{IssueID: targetIssue.ID, DependencyID: dependencyIssue.ID})
unittest.AssertNotExistsBean(t, &issues_model.IssueDependency{
IssueID: targetIssue.ID,
DependencyID: dependencyIssue.ID,
})
} }
+7
View File
@@ -649,6 +649,13 @@ td .commit-summary {
text-decoration: line-through; text-decoration: line-through;
} }
.repository.view.issue .ui.depending .item .item-left {
display: flex;
flex: 1;
flex-direction: column;
min-width: 0;
}
.repository .comment.form .content .field:first-child { .repository .comment.form .content .field:first-child {
clear: none; clear: none;
} }