Skip to content

Commit 20f47bb

Browse files
earl-warrenGustedGiteaBot
authored
fix generated source URL on rendered files (#26364)
- The permalink and 'Reference in New issue' URL of an renderable file (those where you can see the source and a rendered version of it, such as markdown) doesn't contain `?display=source`. This leads the issue that the URL doesn't have any effect, as by default the rendered version is shown and thus not the source. - Add `?display=source` to the permalink URL and to 'Reference in New Issue' if it's renderable file. - Add integration testing. Refs: https://codeberg.org/forgejo/forgejo/pulls/1088 Co-authored-by: Gusted <[email protected]> Co-authored-by: Giteabot <[email protected]>
1 parent 71d253f commit 20f47bb

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

models/fixtures/repo_unit.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,15 @@
637637
repo_id: 58
638638
type: 5
639639
created_unix: 946684810
640+
641+
-
642+
id: 96
643+
repo_id: 49
644+
type: 1
645+
created_unix: 946684810
646+
647+
-
648+
id: 97
649+
repo_id: 49
650+
type: 2
651+
created_unix: 946684810

templates/repo/view_file.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@
116116
</table>
117117
<div class="code-line-menu ui vertical pointing menu tippy-target">
118118
{{if $.Permission.CanRead $.UnitTypeIssues}}
119-
<a class="item ref-in-new-issue" data-url-issue-new="{{.RepoLink}}/issues/new" data-url-param-body-link="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}" rel="nofollow noindex">{{.locale.Tr "repo.issues.context.reference_issue"}}</a>
119+
<a class="item ref-in-new-issue" data-url-issue-new="{{.RepoLink}}/issues/new" data-url-param-body-link="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}" rel="nofollow noindex">{{.locale.Tr "repo.issues.context.reference_issue"}}</a>
120120
{{end}}
121121
<a class="item view_git_blame" href="{{.Repository.Link}}/blame/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.view_git_blame"}}</a>
122-
<a class="item copy-line-permalink" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}">{{.locale.Tr "repo.file_copy_permalink"}}</a>
122+
<a class="item copy-line-permalink" data-url="{{.Repository.Link}}/src/commit/{{PathEscape .CommitID}}/{{PathEscapeSegments .TreePath}}{{if $.HasSourceRenderedToggle}}?display=source{{end}}">{{.locale.Tr "repo.file_copy_permalink"}}</a>
123123
</div>
124124
{{end}}
125125
{{end}}

tests/integration/repo_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,39 @@ func TestMarkDownReadmeImageSubfolder(t *testing.T) {
408408
assert.True(t, exists, "Image not found in markdown file")
409409
assert.Equal(t, "/user2/repo1/media/branch/sub-home-md-img-check/docs/test-fake-img.jpg", src)
410410
}
411+
412+
func TestGeneratedSourceLink(t *testing.T) {
413+
defer tests.PrepareTestEnv(t)()
414+
415+
t.Run("Rendered file", func(t *testing.T) {
416+
defer tests.PrintCurrentTest(t)()
417+
req := NewRequest(t, "GET", "/user2/repo1/src/branch/master/README.md?display=source")
418+
resp := MakeRequest(t, req, http.StatusOK)
419+
doc := NewHTMLParser(t, resp.Body)
420+
421+
dataURL, exists := doc.doc.Find(".copy-line-permalink").Attr("data-url")
422+
assert.True(t, exists)
423+
assert.Equal(t, "/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d/README.md?display=source", dataURL)
424+
425+
dataURL, exists = doc.doc.Find(".ref-in-new-issue").Attr("data-url-param-body-link")
426+
assert.True(t, exists)
427+
assert.Equal(t, "/user2/repo1/src/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d/README.md?display=source", dataURL)
428+
})
429+
430+
t.Run("Non-Rendered file", func(t *testing.T) {
431+
defer tests.PrintCurrentTest(t)()
432+
433+
session := loginUser(t, "user27")
434+
req := NewRequest(t, "GET", "/user27/repo49/src/branch/master/test/test.txt")
435+
resp := session.MakeRequest(t, req, http.StatusOK)
436+
doc := NewHTMLParser(t, resp.Body)
437+
438+
dataURL, exists := doc.doc.Find(".copy-line-permalink").Attr("data-url")
439+
assert.True(t, exists)
440+
assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL)
441+
442+
dataURL, exists = doc.doc.Find(".ref-in-new-issue").Attr("data-url-param-body-link")
443+
assert.True(t, exists)
444+
assert.Equal(t, "/user27/repo49/src/commit/aacbdfe9e1c4b47f60abe81849045fa4e96f1d75/test/test.txt", dataURL)
445+
})
446+
}

0 commit comments

Comments
 (0)