Skip to content

Commit bb6edb2

Browse files
GiteaBotlunny
andauthored
Fix a panic bug when head repository deleting (#30674) (#30676)
Backport #30674 & #30679 by @lunny When visiting a pull request files which head repository has been deleted, it will panic because headrepo is nil. --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent ad34d22 commit bb6edb2

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

routers/web/repo/pull.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -865,21 +865,21 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
865865

866866
if pull.HeadRepo != nil {
867867
ctx.Data["SourcePath"] = pull.HeadRepo.Link() + "/src/branch/" + util.PathEscapeSegments(pull.HeadBranch)
868-
}
869868

870-
if !pull.HasMerged && ctx.Doer != nil {
871-
perm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
872-
if err != nil {
873-
ctx.ServerError("GetUserRepoPermission", err)
874-
return
875-
}
869+
if !pull.HasMerged && ctx.Doer != nil {
870+
perm, err := access_model.GetUserRepoPermission(ctx, pull.HeadRepo, ctx.Doer)
871+
if err != nil {
872+
ctx.ServerError("GetUserRepoPermission", err)
873+
return
874+
}
876875

877-
if perm.CanWrite(unit.TypeCode) || issues_model.CanMaintainerWriteToBranch(ctx, perm, pull.HeadBranch, ctx.Doer) {
878-
ctx.Data["CanEditFile"] = true
879-
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
880-
ctx.Data["HeadRepoLink"] = pull.HeadRepo.Link()
881-
ctx.Data["HeadBranchName"] = pull.HeadBranch
882-
ctx.Data["BackToLink"] = setting.AppSubURL + ctx.Req.URL.RequestURI()
876+
if perm.CanWrite(unit.TypeCode) || issues_model.CanMaintainerWriteToBranch(ctx, perm, pull.HeadBranch, ctx.Doer) {
877+
ctx.Data["CanEditFile"] = true
878+
ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
879+
ctx.Data["HeadRepoLink"] = pull.HeadRepo.Link()
880+
ctx.Data["HeadBranchName"] = pull.HeadBranch
881+
ctx.Data["BackToLink"] = setting.AppSubURL + ctx.Req.URL.RequestURI()
882+
}
883883
}
884884
}
885885
}

tests/integration/pull_compare_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
package integration
55

66
import (
7+
"fmt"
78
"net/http"
9+
"net/url"
810
"testing"
911

12+
"code.gitea.io/gitea/models/db"
13+
issues_model "code.gitea.io/gitea/models/issues"
14+
repo_model "code.gitea.io/gitea/models/repo"
15+
"code.gitea.io/gitea/models/unittest"
16+
user_model "code.gitea.io/gitea/models/user"
17+
repo_service "code.gitea.io/gitea/services/repository"
1018
"code.gitea.io/gitea/tests"
1119

1220
"github.com/stretchr/testify/assert"
@@ -32,4 +40,36 @@ func TestPullCompare(t *testing.T) {
3240
doc := NewHTMLParser(t, resp.Body)
3341
editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
3442
assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
43+
44+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
45+
defer tests.PrepareTestEnv(t)()
46+
47+
session := loginUser(t, "user1")
48+
testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
49+
testCreateBranch(t, session, "user1", "repo1", "branch/master", "master1", http.StatusSeeOther)
50+
testEditFile(t, session, "user1", "repo1", "master1", "README.md", "Hello, World (Edited)\n")
51+
testPullCreate(t, session, "user1", "repo1", false, "master", "master1", "This is a pull title")
52+
53+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
54+
issueIndex := unittest.AssertExistsAndLoadBean(t, &issues_model.IssueIndex{GroupID: repo1.ID}, unittest.OrderBy("group_id ASC"))
55+
prFilesURL := fmt.Sprintf("/user2/repo1/pulls/%d/files", issueIndex.MaxIndex)
56+
req = NewRequest(t, "GET", prFilesURL)
57+
resp = session.MakeRequest(t, req, http.StatusOK)
58+
doc := NewHTMLParser(t, resp.Body)
59+
editButtonCount := doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
60+
assert.Greater(t, editButtonCount, 0, "Expected to find a button to edit a file in the PR diff view but there were none")
61+
62+
repoForked := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
63+
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
64+
65+
// delete the head repository and revisit the PR diff view
66+
err := repo_service.DeleteRepositoryDirectly(db.DefaultContext, user2, repoForked.ID)
67+
assert.NoError(t, err)
68+
69+
req = NewRequest(t, "GET", prFilesURL)
70+
resp = session.MakeRequest(t, req, http.StatusOK)
71+
doc = NewHTMLParser(t, resp.Body)
72+
editButtonCount = doc.doc.Find(".diff-file-header-actions a[href*='/_edit/']").Length()
73+
assert.EqualValues(t, editButtonCount, 0, "Expected not to find a button to edit a file in the PR diff view because head repository has been deleted")
74+
})
3575
}

0 commit comments

Comments
 (0)