From a62a88764970d70287395896f511a884129c29ac Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Mon, 20 May 2024 13:57:57 +0800 Subject: [PATCH 1/2] Fix incorrect "blob excerpt" link when comparing files (#31013) When comparing files between the base repo and forked repo, the "blob excerpt" link should point to the forked repo, because the commit doesn't exist in base repo. Co-authored-by: Giteabot (cherry picked from commit f48cc501c46a2d34eb701561f01d888d689d60d5) Conflicts: - templates/repo/diff/section_split.tmpl - templates/repo/diff/section_unified.tmpl Resolved the conflict by picking Gitea's change over ours, and porting it. - tests/integration/compare_test.go Kept our test, but picked the "compare all of the relevant links" part of the Gitea test. --- templates/repo/diff/section_split.tmpl | 7 ++++--- templates/repo/diff/section_unified.tmpl | 7 ++++--- tests/integration/compare_test.go | 12 ++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/templates/repo/diff/section_split.tmpl b/templates/repo/diff/section_split.tmpl index 7b1f72939e..50522d989e 100644 --- a/templates/repo/diff/section_split.tmpl +++ b/templates/repo/diff/section_split.tmpl @@ -1,4 +1,5 @@ {{$file := .file}} +{{$blobExcerptRepoLink := or $.root.CommitRepoLink $.root.RepoLink}} @@ -18,17 +19,17 @@
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} - {{end}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}} - {{end}} {{if eq $line.GetExpandDirection 2}} - {{end}} diff --git a/templates/repo/diff/section_unified.tmpl b/templates/repo/diff/section_unified.tmpl index d7fe75895b..ab78097f26 100644 --- a/templates/repo/diff/section_unified.tmpl +++ b/templates/repo/diff/section_unified.tmpl @@ -1,4 +1,5 @@ {{$file := .file}} +{{$blobExcerptRepoLink := or $.root.CommitRepoLink $.root.RepoLink}} @@ -14,17 +15,17 @@
{{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 5)}} - {{end}} {{if or (eq $line.GetExpandDirection 3) (eq $line.GetExpandDirection 4)}} - {{end}} {{if eq $line.GetExpandDirection 2}} - {{end}} diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index ffcdd2f23d..50c5b373ae 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -251,8 +251,16 @@ func TestCompareCodeExpand(t *testing.T) { owner.Name, repo.Name, forker.Name, repo.Name+"-copy") resp := session.MakeRequest(t, req, http.StatusOK) htmlDoc := NewHTMLParser(t, resp.Body) - htmlDoc.AssertElement(t, fmt.Sprintf("button.code-expander-button[hx-get^='/%s/%s/blob_excerpt/'] svg.octicon-fold-up", forker.Name, repo.Name+"-copy"), true) - htmlDoc.AssertElement(t, fmt.Sprintf("button.code-expander-button[hx-get^='/%s/%s/blob_excerpt/'] svg.octicon-fold-down", forker.Name, repo.Name+"-copy"), true) + + els := htmlDoc.Find(`button.code-expander-button[hx-get]`) + + // all the links in the comparison should be to the forked repo&branch + assert.NotZero(t, els.Length()) + expectedPrefix := fmt.Sprintf("/%s/%s/blob_excerpt/", forker.Name, repo.Name+"-copy") + for i := 0; i < els.Length(); i++ { + link := els.Eq(i).AttrOr("hx-get", "") + assert.True(t, strings.HasPrefix(link, expectedPrefix)) + } }) }) } From fd9ee1901b44224d41de762dc2b28fc8a4367951 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 24 May 2024 10:30:38 +0200 Subject: [PATCH 2/2] tests: Add a test for code expansion on PRs This adds a new test case to `TestCompareCodeExpand` to exercise the case where we're viewing a PR's diff. Signed-off-by: Gergely Nagy --- tests/integration/compare_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 50c5b373ae..5d585e061d 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -18,6 +18,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/optional" + "code.gitea.io/gitea/modules/test" repo_service "code.gitea.io/gitea/services/repository" files_service "code.gitea.io/gitea/services/repository/files" "code.gitea.io/gitea/tests" @@ -262,5 +263,30 @@ func TestCompareCodeExpand(t *testing.T) { assert.True(t, strings.HasPrefix(link, expectedPrefix)) } }) + + t.Run("code expander targets the repo in a PR", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + // Create a pullrequest + resp := testPullCreate(t, session, forker.Name, repo.Name+"-copy", false, "main", "code-expand", "This is a pull title") + + // Grab the URL for the PR + url := test.RedirectURL(resp) + "/files" + + // Visit the PR's diff + req := NewRequest(t, "GET", url) + resp = session.MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + + els := htmlDoc.Find(`button.code-expander-button[hx-get]`) + + // all the links in the comparison should be to the original repo&branch + assert.NotZero(t, els.Length()) + expectedPrefix := fmt.Sprintf("/%s/%s/blob_excerpt/", owner.Name, repo.Name) + for i := 0; i < els.Length(); i++ { + link := els.Eq(i).AttrOr("hx-get", "") + assert.True(t, strings.HasPrefix(link, expectedPrefix)) + } + }) }) }